1
1
# Neo4j Driver for Javascript
2
2
3
- An alpha-level database driver for a new Neo4j remoting protocol .
3
+ An alpha-level database driver for Neo4j 3.0.0+ .
4
4
5
5
Note: 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!
6
6
7
+ Find detailed docs at [ alpha.neohq.net] ( http://alpha.neohq.net/docs/javascript-driver/ ) .
8
+
7
9
## Include module in Node.js application
8
10
11
+ ``` shell
12
+ npm install neo4j-driver
13
+ ```
14
+
9
15
``` javascript
10
- var neo4j = require (' neo4j' ).v1 ;
16
+ var neo4j = require (' neo4j-driver ' ).v1 ;
11
17
```
12
18
13
19
## Include in web browser
14
- A global object ` neo4j ` will be available.
20
+
21
+ We build a special browser version of the driver, which supports connecting to Neo4j over WebSockets.
15
22
16
23
``` html
17
24
<script src =" lib/browser/neo4j-web.min.js" ></script >
18
25
```
19
26
20
- ## Usage examples (for both Node.js and browser environments)
27
+ This will make a global ` neo4j ` object available, where you can access the ` v1 ` API at ` neo4j.v1 ` :
21
28
22
29
``` 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
- ];
30
+ var driver = neo4j .v1 .driver (" bolt://localhost" );
31
+ ```
28
32
29
- var params = {
30
- name_a: ' Alice' ,
31
- age_a: 33 ,
32
- name_b: ' Bob' ,
33
- age_b: 44
34
- };
33
+ ## Usage examples
35
34
35
+ ``` javascript
36
36
37
- // Create a Session object to contain all Cypher activity.
37
+ // Create a driver instance
38
38
var driver = neo4j .driver (" bolt://localhost" );
39
+
40
+ // Create a session to run Cypher statements in.
41
+ // Note: Always make sure to close sessions when you are done using them!
39
42
var session = driver .session ();
40
43
41
- // Run a Cypher statement within an implicit transaction
42
- // The streaming way:
43
- session .run (statement .join (' ' ), params).subscribe ({
44
+ // Run a Cypher statement, reading the result in a streaming manner as records arrive:
45
+ session
46
+ .run (" MATCH (alice {name : {nameParam} }) RETURN alice.age" , { nameParam: ' Alice' })
47
+ .subscribe ({
44
48
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);
49
+ console . log (record);
50
+ },
51
+ onCompleted : function () {
52
+ // Completed!
53
+ session . close ();
54
+ },
55
+ onError : function (error ) {
56
+ console .log (error);
53
57
}
54
- });
58
+ });
55
59
56
60
// 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);
61
+ // the Promise way, where the complete result is collected before we act on it:
62
+ session
63
+ .run (" MATCH (alice {name : {nameParam} }) RETURN alice.age" , { nameParam: ' Alice' })
64
+ .then (function (records ){
65
+ records .forEach (function (record ) {
66
+ console .log (record);
69
67
});
68
+
69
+ // Completed!
70
+ session .close ();
71
+ })
72
+ .catch (function (error ) {
73
+ console .log (error);
74
+ });
70
75
```
71
76
72
77
## Building
@@ -84,24 +89,53 @@ See files under `examples/` on how to use.
84
89
This runs the test suite against a fresh download of Neo4j.
85
90
Or ` npm test ` if you already have a running version of a compatible Neo4j server.
86
91
92
+ For development, you can have the build tool rerun the tests each time you change
93
+ the source code:
94
+
95
+ gulp watch-n-test
96
+
87
97
### 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.
98
+ Running tests on windows requires PhantomJS installed and its bin folder added in windows system variable ` Path ` .
99
+ To run the same test suite, run ` .\runTest.ps1 ` instead in powershell with admin right.
100
+ The admin right is required to start/stop Neo4j properly as a system service.
91
101
While there is no need to grab admin right if you are running tests against an existing Neo4j server using ` npm test ` .
92
102
93
103
## 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.
104
+ The Neo4j type system includes 64-bit integer values.
105
+ However, Javascript can only safely represent integers between ` -(2 ` <sup >` 53 ` </sup >` - 1) ` and ` (2 ` <sup >` 53 ` </sup >` - 1) ` .
106
+ In order to support the full Neo4j type system, the driver includes an explicit Integer types.
107
+ Any time the driver recieves an Integer value from Neo4j, it will be represented with the Integer type by the driver.
97
108
98
109
### 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)}) ` .
110
+ Number written directly e.g. ` session.run("CREATE (n:Node {age: {age}})", {age: 22}) ` will be of type ` Float ` in Neo4j.
111
+ To write the ` age ` as an integer the ` neo4j.int ` method should be used:
112
+
113
+ ``` javascript
114
+ var neo4j = require (' neo4j-driver' ).v1 ;
115
+
116
+ session .run (" CREATE (n {age: {myIntParam}})" , {myIntParam: neo4j .int (22 )});
117
+ ```
118
+
119
+ To write integers larger than can be represented as JavaScript numbers, use a string argument to ` neo4j.int ` :
120
+
121
+ ``` javascript
122
+ session .run (" CREATE (n {age: {myIntParam}})" , {myIntParam: neo4j .int (" 9223372036854775807" )});
123
+ ```
102
124
103
125
### 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.
126
+ 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:
127
+
128
+ ``` javascript
129
+ var aSmallInteger = neo4j .int (123 );
130
+ var aNumber = aSmallInteger .toNumber ();
131
+ ```
132
+
133
+ If you will be handling integers larger than that, you can use the Integer instances directly, or convert them to strings:
134
+
135
+ ``` javascript
136
+ var aLargerInteger = neo4j .int (" 9223372036854775807" );
137
+ var integerAsString = aLargerInteger .toString ();
138
+ ```
139
+
140
+ To help you work with Integers, the Integer class exposes a large set of arithmetic methods.
141
+ Refer to the Integer API docs for details.
0 commit comments