Skip to content

Commit d1d8f21

Browse files
committed
Merge branch 'bolt-handshake'
Conflicts: src/v1/result-summary.js test/v1/session.test.js
2 parents efc8c27 + 86c53be commit d1d8f21

14 files changed

+269
-112
lines changed

README.md

+87-53
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,77 @@
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

55
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!
66

7+
Find detailed docs at [alpha.neohq.net](http://alpha.neohq.net/docs/javascript-driver/).
8+
79
## Include module in Node.js application
810

11+
```shell
12+
npm install neo4j-driver
13+
```
14+
915
```javascript
10-
var neo4j = require('neo4j').v1;
16+
var neo4j = require('neo4j-driver').v1;
1117
```
1218

1319
## 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.
1522

1623
```html
1724
<script src="lib/browser/neo4j-web.min.js"></script>
1825
```
1926

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`:
2128

2229
```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+
```
2832

29-
var params = {
30-
name_a: 'Alice',
31-
age_a: 33,
32-
name_b: 'Bob',
33-
age_b: 44
34-
};
33+
## Usage examples
3534

35+
```javascript
3636

37-
// Create a Session object to contain all Cypher activity.
37+
// Create a driver instance
3838
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!
3942
var session = driver.session();
4043

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({
4448
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);
5357
}
54-
});
58+
});
5559

5660
// 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);
6967
});
68+
69+
// Completed!
70+
session.close();
71+
})
72+
.catch(function(error) {
73+
console.log(error);
74+
});
7075
```
7176

7277
## Building
@@ -84,24 +89,53 @@ See files under `examples/` on how to use.
8489
This runs the test suite against a fresh download of Neo4j.
8590
Or `npm test` if you already have a running version of a compatible Neo4j server.
8691

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+
8797
### 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.
91101
While there is no need to grab admin right if you are running tests against an existing Neo4j server using `npm test`.
92102

93103
## 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.
97108

98109
### 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+
```
102124

103125
### 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.

gulpfile.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ var babelify = require('babelify');
3535
var babel = require('gulp-babel');
3636
var watch = require('gulp-watch');
3737
var batch = require('gulp-batch');
38+
var replace = require('gulp-replace');
39+
var decompress = require('gulp-decompress');
3840
var fs = require("fs");
3941
var runSequence = require('run-sequence');
4042
var path = require('path');
4143
var childProcess = require("child_process");
42-
var decompress = require('gulp-decompress');
44+
var minimist = require('minimist');
4345

4446
gulp.task('default', ["test"]);
4547

