neo4j-driver 4.4.0-alpha01 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,565 @@
1
+ # Neo4j Driver for JavaScript
2
+
3
+ This is the official Neo4j driver for JavaScript.
4
+
5
+ Resources to get you started:
6
+
7
+ - [API Documentation](https://neo4j.com/docs/api/javascript-driver/current/)
8
+ - [Neo4j Manual](https://neo4j.com/docs/)
9
+ - [Neo4j Refcard](https://neo4j.com/docs/cypher-refcard/current/)
10
+
11
+ ## What's New in 4.4
12
+
13
+ - [Changelog](https://github.com/neo4j/neo4j-javascript-driver/wiki/4.4-changelog)
14
+
15
+ ## Including the Driver
16
+
17
+ ### In Node.js application
18
+
19
+ Stable channel:
20
+
21
+ ```shell
22
+ npm install neo4j-driver
23
+ ```
24
+
25
+ Pre-release channel:
26
+
27
+ ```shell
28
+ npm install neo4j-driver@next
29
+ ```
30
+
31
+ Please note that `@next` only points to pre-releases that are not suitable for production use.
32
+ To get the latest stable release omit `@next` part altogether or use `@latest` instead.
33
+
34
+ ```javascript
35
+ var neo4j = require('neo4j-driver')
36
+ ```
37
+
38
+ Driver instance should be closed when Node.js application exits:
39
+
40
+ ```javascript
41
+ driver.close() // returns a Promise
42
+ ```
43
+
44
+ otherwise application shutdown might hang or it might exit with a non-zero exit code.
45
+
46
+ ### In web browser
47
+
48
+ We build a special browser version of the driver, which supports connecting to Neo4j over WebSockets.
49
+ It can be included in an HTML page using one of the following tags:
50
+
51
+ ```html
52
+ <!-- Direct reference -->
53
+ <script src="lib/browser/neo4j-web.min.js"></script>
54
+
55
+ <!-- unpkg CDN non-minified -->
56
+ <script src="https://unpkg.com/neo4j-driver"></script>
57
+ <!-- unpkg CDN minified for production use, version X.Y.Z -->
58
+ <script src="https://unpkg.com/neo4j-driver@X.Y.Z/lib/browser/neo4j-web.min.js"></script>
59
+
60
+ <!-- jsDelivr CDN non-minified -->
61
+ <script src="https://cdn.jsdelivr.net/npm/neo4j-driver"></script>
62
+ <!-- jsDelivr CDN minified for production use, version X.Y.Z -->
63
+ <script src="https://cdn.jsdelivr.net/npm/neo4j-driver@X.Y.Z/lib/browser/neo4j-web.min.js"></script>
64
+ ```
65
+
66
+ This will make a global `neo4j` object available, where you can create a driver instance with `neo4j.driver`:
67
+
68
+ ```javascript
69
+ var driver = neo4j.driver(
70
+ 'neo4j://localhost',
71
+ neo4j.auth.basic('neo4j', 'password')
72
+ )
73
+ ```
74
+
75
+ It is not required to explicitly close the driver on a web page. Web browser should gracefully close all open
76
+ WebSockets when the page is unloaded. However, driver instance should be explicitly closed when it's lifetime
77
+ is not the same as the lifetime of the web page:
78
+
79
+ ```javascript
80
+ driver.close() // returns a Promise
81
+ ```
82
+
83
+ ## Usage examples
84
+
85
+ ### Constructing a Driver
86
+
87
+ ```javascript
88
+ // Create a driver instance, for the user `neo4j` with password `password`.
89
+ // It should be enough to have a single driver per database per application.
90
+ var driver = neo4j.driver(
91
+ 'neo4j://localhost',
92
+ neo4j.auth.basic('neo4j', 'password')
93
+ )
94
+
95
+ // Close the driver when application exits.
96
+ // This closes all used network connections.
97
+ await driver.close()
98
+ ```
99
+
100
+ ### Acquiring a Session
101
+
102
+ #### Regular Session
103
+
104
+ ```javascript
105
+ // Create a session to run Cypher statements in.
106
+ // Note: Always make sure to close sessions when you are done using them!
107
+ var session = driver.session()
108
+ ```
109
+
110
+ ##### with a Default Access Mode of `READ`
111
+
112
+ ```javascript
113
+ var session = driver.session({ defaultAccessMode: neo4j.session.READ })
114
+ ```
115
+
116
+ ##### with Bookmarks
117
+
118
+ ```javascript
119
+ var session = driver.session({
120
+ bookmarks: [bookmark1FromPreviousSession, bookmark2FromPreviousSession]
121
+ })
122
+ ```
123
+
124
+ ##### against a Database
125
+
126
+ ```javascript
127
+ var session = driver.session({
128
+ database: 'foo',
129
+ defaultAccessMode: neo4j.session.WRITE
130
+ })
131
+ ```
132
+
133
+ #### Reactive Session
134
+
135
+ ```javascript
136
+ // Create a reactive session to run Cypher statements in.
137
+ // Note: Always make sure to close sessions when you are done using them!
138
+ var rxSession = driver.rxSession()
139
+ ```
140
+
141
+ ##### with a Default Access Mode of `READ`
142
+
143
+ ```javascript
144
+ var rxSession = driver.rxSession({ defaultAccessMode: neo4j.session.READ })
145
+ ```
146
+
147
+ ##### with Bookmarks
148
+
149
+ ```javascript
150
+ var rxSession = driver.rxSession({
151
+ bookmarks: [bookmark1FromPreviousSession, bookmark2FromPreviousSession]
152
+ })
153
+ ```
154
+
155
+ ##### against a Database
156
+
157
+ ```javascript
158
+ var rxSession = driver.rxSession({
159
+ database: 'foo',
160
+ defaultAccessMode: neo4j.session.WRITE
161
+ })
162
+ ```
163
+
164
+ ### Executing Queries
165
+
166
+ #### Consuming Records with Streaming API
167
+
168
+ ```javascript
169
+ // Run a Cypher statement, reading the result in a streaming manner as records arrive:
170
+ session
171
+ .run('MERGE (alice:Person {name : $nameParam}) RETURN alice.name AS name', {
172
+ nameParam: 'Alice'
173
+ })
174
+ .subscribe({
175
+ onKeys: keys => {
176
+ console.log(keys)
177
+ },
178
+ onNext: record => {
179
+ console.log(record.get('name'))
180
+ },
181
+ onCompleted: () => {
182
+ session.close() // returns a Promise
183
+ },
184
+ onError: error => {
185
+ console.log(error)
186
+ }
187
+ })
188
+ ```
189
+
190
+ Subscriber API allows following combinations of `onKeys`, `onNext`, `onCompleted` and `onError` callback invocations:
191
+
192
+ - zero or one `onKeys`,
193
+ - zero or more `onNext` followed by `onCompleted` when operation was successful. `onError` will not be invoked in this case
194
+ - zero or more `onNext` followed by `onError` when operation failed. Callback `onError` might be invoked after couple `onNext` invocations because records are streamed lazily by the database. `onCompleted` will not be invoked in this case.
195
+
196
+ #### Consuming Records with Promise API
197
+
198
+ ```javascript
199
+ // the Promise way, where the complete result is collected before we act on it:
200
+ session
201
+ .run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
202
+ nameParam: 'James'
203
+ })
204
+ .then(result => {
205
+ result.records.forEach(record => {
206
+ console.log(record.get('name'))
207
+ })
208
+ })
209
+ .catch(error => {
210
+ console.log(error)
211
+ })
212
+ .then(() => session.close())
213
+ ```
214
+
215
+ #### Consuming Records with Reactive API
216
+
217
+ ```javascript
218
+ rxSession
219
+ .run('MERGE (james:Person {name: $nameParam}) RETURN james.name AS name', {
220
+ nameParam: 'Bob'
221
+ })
222
+ .records()
223
+ .pipe(
224
+ map(record => record.get('name')),
225
+ concat(rxSession.close())
226
+ )
227
+ .subscribe({
228
+ next: data => console.log(data),
229
+ complete: () => console.log('completed'),
230
+ error: err => console.log(err)
231
+ })
232
+ ```
233
+
234
+ ### Transaction functions
235
+
236
+ ```javascript
237
+ // Transaction functions provide a convenient API with minimal boilerplate and
238
+ // retries on network fluctuations and transient errors. Maximum retry time is
239
+ // configured on the driver level and is 30 seconds by default:
240
+ // Applies both to standard and reactive sessions.
241
+ neo4j.driver('neo4j://localhost', neo4j.auth.basic('neo4j', 'password'), {
242
+ maxTransactionRetryTime: 30000
243
+ })
244
+ ```
245
+
246
+ #### Reading with Async Session
247
+
248
+ ```javascript
249
+ // It is possible to execute read transactions that will benefit from automatic
250
+ // retries on both single instance ('bolt' URI scheme) and Causal Cluster
251
+ // ('neo4j' URI scheme) and will get automatic load balancing in cluster deployments
252
+ var readTxResultPromise = session.readTransaction(txc => {
253
+ // used transaction will be committed automatically, no need for explicit commit/rollback
254
+
255
+ var result = txc.run('MATCH (person:Person) RETURN person.name AS name')
256
+ // at this point it is possible to either return the result or process it and return the
257
+ // result of processing it is also possible to run more statements in the same transaction
258
+ return result
259
+ })
260
+
261
+ // returned Promise can be later consumed like this:
262
+ readTxResultPromise
263
+ .then(result => {
264
+ console.log(result.records)
265
+ })
266
+ .catch(error => {
267
+ console.log(error)
268
+ })
269
+ .then(() => session.close())
270
+ ```
271
+
272
+ #### Reading with Reactive Session
273
+
274
+ ```javascript
275
+ rxSession
276
+ .readTransaction(txc =>
277
+ txc
278
+ .run('MATCH (person:Person) RETURN person.name AS name')
279
+ .records()
280
+ .pipe(map(record => record.get('name')))
281
+ )
282
+ .subscribe({
283
+ next: data => console.log(data),
284
+ complete: () => console.log('completed'),
285
+ error: err => console.log(error)
286
+ })
287
+ ```
288
+
289
+ #### Writing with Async Session
290
+
291
+ ```javascript
292
+ // It is possible to execute write transactions that will benefit from automatic retries
293
+ // on both single instance ('bolt' URI scheme) and Causal Cluster ('neo4j' URI scheme)
294
+ var writeTxResultPromise = session.writeTransaction(async txc => {
295
+ // used transaction will be committed automatically, no need for explicit commit/rollback
296
+
297
+ var result = await txc.run(
298
+ "MERGE (alice:Person {name : 'Alice'}) RETURN alice.name AS name"
299
+ )
300
+ // at this point it is possible to either return the result or process it and return the
301
+ // result of processing it is also possible to run more statements in the same transaction
302
+ return result.records.map(record => record.get('name'))
303
+ })
304
+
305
+ // returned Promise can be later consumed like this:
306
+ writeTxResultPromise
307
+ .then(namesArray => {
308
+ console.log(namesArray)
309
+ })
310
+ .catch(error => {
311
+ console.log(error)
312
+ })
313
+ .then(() => session.close())
314
+ ```
315
+
316
+ #### Writing with Reactive Session
317
+
318
+ ```javascript
319
+ rxSession
320
+ .writeTransaction(txc =>
321
+ txc
322
+ .run("MERGE (alice:Person {name: 'James'}) RETURN alice.name AS name")
323
+ .records()
324
+ .pipe(map(record => record.get('name')))
325
+ )
326
+ .subscribe({
327
+ next: data => console.log(data),
328
+ complete: () => console.log('completed'),
329
+ error: error => console.log(error)
330
+ })
331
+ ```
332
+
333
+ ### Explicit Transactions
334
+
335
+ #### With Async Session
336
+
337
+ ```javascript
338
+ // run statement in a transaction
339
+ const txc = session.beginTransaction()
340
+ try {
341
+ const result1 = await txc.run(
342
+ 'MERGE (bob:Person {name: $nameParam}) RETURN bob.name AS name',
343
+ {
344
+ nameParam: 'Bob'
345
+ }
346
+ )
347
+ result1.records.forEach(r => console.log(r.get('name')))
348
+ console.log('First query completed')
349
+
350
+ const result2 = await txc.run(
351
+ 'MERGE (adam:Person {name: $nameParam}) RETURN adam.name AS name',
352
+ {
353
+ nameParam: 'Adam'
354
+ }
355
+ )
356
+ result2.records.forEach(r => console.log(r.get('name')))
357
+ console.log('Second query completed')
358
+
359
+ await txc.commit()
360
+ console.log('committed')
361
+ } catch (error) {
362
+ console.log(error)
363
+ await txc.rollback()
364
+ console.log('rolled back')
365
+ } finally {
366
+ await session.close()
367
+ }
368
+ ```
369
+
370
+ #### With Reactive Session
371
+
372
+ ```javascript
373
+ rxSession
374
+ .beginTransaction()
375
+ .pipe(
376
+ flatMap(txc =>
377
+ concat(
378
+ txc
379
+ .run(
380
+ 'MERGE (bob:Person {name: $nameParam}) RETURN bob.name AS name',
381
+ {
382
+ nameParam: 'Bob'
383
+ }
384
+ )
385
+ .records()
386
+ .pipe(map(r => r.get('name'))),
387
+ of('First query completed'),
388
+ txc
389
+ .run(
390
+ 'MERGE (adam:Person {name: $nameParam}) RETURN adam.name AS name',
391
+ {
392
+ nameParam: 'Adam'
393
+ }
394
+ )
395
+ .records()
396
+ .pipe(map(r => r.get('name'))),
397
+ of('Second query completed'),
398
+ txc.commit(),
399
+ of('committed')
400
+ ).pipe(catchError(err => txc.rollback().pipe(throwError(err))))
401
+ )
402
+ )
403
+ .subscribe({
404
+ next: data => console.log(data),
405
+ complete: () => console.log('completed'),
406
+ error: error => console.log(error)
407
+ })
408
+ ```
409
+
410
+ ### Numbers and the Integer type
411
+
412
+ The Neo4j type system uses 64-bit signed integer values. The range of values is between `-(2`<sup>`64`</sup>`- 1)` and `(2`<sup>`63`</sup>`- 1)`.
413
+
414
+ However, JavaScript can only safely represent integers between `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`.
415
+
416
+ In order to support the full Neo4j type system, the driver will not automatically convert to javascript integers.
417
+ Any time the driver receives an integer value from Neo4j, it will be represented with an internal integer type by the driver.
418
+
419
+ _**Any javascript number value passed as a parameter will be recognized as `Float` type.**_
420
+
421
+ #### Writing integers
422
+
423
+ Numbers written directly e.g. `session.run("CREATE (n:Node {age: $age})", {age: 22})` will be of type `Float` in Neo4j.
424
+
425
+ To write the `age` as an integer the `neo4j.int` method should be used:
426
+
427
+ ```javascript
428
+ var neo4j = require('neo4j-driver')
429
+
430
+ session.run('CREATE (n {age: $myIntParam})', { myIntParam: neo4j.int(22) })
431
+ ```
432
+
433
+ To write an integer value that are not within the range of `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`, use a string argument to `neo4j.int`:
434
+
435
+ ```javascript
436
+ session.run('CREATE (n {age: $myIntParam})', {
437
+ myIntParam: neo4j.int('9223372036854775807')
438
+ })
439
+ ```
440
+
441
+ #### Reading integers
442
+
443
+ In Neo4j, the type Integer can be larger what can be represented safely as an integer with JavaScript Number.
444
+
445
+ It is only safe to convert to a JavaScript Number if you know that the number will be in the range `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`.
446
+
447
+ In order to facilitate working with integers the driver include `neo4j.isInt`, `neo4j.integer.inSafeRange`, `neo4j.integer.toNumber`, and `neo4j.integer.toString`.
448
+
449
+ ```javascript
450
+ var smallInteger = neo4j.int(123)
451
+ if (neo4j.integer.inSafeRange(smallInteger)) {
452
+ var aNumber = smallInteger.toNumber()
453
+ }
454
+ ```
455
+
456
+ If you will be handling integers that is not within the JavaScript safe range of integers, you should convert the value to a string:
457
+
458
+ ```javascript
459
+ var largeInteger = neo4j.int('9223372036854775807')
460
+ if (!neo4j.integer.inSafeRange(largeInteger)) {
461
+ var integerAsString = largeInteger.toString()
462
+ }
463
+ ```
464
+
465
+ #### Enabling native numbers
466
+
467
+ Starting from 1.6 version of the driver it is possible to configure it to only return native numbers instead of custom `Integer` objects.
468
+ The configuration option affects all integers returned by the driver. **Enabling this option can result in a loss of precision and incorrect numeric
469
+ values being returned if the database contains integer numbers outside of the range** `[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]`.
470
+ To enable potentially lossy integer values use the driver's configuration object:
471
+
472
+ ```javascript
473
+ var driver = neo4j.driver(
474
+ 'neo4j://localhost',
475
+ neo4j.auth.basic('neo4j', 'password'),
476
+ { disableLosslessIntegers: true }
477
+ )
478
+ ```
479
+
480
+ ## Building
481
+
482
+ ```
483
+ npm ci
484
+ npm run build
485
+ ```
486
+
487
+ This produces browser-compatible standalone files under `lib/browser` and a Node.js module version under `lib/`.
488
+ See files under `examples/` on how to use.
489
+
490
+ ## Testing
491
+
492
+ Tests **require** latest [Testkit 4.3](https://github.com/neo4j-drivers/testkit/tree/4.3), Python3 and Docker.
493
+
494
+ Testkit is needed to be cloned and configured to run against the Javascript Lite Driver. Use the following steps to configure Testkit.
495
+
496
+ 1. Clone the Testkit repository
497
+
498
+ ```
499
+ git clone https://github.com/neo4j-drivers/testkit.git
500
+ ```
501
+
502
+ 2. Under the Testkit folder, install the requirements.
503
+
504
+ ```
505
+ pip3 install -r requirements.txt
506
+ ```
507
+
508
+ 3. Define some enviroment variables to configure Testkit
509
+
510
+ ```
511
+ export TEST_DRIVER_NAME=javascript
512
+ export TEST_DRIVER_REPO=<path for the root folder of driver repository>
513
+ unset TEST_DRIVER_LITE
514
+ ```
515
+
516
+ To run test against against some Neo4j version:
517
+
518
+ ```
519
+ python3 main.py
520
+ ```
521
+
522
+ More details about how to use Teskit could be found on [its repository](https://github.com/neo4j-drivers/testkit/tree/4.3)
523
+
524
+ ## Testing (Legacy)
525
+
526
+ Tests **require** latest [Boltkit](https://github.com/neo4j-contrib/boltkit) and [Firefox](https://www.mozilla.org/firefox/) to be installed in the system.
527
+
528
+ Boltkit is needed to start, stop and configure local test database. Boltkit can be installed with the following command:
529
+
530
+ ```
531
+ pip3 install --upgrade boltkit
532
+ ```
533
+
534
+ To run tests against "default" Neo4j version:
535
+
536
+ ```
537
+ ./runTests.sh
538
+ ```
539
+
540
+ To run tests against specified Neo4j version:
541
+
542
+ ```
543
+ ./runTests.sh '-e 4.2.0'
544
+ ```
545
+
546
+ Simple `npm test` can also be used if you already have a running version of a compatible Neo4j server.
547
+
548
+ For development, you can have the build tool rerun the tests each time you change
549
+ the source code:
550
+
551
+ ```
552
+ gulp watch-n-test
553
+ ```
554
+
555
+ If the `gulp` command line tool is not available, you might need to install this globally:
556
+
557
+ ```
558
+ npm install -g gulp-cli
559
+ ```
560
+
561
+ ### Testing on windows
562
+
563
+ To run the same test suite, run `.\runTest.ps1` instead in powershell with admin right.
564
+ The admin right is required to start/stop Neo4j properly as a system service.
565
+ While there is no need to grab admin right if you are running tests against an existing Neo4j server using `npm test`.