16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
- import { ResultStreamObserver , FailedObserver } from './internal/bolt'
20
- import { newError , internal , Result , Transaction } from 'neo4j-driver-core'
21
- import ConnectionHolder from './internal/connection-holder'
19
+ import { ResultStreamObserver , FailedObserver } from './internal/observers'
20
+ import { validateQueryAndParameters } from './internal/util'
21
+ import { newError } from './error'
22
+ import Result from './result'
23
+ import Transaction from './transaction'
24
+ import { ConnectionHolder } from './internal/connection-holder'
22
25
import { ACCESS_MODE_READ , ACCESS_MODE_WRITE } from './internal/constants'
23
- import TransactionExecutor from './internal/transaction-executor'
24
- import Bookmark from './internal/bookmark'
25
- import TxConfig from './internal/tx-config'
26
+ import { TransactionExecutor } from './internal/transaction-executor'
27
+ import { Bookmark } from './internal/bookmark'
28
+ import { TxConfig } from './internal/tx-config'
29
+ import ConnectionProvider from './connection-provider'
30
+ import { Query } from './types'
31
+ import Connection from './connection'
32
+ import { NumberOrInteger } from './graph-types'
26
33
27
- const {
28
- util : { validateQueryAndParameters }
29
- } = internal
34
+ type ConnectionConsumer = ( connection : Connection | void ) => any | undefined
35
+ type TransactionWork < T > = ( tx : Transaction ) => Promise < T > | T
36
+
37
+ interface TransactionConfig {
38
+ timeout ?: NumberOrInteger
39
+ metadata ?: object
40
+ }
30
41
31
42
/**
32
43
* A Session instance is used for handling the connection and
@@ -36,6 +47,18 @@ const {
36
47
* @access public
37
48
*/
38
49
class Session {
50
+ private _mode : string
51
+ private _database : string
52
+ private _reactive : boolean
53
+ private _fetchSize : number
54
+ private _readConnectionHolder : ConnectionHolder
55
+ private _writeConnectionHolder : ConnectionHolder
56
+ private _open : boolean
57
+ private _hasTx : boolean
58
+ private _lastBookmark : Bookmark
59
+ private _transactionExecutor : TransactionExecutor
60
+ private _onComplete : ( meta : any ) => void
61
+
39
62
/**
40
63
* @constructor
41
64
* @protected
@@ -48,14 +71,22 @@ class Session {
48
71
* @param {boolean } args.reactive - Whether this session should create reactive streams
49
72
* @param {number } args.fetchSize - Defines how many records is pulled in each pulling batch
50
73
*/
51
- constructor ( {
74
+ constructor ( {
52
75
mode,
53
76
connectionProvider,
54
77
bookmark,
55
78
database,
56
79
config,
57
80
reactive,
58
81
fetchSize
82
+ } : {
83
+ mode : string
84
+ connectionProvider : ConnectionProvider
85
+ bookmark : Bookmark
86
+ database : string
87
+ config : any
88
+ reactive : boolean
89
+ fetchSize : number
59
90
} ) {
60
91
this . _mode = mode
61
92
this . _database = database
@@ -91,7 +122,11 @@ class Session {
91
122
* @param {TransactionConfig } [transactionConfig] - Configuration for the new auto-commit transaction.
92
123
* @return {Result } New Result.
93
124
*/
94
- run ( query , parameters , transactionConfig ) {
125
+ run (
126
+ query : Query ,
127
+ parameters ?: any ,
128
+ transactionConfig ?: TransactionConfig
129
+ ) : Result {
95
130
const { validatedQuery, params } = validateQueryAndParameters (
96
131
query ,
97
132
parameters
@@ -102,7 +137,7 @@ class Session {
102
137
103
138
return this . _run ( validatedQuery , params , connection => {
104
139
this . _assertSessionIsOpen ( )
105
- return connection . protocol ( ) . run ( validatedQuery , params , {
140
+ return ( connection as Connection ) . protocol ( ) . run ( validatedQuery , params , {
106
141
bookmark : this . _lastBookmark ,
107
142
txConfig : autoCommitTxConfig ,
108
143
mode : this . _mode ,
@@ -114,7 +149,11 @@ class Session {
114
149
} )
115
150
}
116
151
117
- _run ( query , parameters , customRunner ) {
152
+ _run (
153
+ query : Query ,
154
+ parameters : any ,
155
+ customRunner : ConnectionConsumer
156
+ ) : Result {
118
157
const connectionHolder = this . _connectionHolderWithMode ( this . _mode )
119
158
120
159
let observerPromise
@@ -143,7 +182,7 @@ class Session {
143
182
return new Result ( observerPromise , query , parameters , connectionHolder )
144
183
}
145
184
146
- async _acquireConnection ( connectionConsumer ) {
185
+ async _acquireConnection ( connectionConsumer : ConnectionConsumer ) {
147
186
let promise
148
187
const connectionHolder = this . _connectionHolderWithMode ( this . _mode )
149
188
if ( ! this . _open ) {
@@ -180,7 +219,7 @@ class Session {
180
219
* @param {TransactionConfig } [transactionConfig] - Configuration for the new auto-commit transaction.
181
220
* @returns {Transaction } New Transaction.
182
221
*/
183
- beginTransaction ( transactionConfig ) {
222
+ beginTransaction ( transactionConfig ?: TransactionConfig ) : Transaction {
184
223
// this function needs to support bookmarks parameter for backwards compatibility
185
224
// parameter was of type {string|string[]} and represented either a single or multiple bookmarks
186
225
// that's why we need to check parameter type and decide how to interpret the value
@@ -194,7 +233,7 @@ class Session {
194
233
return this . _beginTransaction ( this . _mode , txConfig )
195
234
}
196
235
197
- _beginTransaction ( accessMode , txConfig ) {
236
+ _beginTransaction ( accessMode : string , txConfig : TxConfig ) : Transaction {
198
237
if ( ! this . _open ) {
199
238
throw newError ( 'Cannot begin a transaction on a closed session.' )
200
239
}
@@ -222,13 +261,13 @@ class Session {
222
261
return tx
223
262
}
224
263
225
- _assertSessionIsOpen ( ) {
264
+ _assertSessionIsOpen ( ) {
226
265
if ( ! this . _open ) {
227
266
throw newError ( 'You cannot run more transactions on a closed session.' )
228
267
}
229
268
}
230
269
231
- _transactionClosed ( ) {
270
+ _transactionClosed ( ) {
232
271
this . _hasTx = false
233
272
}
234
273
@@ -237,7 +276,7 @@ class Session {
237
276
*
238
277
* @return {string[] } A reference to a previous transaction.
239
278
*/
240
- lastBookmark ( ) {
279
+ lastBookmark ( ) : string [ ] {
241
280
return this . _lastBookmark . values ( )
242
281
}
243
282
@@ -255,7 +294,10 @@ class Session {
255
294
* @return {Promise } Resolved promise as returned by the given function or rejected promise when given
256
295
* function or commit fails.
257
296
*/
258
- readTransaction ( transactionWork , transactionConfig ) {
297
+ readTransaction < T > (
298
+ transactionWork : TransactionWork < T > ,
299
+ transactionConfig ?: TransactionConfig
300
+ ) : Promise < T > {
259
301
const config = new TxConfig ( transactionConfig )
260
302
return this . _runTransaction ( ACCESS_MODE_READ , config , transactionWork )
261
303
}
@@ -274,12 +316,19 @@ class Session {
274
316
* @return {Promise } Resolved promise as returned by the given function or rejected promise when given
275
317
* function or commit fails.
276
318
*/
277
- writeTransaction ( transactionWork , transactionConfig ) {
319
+ writeTransaction < T > (
320
+ transactionWork : TransactionWork < T > ,
321
+ transactionConfig ?: TransactionConfig
322
+ ) : Promise < T > {
278
323
const config = new TxConfig ( transactionConfig )
279
324
return this . _runTransaction ( ACCESS_MODE_WRITE , config , transactionWork )
280
325
}
281
326
282
- _runTransaction ( accessMode , transactionConfig , transactionWork ) {
327
+ _runTransaction < T > (
328
+ accessMode : string ,
329
+ transactionConfig : TxConfig ,
330
+ transactionWork : TransactionWork < T >
331
+ ) : Promise < T > {
283
332
return this . _transactionExecutor . execute (
284
333
( ) => this . _beginTransaction ( accessMode , transactionConfig ) ,
285
334
transactionWork
@@ -290,7 +339,7 @@ class Session {
290
339
* Update value of the last bookmark.
291
340
* @param {Bookmark } newBookmark - The new bookmark.
292
341
*/
293
- _updateBookmark ( newBookmark ) {
342
+ _updateBookmark ( newBookmark ?: Bookmark ) {
294
343
if ( newBookmark && ! newBookmark . isEmpty ( ) ) {
295
344
this . _lastBookmark = newBookmark
296
345
}
@@ -300,7 +349,7 @@ class Session {
300
349
* Close this session.
301
350
* @return {Promise }
302
351
*/
303
- async close ( ) {
352
+ async close ( ) : Promise < void > {
304
353
if ( this . _open ) {
305
354
this . _open = false
306
355
this . _transactionExecutor . close ( )
@@ -310,7 +359,7 @@ class Session {
310
359
}
311
360
}
312
361
313
- _connectionHolderWithMode ( mode ) {
362
+ _connectionHolderWithMode ( mode : string ) : ConnectionHolder {
314
363
if ( mode === ACCESS_MODE_READ ) {
315
364
return this . _readConnectionHolder
316
365
} else if ( mode === ACCESS_MODE_WRITE ) {
@@ -320,14 +369,14 @@ class Session {
320
369
}
321
370
}
322
371
323
- _onCompleteCallback ( meta ) {
372
+ _onCompleteCallback ( meta : any ) {
324
373
this . _updateBookmark ( new Bookmark ( meta . bookmark ) )
325
374
}
326
375
327
376
/**
328
377
* @protected
329
378
*/
330
- static _validateSessionMode ( rawMode ) {
379
+ static _validateSessionMode ( rawMode ?: string ) : string {
331
380
const mode = rawMode || ACCESS_MODE_WRITE
332
381
if ( mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE ) {
333
382
throw newError ( 'Illegal session mode ' + mode )
@@ -336,7 +385,7 @@ class Session {
336
385
}
337
386
}
338
387
339
- function _createTransactionExecutor ( config ) {
388
+ function _createTransactionExecutor ( config : any ) : TransactionExecutor {
340
389
const maxRetryTimeMs =
341
390
config && config . maxTransactionRetryTime
342
391
? config . maxTransactionRetryTime
@@ -345,3 +394,4 @@ function _createTransactionExecutor (config) {
345
394
}
346
395
347
396
export default Session
397
+ export { TransactionConfig }
0 commit comments