@@ -52,7 +54,7 @@ gulp.task('build-browser', function () {
5254
var browserOutput = 'lib/browser';
5355
// Our app bundler
5456
var appBundler = browserify({
55-
entries: ['src/neo4j.js'],
57+
entries: ['src/index.js'],
5658
cache: {},
5759
standalone: 'neo4j',
5860
packageCache: {}
@@ -154,14 +156,18 @@ gulp.task('run-browser-test', function(){
154156
});
155157

156158
gulp.task('watch', function () {
157-
watch('src/**/*.js', batch(function (events, done) {
158-
gulp.start('all', done);
159-
}));
159+
return watch('src/**/*.js', batch(function (events, done) {
160+
gulp.start('all', done);
161+
}));
162+
});
163+
164+
gulp.task('watch-n-test', ['test-nodejs'], function () {
165+
return gulp.watch(['src/**/*.js', "test/**/*.js"], ['test-nodejs'] );
160166
});
161167

162-
var neo4jLinuxUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-M01-NIGHTLY-unix.tar.gz';
163-
var neo4jWinUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-M01-NIGHTLY-windows.zip';
164-
var neo4jHome = './build/neo4j-enterprise-3.0.0-M01';
168+
var neo4jLinuxUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-M02-NIGHTLY-unix.tar.gz';
169+
var neo4jWinUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-M02-NIGHTLY-windows.zip';
170+
var neo4jHome = './build/neo4j-enterprise-3.0.0-M02';
165171
var isWin = /^win/.test(process.platform);
166172

167173
gulp.task('download-neo4j', function() {
@@ -196,10 +202,17 @@ var runPowershell = function( cmd ) {
196202
child.stdin.end(); //end input
197203
}
198204

199-
// gulp.task('start-neo4j', ['download-neo4j'], shell.task([
200-
// 'chmod +x ' + neo4jHome + '/bin/neo4j',
201-
// neo4jHome + '/bin/neo4j start',
202-
// ]));
205+
/** Set the project version, controls package.json and version.js */
206+
gulp.task('set', function() {
207+
// Get the --version arg from command line
208+
var version = minimist(process.argv.slice(2), { string: 'version' }).version;
209+
210+
// Change the version in relevant files
211+
return gulp.src(['package.json'], {base: "./"})
212+
.pipe(replace('0.0.0-dev', version))
213+
.pipe(gulp.dest('./'));
214+
215+
});
203216

204217
gulp.task('start-neo4j', ['download-neo4j'], function() {
205218
if(isWin) {

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "neo4j-driver",
3-
"version": "1.0.0-alpha.1",
3+
"version": "0.0.0-dev",
44
"description": "Connect to Neo4j 3.0.0 and up from JavaScript",
55
"author": "Neo Technology Inc.",
6-
"license": "Apache License 2.0",
6+
"license": "Apache-2.0",
77
"repository": {
88
"type": "git",
99
"url": "git://github.com/neo4j/neo4j-javascript-driver.git"
@@ -15,7 +15,7 @@
1515
"stop-neo4j": "gulp stop-neo4j",
1616
"docs": "node_modules/.bin/esdoc -c esdoc.json"
1717
},
18-
"main": "lib/neo4j.js",
18+
"main": "lib/index.js",
1919
"devDependencies": {
2020
"babel": "^5.8.23",
2121
"babelify": "^6.3.0",
@@ -27,20 +27,22 @@
2727
"gulp-batch": "^1.0.5",
2828
"gulp-concat": "^2.6.0",
2929
"gulp-cucumber": "^0.0.12",
30+
"gulp-decompress": "^1.2.0",
3031
"gulp-download": "^0.0.1",
3132
"gulp-if": "^1.2.5",
3233
"gulp-jasmine": "^2.1.0",
3334
"gulp-jasmine-browser": "^0.2.3",
35+
"gulp-replace": "^0.5.4",
3436
"gulp-shell": "^0.4.3",
3537
"gulp-uglify": "^1.4.2",
3638
"gulp-util": "^3.0.6",
3739
"gulp-watch": "^4.3.5",
3840
"jasmine-reporters": "^2.0.7",
41+
"minimist": "^1.2.0",
3942
"phantomjs2-ext": "^0.1.0",
4043
"run-sequence": "^1.1.4",
4144
"through2": "~2.0.0",
4245
"vinyl-buffer": "^1.0.0",
43-
"vinyl-source-stream": "^1.1.0",
44-
"gulp-decompress": "^1.2.0"
46+
"vinyl-source-stream": "^1.1.0"
4547
}
4648
}

src/neo4j.js renamed to src/index.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@
1717
* limitations under the License.
1818
*/
1919

20-
import {int, isInt} from './v1/integer';
21-
import Driver from './v1/driver';
22-
23-
let USER_AGENT = "neo4j-javascript/0.0";
20+
import * as v1 from './v1/index';
2421

2522
export default {
26-
v1: {
27-
driver: (url) => new Driver(url, USER_AGENT),
28-
int: int,
29-
isInt: isInt
30-
}
23+
v1: v1
3124
}

src/v1/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import {int, isInt} from './integer';
21+
import Driver from './driver';
22+
import {VERSION} from '../version';
23+
24+
let USER_AGENT = "neo4j-javascript/" + VERSION;
25+
26+
export default {
27+
driver: (url) => new Driver(url, USER_AGENT),
28+
int: int,
29+
isInt: isInt
30+
}

0 commit comments

Comments
 (0)