@@ -30,9 +30,16 @@ import {
3030} from './internal/pool-config'
3131import Session from './session'
3232import RxSession from './session-rx'
33+ import { ALL } from './internal/request-message'
3334
3435const DEFAULT_MAX_CONNECTION_LIFETIME = 60 * 60 * 1000 // 1 hour
3536
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+
3643/**
3744 * Constant that represents read session access mode.
3845 * Should be used like this: `driver.session({ defaultAccessMode: neo4j.session.READ })`.
@@ -132,19 +139,23 @@ class Driver {
132139 * @param {string } param.defaultAccessMode=WRITE - the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
133140 * @param {string|string[] } param.bookmarks - the initial reference or references to some previous
134141 * 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.
135144 * @param {string } param.database - the database this session will operate on.
136145 * @return {Session } new session.
137146 */
138147 session ( {
139148 defaultAccessMode = WRITE ,
140149 bookmarks : bookmarkOrBookmarks ,
141- database = ''
150+ database = '' ,
151+ fetchSize
142152 } = { } ) {
143153 return this . _newSession ( {
144154 defaultAccessMode,
145155 bookmarkOrBookmarks,
146156 database,
147- reactive : false
157+ reactive : false ,
158+ fetchSize : validateFetchSizeValue ( fetchSize , this . _config . fetchSize )
148159 } )
149160 }
150161
@@ -229,7 +240,13 @@ class Driver {
229240 /**
230241 * @private
231242 */
232- _newSession ( { defaultAccessMode, bookmarkOrBookmarks, database, reactive } ) {
243+ _newSession ( {
244+ defaultAccessMode,
245+ bookmarkOrBookmarks,
246+ database,
247+ reactive,
248+ fetchSize
249+ } ) {
233250 const sessionMode = Driver . _validateSessionMode ( defaultAccessMode )
234251 const connectionProvider = this . _getOrCreateConnectionProvider ( )
235252 const bookmark = bookmarkOrBookmarks
@@ -241,7 +258,8 @@ class Driver {
241258 connectionProvider,
242259 bookmark,
243260 config : this . _config ,
244- reactive
261+ reactive,
262+ fetchSize
245263 } )
246264 }
247265
@@ -277,6 +295,10 @@ function sanitizeConfig (config) {
277295 config . connectionAcquisitionTimeout ,
278296 DEFAULT_ACQUISITION_TIMEOUT
279297 )
298+ config . fetchSize = validateFetchSizeValue (
299+ config . fetchSize ,
300+ DEFAULT_FETCH_SIZE
301+ )
280302}
281303
282304/**
@@ -293,6 +315,23 @@ function sanitizeIntValue (rawValue, defaultWhenAbsent) {
293315 }
294316}
295317
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+
296335export { Driver , READ , WRITE }
297336
298337export default Driver
0 commit comments