Neo4j Driver for JavaScript
This is the official Neo4j driver for JavaScript.
Resources to get you started:
What's New in 4.2
Including the Driver
In Node.js application
Stable channel:
npm install neo4j-driver
Pre-release channel:
npm install neo4j-driver@next
Please note that @next
only points to pre-releases that are not suitable for production use.
To get the latest stable release omit @next
part altogether or use @latest
instead.
var neo4j =
Driver instance should be closed when Node.js application exits:
driver // returns a Promise
otherwise application shutdown might hang or it might exit with a non-zero exit code.
In web browser
We build a special browser version of the driver, which supports connecting to Neo4j over WebSockets. It can be included in an HTML page using one of the following tags:
<!-- Direct reference --> <!-- unpkg CDN non-minified --><!-- unpkg CDN minified for production use, version X.Y.Z --> <!-- jsDelivr CDN non-minified --><!-- jsDelivr CDN minified for production use, version X.Y.Z -->
This will make a global neo4j
object available, where you can create a driver instance with neo4j.driver
:
var driver = neo4j
It is not required to explicitly close the driver on a web page. Web browser should gracefully close all open WebSockets when the page is unloaded. However, driver instance should be explicitly closed when it's lifetime is not the same as the lifetime of the web page:
driver // returns a Promise
Usage examples
Constructing a Driver
// Create a driver instance, for the user `neo4j` with password `password`.// It should be enough to have a single driver per database per application.var driver = neo4j // Close the driver when application exits.// This closes all used network connections.await driver
Acquiring a Session
Regular Session
// Create a session to run Cypher statements in.// Note: Always make sure to close sessions when you are done using them!var session = driver
READ
with a Default Access Mode of var session = driver
with Bookmarks
var session = driver
against a Database
var session = driver
Reactive Session
// Create a reactive session to run Cypher statements in.// Note: Always make sure to close sessions when you are done using them!var rxSession = driver
READ
with a Default Access Mode of var rxSession = driver
with Bookmarks
var rxSession = driver
against a Database
var rxSession = driver
Executing Queries
Consuming Records with Streaming API
// Run a Cypher statement, reading the result in a streaming manner as records arrive:session
Subscriber API allows following combinations of onKeys
, onNext
, onCompleted
and onError
callback invocations:
- zero or one
onKeys
, - zero or more
onNext
followed byonCompleted
when operation was successful.onError
will not be invoked in this case - zero or more
onNext
followed byonError
when operation failed. CallbackonError
might be invoked after coupleonNext
invocations because records are streamed lazily by the database.onCompleted
will not be invoked in this case.
Consuming Records with Promise API
// the Promise way, where the complete result is collected before we act on it:session
Consuming Records with Reactive API
rxSession
Transaction functions
// Transaction functions provide a convenient API with minimal boilerplate and// retries on network fluctuations and transient errors. Maximum retry time is// configured on the driver level and is 30 seconds by default:// Applies both to standard and reactive sessions.neo4j
Reading with Async Session
// It is possible to execute read transactions that will benefit from automatic// retries on both single instance ('bolt' URI scheme) and Causal Cluster// ('neo4j' URI scheme) and will get automatic load balancing in cluster deploymentsvar readTxResultPromise = session // returned Promise can be later consumed like this:readTxResultPromise
Reading with Reactive Session
rxSession
Writing with Async Session
// It is possible to execute write transactions that will benefit from automatic retries// on both single instance ('bolt' URI scheme) and Causal Cluster ('neo4j' URI scheme)var writeTxResultPromise = session // returned Promise can be later consumed like this:writeTxResultPromise
Writing with Reactive Session
rxSession
Explicit Transactions
With Async Session
// run statement in a transactionconst txc = sessiontry const result1 = await txc result1records console const result2 = await txc result2records console await txc console catch error console await txc console finally await session
With Reactive Session
rxSession
Numbers and the Integer type
The Neo4j type system uses 64-bit signed integer values. The range of values is between -(2
64
- 1)
and (2
63
- 1)
.
However, JavaScript can only safely represent integers between Number.MIN_SAFE_INTEGER
-(2
53
- 1)
and Number.MAX_SAFE_INTEGER
(2
53
- 1)
.
In order to support the full Neo4j type system, the driver will not automatically convert to javascript integers. Any time the driver receives an integer value from Neo4j, it will be represented with an internal integer type by the driver.
Any javascript number value passed as a parameter will be recognized as Float
type.
Writing integers
Numbers written directly e.g. session.run("CREATE (n:Node {age: $age})", {age: 22})
will be of type Float
in Neo4j.
To write the age
as an integer the neo4j.int
method should be used:
var neo4j = session
To write an integer value that are not within the range of Number.MIN_SAFE_INTEGER
-(2
53
- 1)
and Number.MAX_SAFE_INTEGER
(2
53
- 1)
, use a string argument to neo4j.int
:
session
Reading integers
In Neo4j, the type Integer can be larger what can be represented safely as an integer with JavaScript Number.
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
53
- 1)
and Number.MAX_SAFE_INTEGER
(2
53
- 1)
.
In order to facilitate working with integers the driver include neo4j.isInt
, neo4j.integer.inSafeRange
, neo4j.integer.toNumber
, and neo4j.integer.toString
.
var smallInteger = neo4jif neo4jinteger var aNumber = smallInteger
If you will be handling integers that is not within the JavaScript safe range of integers, you should convert the value to a string:
var largeInteger = neo4jif !neo4jinteger var integerAsString = largeInteger
Enabling native numbers
Starting from 1.6 version of the driver it is possible to configure it to only return native numbers instead of custom Integer
objects.
The configuration option affects all integers returned by the driver. Enabling this option can result in a loss of precision and incorrect numeric
values being returned if the database contains integer numbers outside of the range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]
.
To enable potentially lossy integer values use the driver's configuration object:
var driver = neo4j
Building
npm install
npm run build
This produces browser-compatible standalone files under lib/browser
and a Node.js module version under lib/
.
See files under examples/
on how to use.
Testing
Tests require latest Boltkit and Firefox to be installed in the system.
Boltkit is needed to start, stop and configure local test database. Boltkit can be installed with the following command:
pip3 install --upgrade boltkit
To run tests against "default" Neo4j version:
./runTests.sh
To run tests against specified Neo4j version:
./runTests.sh '-e 4.2.0'
Simple npm test
can also be used if you already have a running version of a compatible Neo4j server.
For development, you can have the build tool rerun the tests each time you change the source code:
gulp watch-n-test
If the gulp
command line tool is not available, you might need to install this globally:
npm install -g gulp-cli
Testing on windows
To run the same test suite, run .\runTest.ps1
instead in powershell with admin right.
The admin right is required to start/stop Neo4j properly as a system service.
While there is no need to grab admin right if you are running tests against an existing Neo4j server using npm test
.