Skip to content

Commit

Permalink
Merge pull request #37 from serafdev/feature/multi-databases
Browse files Browse the repository at this point in the history
Multi Databases Support
  • Loading branch information
Eric Solender authored Aug 31, 2020
2 parents a85efad + 3f8af5d commit 2f9df41
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ jobs:
- name: Build
run: go build -v .
- name: Run Unit Tests
run: go test ./... -short
run: go test ./... -cover -short
- name: Start Neo4j Docker
run: |
docker-compose -f .github/docker-compose.yaml up -d
- name: Wait for neo4j to be ready
run: |
sleep 45
- name: Run Integration Test
run: go test ./... -run Integration
run: go test ./... -cover -run Integration
- name: Stop Neo4j Docker
run: |
docker-compose -f .github/docker-compose.yaml down --remove-orphans
Expand Down
12 changes: 11 additions & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package gogm

import (
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestIntegration(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,16 @@ func TestIntegration(t *testing.T) {
req.Nil(sess.PurgeDatabase())

req.Nil(sess.Close())

// Test Opening and Closing Session using SessionConfig
sessConf, err := NewSessionWithConfig(SessionConfig{
AccessMode: AccessModeRead,
})
req.Nil(err)
req.Nil(sessConf.Close())

req.Nil(driver.Close())

}

// runs with integration test
Expand Down
31 changes: 28 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ package gogm
import (
"errors"
"fmt"
"reflect"

dsl "github.com/mindstand/go-cypherdsl"
"github.com/neo4j/neo4j-go-driver/neo4j"
"reflect"
)

const defaultDepth = 1

const AccessModeRead = neo4j.AccessModeRead
const AccessModeWrite = neo4j.AccessModeWrite

type SessionConfig neo4j.SessionConfig

type Session struct {
neoSess neo4j.Session
tx neo4j.Transaction
Expand All @@ -46,9 +52,9 @@ func NewSession(readonly bool) (*Session, error) {
var mode neo4j.AccessMode

if readonly {
mode = neo4j.AccessModeRead
mode = AccessModeRead
} else {
mode = neo4j.AccessModeWrite
mode = AccessModeWrite
}

neoSess, err := driver.Session(mode)
Expand All @@ -63,6 +69,25 @@ func NewSession(readonly bool) (*Session, error) {
return session, nil
}

func NewSessionWithConfig(conf SessionConfig) (*Session, error) {
if driver == nil {
return nil, errors.New("driver cannot be nil")
}

neoSess, err := driver.NewSession(neo4j.SessionConfig{
AccessMode: conf.AccessMode,
Bookmarks: conf.Bookmarks,
DatabaseName: conf.DatabaseName,
})
if err != nil {
return nil, err
}

return &Session{
neoSess: neoSess,
DefaultDepth: defaultDepth,
}, nil
}
func (s *Session) Begin() error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
Expand Down

0 comments on commit 2f9df41

Please sign in to comment.