Skip to content

Commit 597c20f

Browse files
committed
Update session creation API
The API is changed to leverage object destructuring, so that we have named arguments and default values.
1 parent 25b8cea commit 597c20f

File tree

8 files changed

+122
-70
lines changed

8 files changed

+122
-70
lines changed

src/driver.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,18 @@ class Driver {
178178
* it is closed, the underlying connection will be released to the connection
179179
* pool and made available for others to use.
180180
*
181-
* @param {string} [mode=WRITE] the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
182-
* @param {string|string[]} [bookmarkOrBookmarks=null] the initial reference or references to some previous
181+
* @param {string} [defaultAccessMode=WRITE] the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
182+
* @param {string|string[]} [bookmarks=null] the initial reference or references to some previous
183183
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
184+
* @param {string} [db=''] the database this session will connect to.
184185
* @return {Session} new session.
185186
*/
186-
session (mode, bookmarkOrBookmarks) {
187-
const sessionMode = Driver._validateSessionMode(mode)
187+
session ({
188+
defaultAccessMode = WRITE,
189+
bookmarks: bookmarkOrBookmarks,
190+
db = ''
191+
} = {}) {
192+
const sessionMode = Driver._validateSessionMode(defaultAccessMode)
188193
const connectionProvider = this._getOrCreateConnectionProvider()
189194
const bookmark = bookmarkOrBookmarks
190195
? new Bookmark(bookmarkOrBookmarks)

test/examples.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('examples', () => {
193193
'b.acme.com:7676',
194194
'c.acme.com:8787'
195195
])
196-
const session = driver.session(neo4j.WRITE)
196+
const session = driver.session({ defaultAccessMode: neo4j.WRITE })
197197

198198
session
199199
.run('CREATE (n:Person { name: $name })', { name: name })
@@ -628,7 +628,7 @@ describe('examples', () => {
628628
const savedBookmarks = []
629629

630630
// Create the first person and employment relationship.
631-
const session1 = driver.session(neo4j.WRITE)
631+
const session1 = driver.session({ defaultAccessMode: neo4j.WRITE })
632632
const first = session1
633633
.writeTransaction(tx => addCompany(tx, 'Wayne Enterprises'))
634634
.then(() => session1.writeTransaction(tx => addPerson(tx, 'Alice')))
@@ -644,7 +644,7 @@ describe('examples', () => {
644644
})
645645

646646
// Create the second person and employment relationship.
647-
const session2 = driver.session(neo4j.WRITE)
647+
const session2 = driver.session({ defaultAccessMode: neo4j.WRITE })
648648
const second = session2
649649
.writeTransaction(tx => addCompany(tx, 'LexCorp'))
650650
.then(() => session2.writeTransaction(tx => addPerson(tx, 'Bob')))
@@ -659,7 +659,10 @@ describe('examples', () => {
659659

660660
// Create a friendship between the two people created above.
661661
const last = Promise.all([first, second]).then(ignore => {
662-
const session3 = driver.session(neo4j.WRITE, savedBookmarks)
662+
const session3 = driver.session({
663+
defaultAccessMode: neo4j.WRITE,
664+
bookmarks: savedBookmarks
665+
})
663666

664667
return session3
665668
.writeTransaction(tx => makeFriends(tx, 'Alice', 'Bob'))

test/internal/node/direct.driver.boltkit.test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ describe('direct driver with stub server', () => {
7575

7676
boltStub.run(() => {
7777
const driver = boltStub.newDriver('bolt://127.0.0.1:9001')
78-
const session = driver.session(READ, 'neo4j:bookmark:v1:tx42')
78+
const session = driver.session({
79+
defaultAccessMode: READ,
80+
bookmarks: ['neo4j:bookmark:v1:tx42']
81+
})
7982
const tx = session.beginTransaction()
8083
tx.run('MATCH (n) RETURN n.name AS name').then(result => {
8184
const records = result.records
@@ -111,7 +114,10 @@ describe('direct driver with stub server', () => {
111114

112115
boltStub.run(() => {
113116
const driver = boltStub.newDriver('bolt://127.0.0.1:9001')
114-
const session = driver.session(WRITE, 'neo4j:bookmark:v1:tx42')
117+
const session = driver.session({
118+
defaultAccessMode: WRITE,
119+
bookmarks: ['neo4j:bookmark:v1:tx42']
120+
})
115121
const tx = session.beginTransaction()
116122
tx.run("CREATE (n {name:'Bob'})").then(result => {
117123
const records = result.records
@@ -145,7 +151,10 @@ describe('direct driver with stub server', () => {
145151

146152
boltStub.run(() => {
147153
const driver = boltStub.newDriver('bolt://127.0.0.1:9001')
148-
const session = driver.session(WRITE, 'neo4j:bookmark:v1:tx42')
154+
const session = driver.session({
155+
defaultAccessMode: WRITE,
156+
bookmarks: ['neo4j:bookmark:v1:tx42']
157+
})
149158
const writeTx = session.beginTransaction()
150159
writeTx.run("CREATE (n {name:'Bob'})").then(result => {
151160
const records = result.records
@@ -192,7 +201,10 @@ describe('direct driver with stub server', () => {
192201

193202
boltStub.run(() => {
194203
const driver = boltStub.newDriver('bolt://127.0.0.1:9001')
195-
const session = driver.session(WRITE, 'neo4j:bookmark:v1:tx42')
204+
const session = driver.session({
205+
defaultAccessMode: WRITE,
206+
bookmarks: ['neo4j:bookmark:v1:tx42']
207+
})
196208
const writeTx = session.beginTransaction()
197209
writeTx.run("CREATE (n {name:'Bob'})").then(result => {
198210
const records = result.records
@@ -239,7 +251,10 @@ describe('direct driver with stub server', () => {
239251

240252
boltStub.run(() => {
241253
const driver = boltStub.newDriver('bolt://127.0.0.1:9001')
242-
const session = driver.session(WRITE, 'neo4j:bookmark:v1:tx42')
254+
const session = driver.session({
255+
defaultAccessMode: WRITE,
256+
bookmarks: ['neo4j:bookmark:v1:tx42']
257+
})
243258
const writeTx = session.beginTransaction()
244259
writeTx.run("CREATE (n {name:'Bob'})").then(result => {
245260
const records = result.records

0 commit comments

Comments
 (0)