11# Neo4j Driver for Javascript
22
3- An alpha-level database driver for a new Neo4j remoting protocol .
3+ An alpha-level database driver for Neo4j 3.0.0+ .
44
55Note: This is in active development, the API is not stable. Please try it out and give us feedback, but expect things to break in the medium term!
66
77## Include module in Node.js application
88
9+ ``` shell
10+ npm install neo4j-driver
11+ ```
12+
913``` javascript
10- var neo4j = require (' neo4j' ). v1 ;
14+ var neo4j = require (' neo4j-driver/v1 ' ) ;
1115```
1216
1317## Include in web browser
14- A global object ` neo4j ` will be available.
18+
19+ We build a special browser version of the driver, which supports connecting to Neo4j over WebSockets.
1520
1621``` html
1722<script src =" lib/browser/neo4j-web.min.js" ></script >
1823```
1924
20- ## Usage examples (for both Node.js and browser environments)
25+ This will make a global ` neo4j ` object available, where you can access the ` v1 ` API at ` neo4j.v1 ` :
2126
2227``` javascript
23- var statement = [' MERGE (alice:Person {name:{name_a},age:{age_a}})' ,
24- ' MERGE (bob:Person {name:{name_b},age:{age_b}})' ,
25- ' CREATE UNIQUE (alice)-[alice_knows_bob:KNOWS]->(bob)' ,
26- ' RETURN alice, bob, alice_knows_bob'
27- ];
28+ var driver = neo4j .v1 .driver (" bolt://localhost" );
29+ ```
2830
29- var params = {
30- name_a: ' Alice' ,
31- age_a: 33 ,
32- name_b: ' Bob' ,
33- age_b: 44
34- };
31+ ## Usage examples
3532
33+ ``` javascript
3634
37- // Create a Session object to contain all Cypher activity.
35+ // Create a driver instance
3836var driver = neo4j .driver (" bolt://localhost" );
37+
38+ // Create a session to run Cypher statements in.
39+ // Note: Always make sure to close sessions when you are done using them!
3940var session = driver .session ();
4041
41- // Run a Cypher statement within an implicit transaction
42- // The streaming way:
43- session .run (statement .join (' ' ), params).subscribe ({
42+ // Run a Cypher statement, reading the result in a streaming manner as records arrive:
43+ session
44+ .run (" MATCH (alice {name : {nameParam} }) RETURN alice.age" , { nameParam: ' Alice' })
45+ .subscribe ({
4446 onNext : function (record ) {
45- // On receipt of RECORD
46- for ( var i in record) {
47- console . log (i, ' : ' , record[i]);
48- }
49- }, onCompleted : function () {
50- session . close ();
51- }, onError : function (error ) {
52- console .log (error);
47+ console . log (record);
48+ },
49+ onCompleted : function () {
50+ // Completed!
51+ session . close ();
52+ },
53+ onError : function (error ) {
54+ console .log (error);
5355 }
54- });
56+ });
5557
5658// or
57- // the Promise way, where the complete response is collected:
58- session .run (statement .join (' ' ), params)
59- .then (function (records ){
60- records .forEach (function (record ) {
61- for (var i in record) {
62- console .log (i, ' : ' , record[i]);
63- }
64- });
65- session .close ();
66- })
67- .catch (function (error ) {
68- console .log (error);
59+ // the Promise way, where the complete result is collected before we act on it:
60+ session
61+ .run (" MATCH (alice {name : {nameParam} }) RETURN alice.age" , { nameParam: ' Alice' })
62+ .then (function (records ){
63+ records .forEach (function (record ) {
64+ console .log (record);
6965 });
66+
67+ // Completed!
68+ session .close ();
69+ })
70+ .catch (function (error ) {
71+ console .log (error);
72+ });
7073```
7174
7275## Building
@@ -85,23 +88,47 @@ This runs the test suite against a fresh download of Neo4j.
8588Or ` npm test ` if you already have a running version of a compatible Neo4j server.
8689
8790### Testing on windows
88- Running tests on windows requires PhantomJS installed and its bin folder added in windows system variable ` Path ` .
89- To run the same test suite, run ` .\runTest.ps1 ` instead in powershell with admin right.
90- The admin right is required to start/stop Neo4j properly as a system service.
91+ Running tests on windows requires PhantomJS installed and its bin folder added in windows system variable ` Path ` .
92+ To run the same test suite, run ` .\runTest.ps1 ` instead in powershell with admin right.
93+ The admin right is required to start/stop Neo4j properly as a system service.
9194While there is no need to grab admin right if you are running tests against an existing Neo4j server using ` npm test ` .
9295
9396## A note on numbers and the Integer type
94- For this driver to fully map to the Neo4j type system handling of 64-bits Integers is needed.
95- Javascript can saftely represent numbers between ` -(2 ` <sup >` 53 ` </sup >` - 1) ` and ` (2 ` <sup >` 53 ` </sup >` - 1) ` .
96- Therefore, an Integer type is introduced.
97+ The Neo4j type system includes 64-bit integer values.
98+ However, Javascript can only safely represent integers between ` -(2 ` <sup >` 53 ` </sup >` - 1) ` and ` (2 ` <sup >` 53 ` </sup >` - 1) ` .
99+ In order to support the full Neo4j type system, the driver includes an explicit Integer types.
100+ Any time the driver recieves an Integer value from Neo4j, it will be represented with the Integer type by the driver.
97101
98102### Write integers
99- Number written directly e.g. ` session.run("CREATE (n:Node {age: {age}})", {age: 22}) ` will be of type ` Float ` in Neo4j.
100- To write the ` age ` as an integer the ` neo4j.int ` method should be used.
101- E.g. ` session.run("CREATE (n:Node {age: {age}})", {age: neo4j.int(22)}) ` .
103+ Number written directly e.g. ` session.run("CREATE (n:Node {age: {age}})", {age: 22}) ` will be of type ` Float ` in Neo4j.
104+ To write the ` age ` as an integer the ` neo4j.int ` method should be used:
105+
106+ ``` javascript
107+ var neo4j = require (' neo4j-driver' );
108+
109+ session .run (" CREATE (n {age: {myIntParam}})" , {myIntParam: neo4j .int (22 )});
110+ ```
111+
112+ To write integers larger than can be represented as JavaScript numbers, use a string argument to ` neo4j.int ` :
113+
114+ ``` javascript
115+ session .run (" CREATE (n {age: {myIntParam}})" , {myIntParam: neo4j .int (" 9223372036854775807" )});
116+ ```
102117
103118### Read integers
104- To get the value of a from Neo4j received integer, the safeast way would be to use the ` .toString() ` method on
105- an Integer object.
106- E.g. ` console.log(result.age.toString()) ` .
107- To check if a variable is of the Integer type, the method ` neo4j.isInt(variable) ` can be used.
119+ Since Integers can be larger than can be represented as JavaScript numbers, it is only safe to convert Integer instances to JavaScript numbers if you know that they will not exceed ` (2 ` <sup >` 53 ` </sup >` - 1) ` in size:
120+
121+ ``` javascript
122+ var aSmallInteger = neo4j .int (123 );
123+ var aNumber = aSmallInteger .toNumber ();
124+ ```
125+
126+ If you will be handling integers larger than that, you can use the Integer instances directly, or convert them to strings:
127+
128+ ``` javascript
129+ var aLargerInteger = neo4j .int (" 9223372036854775807" );
130+ var integerAsString = aLargerInteger .toString ();
131+ ```
132+
133+ To help you work with Integers, the Integer class exposes a large set of arithmetic methods.
134+ Refer to the Integer API docs for details.
0 commit comments