Skip to content

Commit 391592d

Browse files
committed
Moving configuration to the session
1 parent 87dc0a6 commit 391592d

File tree

14 files changed

+178
-149
lines changed

14 files changed

+178
-149
lines changed

packages/core/src/bookmark-manager.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
/**
2121
* Interface for the piece of software responsible for keeping track of current active bookmarks accross the driver.
2222
* @interface
23+
* @since 5.0
24+
* @experimental
2325
*/
2426
export default class BookmarkManager {
25-
constructor () {
27+
/**
28+
* @constructor
29+
* @private
30+
*/
31+
private constructor () {
2632
throw new Error('Not implemented')
2733
}
2834

@@ -31,6 +37,7 @@ export default class BookmarkManager {
3137
*
3238
* This method will be called when auto-commit queries finish and when explicit transactions
3339
* get commited.
40+
*
3441
* @param {string} database The database which the bookmarks belongs to
3542
* @param {Iterable<string>} previousBookmarks The bookmarks used when starting the transaction
3643
* @param {Iterable<string>} newBookmarks The new bookmarks received at the end of the transaction.
@@ -81,7 +88,10 @@ export interface BookmarkManagerConfig {
8188

8289
/**
8390
* @typedef {Object} BookmarkManagerConfig
84-
* @property {Map<string,Iterable<string>>} [initialBookmarks] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
91+
*
92+
* @since 5.0
93+
* @experimental
94+
* @property {Map<string,Iterable<string>>} [initialBookmarks@experimental] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
8595
* @property {function([database]: string):Promise<Iterable<string>>} [bookmarksSupplier] Called for supplying extra bookmarks to the BookmarkManager
8696
* 1. supplying bookmarks from the given database when the default BookmarkManager's `.getBookmarks(database)` gets called.
8797
* 2. supplying all the bookmarks when the default BookmarkManager's `.getAllBookmarks()` gets called
@@ -90,6 +100,8 @@ export interface BookmarkManagerConfig {
90100
/**
91101
* Provides an configured {@link BookmarkManager} instance.
92102
*
103+
* @since 5.0
104+
* @experimental
93105
* @param {BookmarkManagerConfig} [config={}]
94106
* @returns {BookmarkManager}
95107
*/

packages/core/src/driver.ts

Lines changed: 112 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,110 @@ interface DriverConfig {
9696
trust?: TrustStrategy
9797
fetchSize?: number
9898
logging?: LoggingConfig
99+
}
100+
101+
/**
102+
* The session configuration
103+
*
104+
* @interface
105+
*
106+
* @param {string} defaultAccessMode=WRITE - The access mode of this session, allowed values are {@link READ} and {@link WRITE}.
107+
* @param {string|string[]} bookmarks - The initial reference or references to some previous
108+
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
109+
* @param {number} fetchSize - The record fetch size of each batch of this session.
110+
* Use {@link FETCH_ALL} to always pull all records in one batch. This will override the config value set on driver config.
111+
* @param {string} database - The database this session will operate on.
112+
* @param {string} impersonatedUser - The username which the user wants to impersonate for the duration of the session.
113+
* @param {BookmarkManager} [bookmarkManager] = The bookmark manager
114+
*/
115+
class SessionConfig {
116+
defaultAccessMode?: SessionMode
117+
bookmarks?: string | string[]
118+
database?: string
119+
impersonatedUser?: string
120+
fetchSize?: number
99121
bookmarkManager?: BookmarkManager
122+
123+
/**
124+
* @constructor
125+
* @private
126+
*/
127+
constructor () {
128+
/**
129+
* The access mode of this session, allowed values are {@link READ} and {@link WRITE}.
130+
* **Default**: {@link WRITE}
131+
* @type {string}
132+
*/
133+
this.defaultAccessMode = WRITE
134+
/**
135+
* The initial reference or references to some previous
136+
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
137+
* @type {string|string[]|undefined}
138+
*/
139+
this.bookmarks = []
140+
141+
/**
142+
* The database this session will operate on.
143+
*
144+
* @type {string|undefined}
145+
*/
146+
this.database = ''
147+
148+
/**
149+
* The username which the user wants to impersonate for the duration of the session.
150+
*
151+
* @type {string|undefined}
152+
*/
153+
this.impersonatedUser = undefined
154+
155+
/**
156+
* The record fetch size of each batch of this session.
157+
*
158+
* Use {@link FETCH_ALL} to always pull all records in one batch. This will override the config value set on driver config.
159+
*
160+
* @type {number|undefined}
161+
*/
162+
this.fetchSize = undefined
163+
/**
164+
* Configure a BookmarkManager for the session to use
165+
*
166+
* A BookmarkManager is a piece of software responsible for keeping casual consistency between different sessions by sharing bookmarks
167+
* between the them.
168+
* Enabling it is done by supplying an BookmarkManager implementation instance to this param.
169+
* A default implementation could be acquired by calling the factory function {@link bookmarkManager}.
170+
*
171+
* **Warning**: Share the same BookmarkManager instance accross all session can have a negative impact
172+
* on performance since all the queries will wait for the latest changes being propagated across the cluster.
173+
* For keeping consistency between a group of queries, use {@link Session} for grouping them.
174+
* For keeping consistency between a group of sessions, use {@link BookmarkManager} instance for groupping them.
175+
*
176+
* @example
177+
* const bookmarkManager = neo4j.bookmarkManager()
178+
* const linkedSession1 = driver.session({ database:'neo4j', bookmarkManager })
179+
* const linkedSession2 = driver.session({ database:'neo4j', bookmarkManager })
180+
* const unlinkedSession = driver.session({ database:'neo4j' })
181+
*
182+
* // Creating Driver User
183+
* const createUserQueryResult = await linkedSession1.run('CREATE (p:Person {name: $name})', { name: 'Driver User'})
184+
*
185+
* // Reading Driver User will *NOT* wait of the changes being propagated to the server before RUN the query
186+
* // So the 'Driver User' person might not exist in the Result
187+
* const unlinkedReadResult = await unlinkedSession.run('CREATE (p:Person {name: $name}) RETURN p', { name: 'Driver User'})
188+
*
189+
* // Reading Driver User will wait of the changes being propagated to the server before RUN the query
190+
* // So the 'Driver User' person should exist in the Result, unless deleted.
191+
* const linkedSesssion2 = await linkedSession2.run('CREATE (p:Person {name: $name}) RETURN p', { name: 'Driver User'})
192+
*
193+
* await linkedSession1.close()
194+
* await linkedSession2.close()
195+
* await unlinkedSession.close()
196+
*
197+
* @experimental
198+
* @type {BookmarkManager|undefined}
199+
* @since 5.0
200+
*/
201+
this.bookmarkManager = undefined
202+
}
100203
}
101204

102205
/**
@@ -285,15 +388,7 @@ class Driver {
285388
* pool and made available for others to use.
286389
*
287390
* @public
288-
* @param {Object} param - The object parameter
289-
* @param {string} param.defaultAccessMode=WRITE - The access mode of this session, allowed values are {@link READ} and {@link WRITE}.
290-
* @param {string|string[]} param.bookmarks - The initial reference or references to some previous
291-
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
292-
* @param {number} param.fetchSize - The record fetch size of each batch of this session.
293-
* Use {@link FETCH_ALL} to always pull all records in one batch. This will override the config value set on driver config.
294-
* @param {string} param.database - The database this session will operate on.
295-
* @param {string} param.impersonatedUser - The username which the user wants to impersonate for the duration of the session.
296-
* @param {boolean} param.ignoreBookmarkManager - Disable the bookmark manager usage in the session.
391+
* @param {SessionConfig} param - The session configuration
297392
* @return {Session} new session.
298393
*/
299394
session ({
@@ -302,15 +397,8 @@ class Driver {
302397
database = '',
303398
impersonatedUser,
304399
fetchSize,
305-
ignoreBookmarkManager
306-
}: {
307-
defaultAccessMode?: SessionMode
308-
bookmarks?: string | string[]
309-
database?: string
310-
impersonatedUser?: string
311-
fetchSize?: number
312-
ignoreBookmarkManager?: boolean
313-
} = {}): Session {
400+
bookmarkManager
401+
}: SessionConfig = {}): Session {
314402
return this._newSession({
315403
defaultAccessMode,
316404
bookmarkOrBookmarks,
@@ -319,7 +407,7 @@ class Driver {
319407
impersonatedUser,
320408
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
321409
fetchSize: validateFetchSizeValue(fetchSize, this._config.fetchSize!),
322-
ignoreBookmarkManager
410+
bookmarkManager
323411
})
324412
}
325413

@@ -356,24 +444,22 @@ class Driver {
356444
reactive,
357445
impersonatedUser,
358446
fetchSize,
359-
ignoreBookmarkManager
447+
bookmarkManager
360448
}: {
361449
defaultAccessMode: SessionMode
362450
bookmarkOrBookmarks?: string | string[]
363451
database: string
364452
reactive: boolean
365453
impersonatedUser?: string
366454
fetchSize: number
367-
ignoreBookmarkManager?: boolean
455+
bookmarkManager?: BookmarkManager
368456
}): Session {
369457
const sessionMode = Session._validateSessionMode(defaultAccessMode)
370458
const connectionProvider = this._getOrCreateConnectionProvider()
371459
const bookmarks = bookmarkOrBookmarks != null
372460
? new Bookmarks(bookmarkOrBookmarks)
373461
: Bookmarks.empty()
374-
const bookmarkManager = ignoreBookmarkManager !== true
375-
? this._config.bookmarkManager
376-
: undefined
462+
377463
return this._createSession({
378464
mode: sessionMode,
379465
database: database ?? '',
@@ -486,7 +572,7 @@ function validateFetchSizeValue (
486572
/**
487573
* @private
488574
*/
489-
function extractConnectionTimeout (config: any): number|null {
575+
function extractConnectionTimeout (config: any): number | null {
490576
const configuredTimeout = parseInt(config.connectionTimeout, 10)
491577
if (configuredTimeout === 0) {
492578
// timeout explicitly configured to 0
@@ -513,5 +599,5 @@ function createHostNameResolver (config: any): ConfiguredCustomResolver {
513599
}
514600

515601
export { Driver, READ, WRITE }
516-
602+
export type { SessionConfig }
517603
export default Driver

packages/core/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import Session, { TransactionConfig } from './session'
7676
import Driver, * as driver from './driver'
7777
import auth from './auth'
7878
import BookmarkManager, { BookmarkManagerConfig, bookmarkManager } from './bookmark-manager'
79+
import { SessionConfig } from './driver'
7980
import * as types from './types'
8081
import * as json from './json'
8182
import * as internal from './internal' // todo: removed afterwards
@@ -219,7 +220,8 @@ export type {
219220
ResultObserver,
220221
TransactionConfig,
221222
BookmarkManager,
222-
BookmarkManagerConfig
223+
BookmarkManagerConfig,
224+
SessionConfig
223225
}
224226

225227
export default forExport

packages/core/src/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

20-
import BookmarkManager from './bookmark-manager'
21-
2220
/**
2321
* @private
2422
*/
@@ -66,7 +64,6 @@ export interface Config {
6664
logging?: LoggingConfig
6765
resolver?: (address: string) => string[] | Promise<string[]>
6866
userAgent?: string
69-
bookmarkManager?: BookmarkManager
7067
}
7168

7269
/**

packages/core/test/driver.test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ describe('Driver', () => {
8989
const manager = bookmarkManager()
9090
const driver = new Driver(
9191
META_INFO,
92-
{ ...CONFIG, bookmarkManager: manager },
92+
{ ...CONFIG },
9393
mockCreateConnectonProvider(connectionProvider),
9494
createSession
9595
)
9696

97-
const session = driver.session()
97+
const session = driver.session({ bookmarkManager: manager })
9898

9999
try {
100100
expect(createSession).toBeCalledWith(expect.objectContaining({
@@ -107,48 +107,47 @@ describe('Driver', () => {
107107
}
108108
})
109109

110-
it('should create session without bookmark manager when bookmark manager is ignored', async () => {
110+
it.each([
111+
[[], Bookmarks.empty()],
112+
['bookmark', new Bookmarks('bookmark')],
113+
[['bookmark'], new Bookmarks(['bookmark'])],
114+
[['bookmark1', 'bookmark2'], new Bookmarks(['bookmark1', 'bookmark2'])]
115+
])('should create session with bookmark manager when bookmark set', async (bookmarks, expectedBookmarks) => {
111116
const manager = bookmarkManager()
112117
const driver = new Driver(
113118
META_INFO,
114-
{ ...CONFIG, bookmarkManager: manager },
119+
{ ...CONFIG },
115120
mockCreateConnectonProvider(connectionProvider),
116121
createSession
117122
)
118123

119-
const session = driver.session({ ignoreBookmarkManager: true })
124+
const session = driver.session({ bookmarks, bookmarkManager: manager })
120125

121126
try {
122127
expect(createSession).toBeCalledWith(expect.objectContaining({
123-
bookmarkManager: undefined,
124-
bookmarks: Bookmarks.empty()
128+
bookmarkManager: manager,
129+
bookmarks: expectedBookmarks
125130
}))
126131
} finally {
127132
await session.close()
128133
await driver.close()
129134
}
130135
})
131136

132-
it.each([
133-
[[], Bookmarks.empty()],
134-
['bookmark', new Bookmarks('bookmark')],
135-
[['bookmark'], new Bookmarks(['bookmark'])],
136-
[['bookmark1', 'bookmark2'], new Bookmarks(['bookmark1', 'bookmark2'])]
137-
])('should create session with bookmark manager when bookmark set', async (bookmarks, expectedBookmarks) => {
138-
const manager = bookmarkManager()
137+
it('should create session without bookmark manager when no bookmark manager is set', async () => {
139138
const driver = new Driver(
140139
META_INFO,
141-
{ ...CONFIG, bookmarkManager: manager },
140+
{ ...CONFIG },
142141
mockCreateConnectonProvider(connectionProvider),
143142
createSession
144143
)
145144

146-
const session = driver.session({ bookmarks })
145+
const session = driver.session()
147146

148147
try {
149148
expect(createSession).toBeCalledWith(expect.objectContaining({
150-
bookmarkManager: manager,
151-
bookmarks: expectedBookmarks
149+
bookmarkManager: undefined,
150+
bookmarks: Bookmarks.empty()
152151
}))
153152
} finally {
154153
await session.close()

packages/neo4j-driver-lite/src/index.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ import {
7272
auth,
7373
BookmarkManager,
7474
bookmarkManager,
75-
BookmarkManagerConfig
75+
BookmarkManagerConfig,
76+
SessionConfig
7677
} from 'neo4j-driver-core'
7778
import {
7879
DirectConnectionProvider,
@@ -202,21 +203,6 @@ const {
202203
* logger: (level, message) => console.log(level + ' ' + message)
203204
* },
204205
*
205-
* // Configure a BookmarkManager for the driver to use
206-
* //
207-
* // A BookmarkManager is a piece of software responsible for keeping casual consistency between different sessions by sharing bookmarks
208-
* // between the them.
209-
* // Enabling it is done by supplying an BookmarkManager implementation instance to this param.
210-
* // A default implementation could be acquired by calling the factory function bookmarkManager.
211-
* //
212-
* // **Warning**: Enabling the BookmarkManager can have a negative impact on performance since all the queries will wait for the latest changes
213-
* // being propagated across the cluster.
214-
* // For keeping consistency between a group of queries, use Session for grouping them.
215-
* //
216-
* // Example:
217-
* // const driver = neo4j.driver(url, auth, { bookmarkManager: neo4j.bookmarkManager() })
218-
* bookmarkManager: undefined, // Disabled
219-
*
220206
* // Specify a custom server address resolver function used by the routing driver to resolve the initial address used to create the driver.
221207
* // Such resolution happens:
222208
* // * during the very first rediscovery when driver is created
@@ -544,6 +530,7 @@ export type {
544530
ResultObserver,
545531
NotificationPosition,
546532
BookmarkManager,
547-
BookmarkManagerConfig
533+
BookmarkManagerConfig,
534+
SessionConfig
548535
}
549536
export default forExport

0 commit comments

Comments
 (0)