- This is server-side only package, to retrieve data from the client use call(s) and methods
- This package uses batch operations to perform queries, than means if you sending multiple queries to Neo4j in current event loop, all of them will be sent in closest (next) event loop inside of the one batch
- This package was tested and works like a charm with GrapheneDB
- Please see demo hosted on Meteor (GrapheneDB) and on Heroku (GrapheneDB Add-on)
- To find more about how to use Cypher read Neo4j cheat sheet
See also Isomorphic Reactive Driver.
Install to meteor
meteor add ostrio:neo4jdriver
Demo Apps
API
Neo4jDB([url], [auth])
url
{String} - Absolute URL to Neo4j server, support bothhttp://
andhttps://
protocolsauth
{Object} - User credentialsauth.password
{String}auth.username
{String}
Create Neo4jDB
instance and connect to Neo4j
1db = new Neo4jDB 'http://localhost:7474' 2, 3 username: 'neo4j' 4 password: '1234'
db.propertyKeys()
List all property keys ever used in the database. Read more.
Returns an array of strings
db.labels()
List all labels ever used in the database. Read more.
Returns an array of strings
db.relationshipTypes()
List all relationship types ever used in the database. Read more.
Returns an array of strings
db.version()
Return version of Neo4j server driver connected to.
Returns string, like 2.2.5
db.query(cypher, [opts], [callback])
Send query to Neo4j via transactional endpoint. This Transaction will be immediately committed. This transaction will be sent inside batch, so if you call multiple async queries, all of them will be sent in one batch in closest (next) event loop. Read more.
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameterscallback
{Function} - Callback witherror
andresult
arguments- Returns {Neo4jCursor}
If callback
is passed, the method runs asynchronously, instead of synchronously.
1db.query "CREATE (n {userData}) RETURN n", userData: username: 'John Black'
db.queryOne(cypher, [opts])
Returns first result received from Neo4j
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameters- Returns {Object}
1db.queryOne "CREATE (n {userData}) RETURN n", userData: username: 'John Black' 2# Returns node as Object: 3# { 4# n: { 5# id: 8421, 6# username: "John Black" 7# metadata: { 8# id: 8421, 9# labels": [] 10# } 11# } 12# }
db.querySync(cypher, [opts])
Runs alway synchronously
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameters- Returns {Neo4jCursor}
1cursor = db.querySync "CREATE (n {userData}) RETURN n", userData: username: 'John Black' 2console.log cursor.fetch() 3# Returns array of nodes: 4# [{ 5# n: { 6# id: 8421, 7# username: "John Black" 8# metadata: { 9# id: 8421, 10# labels": [] 11# } 12# } 13# }]
db.queryAsync(cypher, [opts], [callback])
Runs alway asynchronously, even if callback is not passed
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameterscallback
{Function} - Callback witherror
andresult
arguments- Returns {Neo4jCursor}
1cursor = db.querySync "CREATE (n {userData}) RETURN n", userData: username: 'John Black' 2console.log cursor.fetch() 3# Returns array of nodes: 4# [{ 5# n: { 6# id: 8421, 7# username: "John Black" 8# metadata: { 9# id: 8421, 10# labels": [] 11# } 12# } 13# }]
db.graph(cypher, [opts], [callback])
Send query via to Transactional endpoint and return results as graph representation. Read more.
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameterscallback
{Function} - Callback witherror
andresult
arguments- Returns {Neo4jCursor}
If callback
is passed, the method runs asynchronously, instead of synchronously.
1cursor = db.graph "MATCH n RETURN n" 2# Actually it is shortcut for: 3# db.query 4# query: "MATCH n RETURN n" 5# resultDataContents: ["graph"] 6console.log cursor.fetch() 7# Returns array of arrays nodes and relationships: 8# [{nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]}, 9# {nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]}, 10# {nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]}]
db.cypher(cypher, [opts], [callback])
Send query to Neo4j via cypher endpoint. Read more.
cypher
{String} - Cypher query stringopts
{Object} - JSON-able map of cypher query parameterscallback
{Function} - Callback witherror
andresult
arguments- Returns {Neo4jCursor}
1cursor = db.cypher "CREATE (n {userData}) RETURN n", userData: username: 'John Black' 2console.log cursor.fetch() 3# Returns array of nodes: 4# [{ 5# n: { 6# id: 8421, 7# username: "John Black" 8# metadata: { 9# id: 8421, 10# labels": [] 11# } 12# } 13# }]
db.batch(tasks, [callback], [reactive], [plain])
Sent tasks to batch endpoint, this method allows to work directly with Neo4j REST API. Read more.
tasks
{[Object]} - Array of taskstasks.$.method
{String} - HTTP(S) method used sending this task, one of: 'POST', 'GET', 'PUT', 'DELETE', 'HEAD'tasks.$.to
{String} - Endpoint (URL) for tasktasks.$.id
{Number} - [Optional] Unique id to identify task. Should be always unique!tasks.$.body
{Object} - [Optional] JSONable object which will be sent as data to taskcallback
{Function} - callback function, if presentbatch()
method will be called asynchronouslyreactive
{Boolean} - iftrue
and ifplain
is true data of node(s) will be updated before returningplain
{Boolean} - iftrue
, results will be returned as simple objects instead ofNeo4jCursor
- Returns array of {[Neo4jCursor]}s or array of Object id
plain
istrue
1batch = db.batch [ 2 method: "POST" 3 to: '/cypher' 4 body: 5 query: "CREATE (n:MyNode {data})" 6 params: data: foo: 'bar' 7, 8 method: "POST" 9 to: '/cypher' 10 body: query: "MATCH (n:MyNode) RETURN n" 11 id: 999 12, 13 method: "POST" 14 to: '/cypher' 15 body: query: "MATCH (n:MyNode) DELETE n"] 16 17for cursor in batch 18 if res._batchId is 999 19 cursor.fetch()
Basic Usage
1db = new Neo4jDB 'http://localhost:7474', { 2 username: 'neo4j' 3 password: '1234' 4 } 5 6cursor = db.query 'CREATE (n:City {props}) RETURN n', 7 props: 8 title: 'Ottawa' 9 lat: 45.416667 10 long: -75.683333 11 12console.log cursor.fetch() 13# Returns array of nodes: 14# [{ 15# n: { 16# long: -75.683333, 17# lat: 45.416667, 18# title: "Ottawa", 19# id: 8421, 20# labels": ["City"], 21# metadata: { 22# id: 8421, 23# labels": ["City"] 24# } 25# } 26# }] 27 28# Iterate through results as plain objects: 29cursor.forEach (node) -> 30 console.log node 31 # Returns node as Object: 32 # { 33 # n: { 34 # long: -75.683333, 35 # lat: 45.416667, 36 # title: "Ottawa", 37 # id: 8421, 38 # labels": ["City"], 39 # metadata: { 40 # id: 8421, 41 # labels": ["City"] 42 # } 43 # } 44 # } 45 46# Iterate through cursor as `Neo4jNode` instances: 47cursor.each (node) -> 48 console.log node.n.get() 49 # { 50 # long: -75.683333, 51 # lat: 45.416667, 52 # title: "Ottawa", 53 # id: 8421, 54 # labels": ["City"], 55 # metadata: { 56 # id: 8421, 57 # labels": ["City"] 58 # } 59 # }
Testing & Dev usage
Local usage
To use the ostrio-neo4jdriver in a project and benefit from updates to the driver as they are released, you can keep your project and the driver in separate directories, and create a symlink between them.
# Stop meteor if it is running $ cd /directory/of/your/project # If you don't have a Meteor project yet, create a new one: $ meteor create MyProject $ cd MyProject # Create `packages` directory inside project's dir $ mkdir packages $ cd packages # Clone this repository to a local `packages` directory $ git clone --bare https://github.com/VeliovGroup/ostrio-neo4jdriver.git # If you need dev branch, switch into it $ git checkout dev # Go back into project's directory $ cd ../ $ meteor add ostrio:neo4jdriver # Do not forget to run Neo4j database, before start work with package
From now any changes in ostrio:neo4jdriver package folder will cause your project app to rebuild.
To run tests:
# Go to local package folder $ cd packages/ostrio-neo4jdriver # Edit first line of `tests.coffee` to set connection to your Neo4j database # Do not forget to run Neo4j database $ meteor test-packages ./