A database driver for Neo4j 3.0.0+.
Resources to get you started:
- Detailed docs Not available yet
- Neo4j Manual
- Neo4j Refcard
- Upcoming version is now named as
4.0.0
instead of2.0.0
to better align with server versions. - Reactive API (built on top of RxJS) is now available with 4.0 version server, which includes reactive protocol improvements.
- Session instances can now be acquired against a specific database against a multi-database server, which is available with 4.0 version server.
- A new
driver.verifyConnectivity()
method is introduced for connectivity verification purposes. - Driver default configuration for
encrypted
is nowfalse
(meaning that driver will only attempt clear text connections by default), and when encryption is explicitly enabled the default trust mode is TRUST_SYSTEM_CA_SIGNED_CERTIFICATES which relies upon underlying system's certificate trust settings.
- Driver API is moved from
neo4j.v1
toneo4j
namespace. driver#session()
method now makes use of object destructuring rather than positional arguments (see Acquiring a Session for examples).session#close()
anddriver#close()
both now returnPromise
s and no more accept callback function arguments.driver.onError
anddriver.onCompleted
callbacks are completely removed. Errors should be monitored on related code paths (i.e. throughPromise#catch
, etc.).bolt+routing
scheme is now renamed toneo4j
.neo4j
scheme is designed to work work with all possible 4.0 server deployments, butbolt
scheme is still available for explicit single instance connections.
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 = require('neo4j-driver')
Driver instance should be closed when Node.js application exits:
driver.close() // returns a Promise
otherwise application shutdown might hang or it might exit with a non-zero exit code.
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 -->
<script src="lib/browser/neo4j-web.min.js"></script>
<!-- unpkg CDN non-minified -->
<script src="https://unpkg.com/neo4j-driver"></script>
<!-- unpkg CDN minified for production use, version X.Y.Z -->
<script src="https://unpkg.com/[email protected]/lib/browser/neo4j-web.min.js"></script>
<!-- jsDelivr CDN non-minified -->
<script src="https://cdn.jsdelivr.net/npm/neo4j-driver"></script>
<!-- jsDelivr CDN minified for production use, version X.Y.Z -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/browser/neo4j-web.min.js"></script>
This will make a global neo4j
object available, where you can access the driver API at neo4j
*:
var driver = neo4j.driver(
'neo4j://localhost',
neo4j.auth.basic('neo4j', '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.close() // returns a Promise
// Create a driver instance, for the user neo4j with password neo4j.
// It should be enough to have a single driver per database per application.
var driver = neo4j.driver(
'neo4j://localhost',
neo4j.auth.basic('neo4j', 'neo4j')
)
// Close the driver when application exits.
// This closes all used network connections.
await driver.close()
// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session()
var session = driver.session({ defaultAccessMode: neo4j.session.READ })
var session = driver.session({
bookmarks: [bookmark1FromPreviousSession, bookmark2FromPreviousSession]
})
var session = driver.session({
database: 'foo',
defaultAccessMode: neo4j.session.WRITE
})
// 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.rxSession()
var rxSession = driver.rxSession({ defaultAccessMode: neo4j.session.READ })
var rxSession = driver.rxSession({
bookmarks: [bookmark1FromPreviousSession, bookmark2FromPreviousSession]
})
var rxSession = driver.rxSession({
database: 'foo',
defaultAccessMode: neo4j.session.WRITE
})
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
.run('MERGE (alice:Person {name : $nameParam}) RETURN alice.name AS name', {
nameParam: 'Alice'
})
.subscribe({
onKeys: keys => {
console.log(keys)
},
onNext: record => {
console.log(record.get('name'))
},
onCompleted: () => {
session.close() // returns a Promise
},
onError: error => {
console.log(error)
}
})
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.
// the Promise way, where the complete result is collected before we act on it:
session
.run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
nameParam: 'James'
})
.then(result => {
result.records.forEach(record => {
console.log(record.get('name'))
})
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
rxSession
.run('MERGE (james:Person {name: $nameParam}) RETURN james.name AS name', {
nameParam: 'Bob'
})
.records()
.pipe(
map(record => record.get('name')),
concat(rxSession.close())
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: err => console.log(err)
})
// 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.driver('neo4j://localhost', neo4j.auth.basic('neo4j', 'neo4j'), {
maxTransactionRetryTime: 30000
})
// 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 deployments
var readTxResultPromise = session.readTransaction(txc => {
// used transaction will be committed automatically, no need for explicit commit/rollback
var result = txc.run('MATCH (person:Person) RETURN person.name AS name')
// at this point it is possible to either return the result or process it and return the
// result of processing it is also possible to run more statements in the same transaction
return result
})
// returned Promise can be later consumed like this:
readTxResultPromise
.then(result => {
console.log(result.records)
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
rxSession
.readTransaction(txc =>
txc
.run('MATCH (person:Person) RETURN person.name AS name')
.records()
.pipe(map(record => record.get('name')))
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: err => console.log(error)
})
// 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.writeTransaction(async txc => {
// used transaction will be committed automatically, no need for explicit commit/rollback
var result = await txc.run(
"MERGE (alice:Person {name : 'Alice' }) RETURN alice.name AS name"
)
// at this point it is possible to either return the result or process it and return the
// result of processing it is also possible to run more statements in the same transaction
return result.records.map(record => record.get('name'))
})
// returned Promise can be later consumed like this:
writeTxResultPromise
.then(namesArray => {
console.log(namesArray)
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
rxSession
.writeTransaction(txc =>
txc
.run("MERGE (alice:Person {name: 'James'}) RETURN alice.name AS name")
.records()
.pipe(map(record => record.get('name')))
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: error => console.log(error)
})
// run statement in a transaction
const txc = session.beginTransaction()
try {
const result1 = await txc.run(
'MERGE (bob:Person {name: $nameParam}) RETURN bob.name AS name',
{
nameParam: 'Bob'
}
)
result1.records.forEach(r => console.log(r.get('name')))
console.log('First query completed')
const result2 = await txc.run(
'MERGE (adam:Person {name: $nameParam}) RETURN adam.name AS name',
{
nameParam: 'Adam'
}
)
result2.records.forEach(r => console.log(r.get('name')))
console.log('Second query completed')
await txc.commit()
console.log('committed')
} catch (error) {
console.log(error)
await txc.rollback()
console.log('rolled back')
} finally {
await session.close()
}
rxSession
.beginTransaction()
.pipe(
flatMap(txc =>
concat(
txc
.run(
'MERGE (bob:Person {name: $nameParam}) RETURN bob.name AS name',
{
nameParam: 'Bob'
}
)
.records()
.pipe(map(r => r.get('name'))),
of('First query completed'),
txc
.run(
'MERGE (adam:Person {name: $nameParam}) RETURN adam.name AS name',
{
nameParam: 'Adam'
}
)
.records()
.pipe(map(r => r.get('name'))),
of('Second query completed'),
txc.commit(),
of('committed')
).pipe(catchError(err => txc.rollback().pipe(throwError(err))))
)
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: error => console.log(error)
})
The Neo4j type system includes 64-bit integer values.
However, JavaScript can only safely represent integers between -(2
53
- 1)
and (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.
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 = require('neo4j-driver')
session.run('CREATE (n {age: {myIntParam}})', { myIntParam: neo4j.int(22) })
To write integers larger than can be represented as JavaScript numbers, use a string argument to neo4j.int
:
session.run('CREATE (n {age: {myIntParam}})', {
myIntParam: neo4j.int('9223372036854775807')
})
Since Integers can be larger than can be represented as JavaScript numbers, it is only safe to convert to JavaScript numbers if you know that they will not exceed (2
53
- 1)
in size.
In order to facilitate working with integers the driver include neo4j.isInt
, neo4j.integer.inSafeRange
, neo4j.integer.toNumber
, and neo4j.integer.toString
.
var aSmallInteger = neo4j.int(123)
if (neo4j.integer.inSafeRange(aSmallInteger)) {
var aNumber = aSmallInteger.toNumber()
}
If you will be handling integers larger than that, you should convert them to strings:
var aLargerInteger = neo4j.int('9223372036854775807')
if (!neo4j.integer.inSafeRange(aLargerInteger)) {
var integerAsString = aLargerInteger.toString()
}
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.driver(
'neo4j://localhost',
neo4j.auth.basic('neo4j', 'neo4j'),
{ disableLosslessIntegers: true }
)
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.
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 3.1.3'
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
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
.