Neo4j adapter is a simple javascript library which provides easy and intuitive API to interact with Neo4j graph db. It is available in client side javascript and nodejs flavors.
To use it on client side:
- Simply include the javascript library, neo4jadapter.js, in your html page.
<script src="neo4jadapter.js" type="text/javascript"></script>
To use it in your node project:
- Copy the neo4j folder in node_modules folder, and include neo4j
var neo4j = require('neo4j').neo4j
var gurl = 'http://localhost:7474';
neo4j.node(1).adapter().load(gurl).done(function(node){
// access node here
});
var gurl = 'http://localhost:7474';
neo4j.node(1, {del: true}).adapter().load(gurl).done(function(node){
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels().adapter().load(gurl).done(function(ret){
// access all node relationships
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels(100).adapter().load(gurl).done(function(ret){
// access all node relationship with id 100
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels("KNOWS").adapter().load(gurl).done(function(ret){
// access node relationships of type "KNOWS"
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels("KNOWS", { dir: "in" }).adapter().load(gurl).done(function(ret){
// access node relationships of type "KNOWS" as incoming relationship
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels("KNOWS", { dir: "out" }).adapter().load(gurl).done(function(ret){
// access node relationships of type "KNOWS" as outgoing relationship
});
var gurl = 'http://localhost:7474';
neo4j.node(1).rels("KNOWS", { dir: 'all', del: true }).adapter().load(gurl).done(function(ret){
});
var gurl = 'http://localhost:7474';
neo4j.rels("KNOWS", { dir: 'all' }).adapter().load(gurl).done(function(ret){
});
var gurl = 'http://localhost:7474';
neo4j.node(1).props().adapter().load(gurl).done(function(ret){
// access all node properties
});
neo4j.node(1).props("foo").adapter().load(gurl).done(function(ret){
// access node property with name 'foo'
});
neo4j.node(1).props("foo", "bar").adapter().load(gurl).done(function(ret){
// updates node property "foo" with "bar"
});
var gurl = 'http://localhost:7474';
neo4j.node(1).props("foo", {del: true}).adapter().load(gurl).done(function(ret){
// delete node property of type "foo"
});
neo4j.node(1).props({ foo: "bar", name: "foo-bar" }).adapter().load(gurl).done(function(ret){
// set node properties
});
var gurl = 'http://localhost:7474';
neo4j.rel(1).props().adapter().load(gurl).done(function(ret){
// access all rel properties
});
neo4j.rel(1).props("foo").adapter().load(gurl).done(function(ret){
// access rel property with name 'foo'
});
var gurl = 'http://localhost:7474';
neo4j.node(1).label("user").adapter().load(gurl).done(function(ret){
// add label "user" to node
});
var gurl = 'http://localhost:7474';
neo4j.query("start n=node(1) return n.id").adapter().load(gurl).done(function(ret){
// return id of node 1
});
var gurl = 'http://localhost:7474';
neo4j.query("start n=node(%s) match n-[:KNOWS]->r where r.country = '%s' return r.name where n.name").adapter(1, 'USA').load(gurl).done(function(ret){
// return all person whome node 1 knows and stays in USA
});
Batch queries are used to combine set of operation and hit once, which saves time and are faster. To create batch using adapter is simple. Create batch as:
var batch = neo4j.batch()
and then keeps on adding actions to batch, which will hit sequentially. For example to set two properties on a node 1:
var gurl = 'http://localhost:7474';
neo4j.batch()
.add("props", "node", 1, "foo", "bar")
.add("props", "node", 1, "blah", "blah")
.adapter().load(gurl).done(function(ret){
// set two properties on node 1
});
To refer an item perviously in batch:
var gurl = 'http://localhost:7474';
neo4j.batch()
.add("node", { "name": "John" })
.add("label", "{0}", "person") // refers, to item at 0 index in batch by {0}
.add("node", { "name": "Marie" })
.add("label", "{2}", "person") // refers, to item at 2 index in batch by {2}
.add("rel", "LOVES", { "from": "{0}", "to": "{2}" })
.adapter().load(gurl).done(function(ret){
// creates John-[:LOVES]->Marie
});