@@ -30,9 +30,16 @@ import {
30
30
} from './internal/pool-config'
31
31
import Session from './session'
32
32
import RxSession from './session-rx'
33
+ import { ALL } from './internal/request-message'
33
34
34
35
const DEFAULT_MAX_CONNECTION_LIFETIME = 60 * 60 * 1000 // 1 hour
35
36
37
+ /**
38
+ * The default record fetch size. This is used in Bolt V4 protocol to pull query execution result in batches.
39
+ * @type {number }
40
+ */
41
+ const DEFAULT_FETCH_SIZE = 1000
42
+
36
43
/**
37
44
* Constant that represents read session access mode.
38
45
* Should be used like this: `driver.session({ defaultAccessMode: neo4j.session.READ })`.
@@ -132,19 +139,23 @@ class Driver {
132
139
* @param {string } param.defaultAccessMode=WRITE - the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
133
140
* @param {string|string[] } param.bookmarks - the initial reference or references to some previous
134
141
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
142
+ * @param {number } param.fetchSize - the record fetch size of each batch of this session.
143
+ * Use {@link ALL} to always pull all records in one batch. This will override the config value set on driver config.
135
144
* @param {string } param.database - the database this session will operate on.
136
145
* @return {Session } new session.
137
146
*/
138
147
session ( {
139
148
defaultAccessMode = WRITE ,
140
149
bookmarks : bookmarkOrBookmarks ,
141
- database = ''
150
+ database = '' ,
151
+ fetchSize
142
152
} = { } ) {
143
153
return this . _newSession ( {
144
154
defaultAccessMode,
145
155
bookmarkOrBookmarks,
146
156
database,
147
- reactive : false
157
+ reactive : false ,
158
+ fetchSize : validateFetchSizeValue ( fetchSize , this . _config . fetchSize )
148
159
} )
149
160
}
150
161
@@ -229,7 +240,13 @@ class Driver {
229
240
/**
230
241
* @private
231
242
*/
232
- _newSession ( { defaultAccessMode, bookmarkOrBookmarks, database, reactive } ) {
243
+ _newSession ( {
244
+ defaultAccessMode,
245
+ bookmarkOrBookmarks,
246
+ database,
247
+ reactive,
248
+ fetchSize
249
+ } ) {
233
250
const sessionMode = Driver . _validateSessionMode ( defaultAccessMode )
234
251
const connectionProvider = this . _getOrCreateConnectionProvider ( )
235
252
const bookmark = bookmarkOrBookmarks
@@ -241,7 +258,8 @@ class Driver {
241
258
connectionProvider,
242
259
bookmark,
243
260
config : this . _config ,
244
- reactive
261
+ reactive,
262
+ fetchSize
245
263
} )
246
264
}
247
265
@@ -277,6 +295,10 @@ function sanitizeConfig (config) {
277
295
config . connectionAcquisitionTimeout ,
278
296
DEFAULT_ACQUISITION_TIMEOUT
279
297
)
298
+ config . fetchSize = validateFetchSizeValue (
299
+ config . fetchSize ,
300
+ DEFAULT_FETCH_SIZE
301
+ )
280
302
}
281
303
282
304
/**
@@ -293,6 +315,23 @@ function sanitizeIntValue (rawValue, defaultWhenAbsent) {
293
315
}
294
316
}
295
317
318
+ /**
319
+ * @private
320
+ */
321
+ function validateFetchSizeValue ( rawValue , defaultWhenAbsent ) {
322
+ const fetchSize = parseInt ( rawValue , 10 )
323
+ if ( fetchSize > 0 || fetchSize === ALL ) {
324
+ return fetchSize
325
+ } else if ( fetchSize === 0 || fetchSize < 0 ) {
326
+ throw new Error (
327
+ 'The fetch size can only be a positive value or -1 for ALL. However fetchSize = ' +
328
+ fetchSize
329
+ )
330
+ } else {
331
+ return defaultWhenAbsent
332
+ }
333
+ }
334
+
296
335
export { Driver , READ , WRITE }
297
336
298
337
export default Driver
0 commit comments