Name: neo4j-javascript-driver
Owner: Neo4j
Description: Neo4j Bolt driver for JavaScript
Created: 2015-07-23 14:46:34.0
Updated: 2018-05-24 05:22:10.0
Pushed: 2018-05-24 18:01:39.0
Size: 24809
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A database driver for Neo4j 3.0.0+.
Resources to get you started:
Stable channel:
install neo4j-driver
Pre-release channel:
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.
neo4j = require('neo4j-driver').v1;
Driver instance should be closed when Node.js application exits:
er.close();
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 -->
ipt src="lib/browser/neo4j-web.min.js"></script>
unpkg CDN non-minified -->
ipt src="https://unpkg.com/neo4j-driver"></script>
unpkg CDN minified for production use, version X.Y.Z -->
ipt src="https://unpkg.com/neo4j-driver@X.Y.Z/lib/browser/neo4j-web.min.js"></script>
jsDelivr CDN non-minified -->
ipt src="https://cdn.jsdelivr.net/npm/neo4j-driver"></script>
jsDelivr CDN minified for production use, version X.Y.Z -->
ipt src="https://cdn.jsdelivr.net/npm/neo4j-driver@X.Y.Z/lib/browser/neo4j-web.min.js"></script>
This will make a global neo4j
object available, where you can access the v1
API at neo4j.v1
:
driver = neo4j.v1.driver("bolt://localhost", neo4j.v1.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:
er.close();
Driver lifecycle:
reate a driver instance, for the user neo4j with password neo4j.
t should be enough to have a single driver per database per application.
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
lose the driver when application exits.
his closes all used network connections.
er.close();
Session API:
reate a session to run Cypher statements in.
ote: Always make sure to close sessions when you are done using them!
session = driver.session();
un a Cypher statement, reading the result in a streaming manner as records arrive:
ion
un('MERGE (alice:Person {name : {nameParam} }) RETURN alice.name AS name', {nameParam: 'Alice'})
ubscribe({
onNext: function (record) {
console.log(record.get('name'));
},
onCompleted: function () {
session.close();
},
onError: function (error) {
console.log(error);
}
;
r
he Promise way, where the complete result is collected before we act on it:
ion
un('MERGE (james:Person {name : {nameParam} }) RETURN james.name AS name', {nameParam: 'James'})
hen(function (result) {
result.records.forEach(function (record) {
console.log(record.get('name'));
});
session.close();
atch(function (error) {
console.log(error);
;
Transaction functions API:
ransaction functions provide a convenient API with minimal boilerplate and
etries on network fluctuations and transient errors. Maximum retry time is
onfigured on the driver level and is 30 seconds by default:
j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {maxTransactionRetryTime: 30000});
t is possible to execute read transactions that will benefit from automatic
etries on both single instance ('bolt' URI scheme) and Causal Cluster
'bolt+routing' URI scheme) and will get automatic load balancing in cluster deployments
readTxResultPromise = session.readTransaction(function (transaction) {
used transaction will be committed automatically, no need for explicit commit/rollback
r result = transaction.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
turn result;
eturned Promise can be later consumed like this:
TxResultPromise.then(function (result) {
ssion.close();
nsole.log(result.records);
atch(function (error) {
nsole.log(error);
t is possible to execute write transactions that will benefit from automatic retries
n both single instance ('bolt' URI scheme) and Causal Cluster ('bolt+routing' URI scheme)
writeTxResultPromise = session.writeTransaction(function (transaction) {
used transaction will be committed automatically, no need for explicit commit/rollback
r result = transaction.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
turn result.records.map(function (record) {
return record.get('name');
;
eturned Promise can be later consumed like this:
eTxResultPromise.then(function (namesArray) {
ssion.close();
nsole.log(namesArray);
atch(function (error) {
nsole.log(error);
Explicit transactions API:
un statement in a transaction
tx = session.beginTransaction();
un("MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name", {nameParam: 'Bob'})
ubscribe({
onNext: function (record) {
console.log(record.get('name'));
},
onCompleted: function () {
console.log('First query completed');
},
onError: function (error) {
console.log(error);
}
;
un("MERGE (adam:Person {name : {nameParam} }) RETURN adam.name AS name", {nameParam: 'Adam'})
ubscribe({
onNext: function (record) {
console.log(record.get('name'));
},
onCompleted: function () {
console.log('Second query completed');
},
onError: function (error) {
console.log(error);
}
;
cide if the transaction should be committed or rolled back
success = false;
success) {
.commit()
.subscribe({
onCompleted: function () {
// this transaction is now committed and session can be closed
session.close();
},
onError: function (error) {
console.log(error);
}
});
se {
transaction is rolled black and nothing is created in the database
nsole.log('rolled back');
.rollback();
Subscriber API allows following combinations of onNext
, onCompleted
and onError
callback invocations:
onNext
followed by onCompleted
when operation was successful. onError
will not be invoked
in this caseonNext
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 casenpm 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 to be installed in the system. It is needed to start, stop and configure local test database. Boltkit can be installed with the following command:
pip 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
Running tests on windows requires PhantomJS installed and its bin folder added in windows system variable Path
.
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
.
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.
Number 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:
neo4j = require('neo4j-driver').v1;
ion.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
:
ion.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
.
aSmallInteger = neo4j.int(123);
neo4j.integer.inSafeRange(aSmallInteger)) {
var aNumber = aSmallInteger.toNumber();
If you will be handling integers larger than that, you should convert them to strings:
aLargerInteger = neo4j.int("9223372036854775807");
!neo4j.integer.inSafeRange(aLargerInteger)) {
var integerAsString = aLargerInteger.toString();