Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Neo4j REST API wrapper for Node.js
JavaScript
branch: master

This branch is 5 commits ahead, 178 commits behind philippkueng:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
node_modules
test
Makefile
README.md
main.js
package.json

README.md

Neo4j REST API wrapper for Node.js


Installation

npm install node-neo4j

Usage

In order to use the library you either have to create a heroku app and add the Neo4j Addon there or install it locally.
If you're using OS X i highly recommend installing Neo4j via Homebrew.

$ brew install neo4j
$ neo4j start

Instantiate a wrapper instance

var neo4j = require('node-neo4j);
db = new neo4j('http://username:password@domain:port');

Node operations

Insert a Node

db.InsertNode({
    name: 'Darth Vader',
    sex: 'male'
},function(err, node){
    if(err) throw err;

    // Output node properties.
    console.log(node.data);

    // Output node id.
    console.log(node.id);
});

Read a Node

db.ReadNode(12, function(err, node){
    if(err) throw err;

    // Output node properties.
    console.log(node.data);

    // Output node id.
    console.log(node.id);
});

Update a Node

Will remove any assigned properties and replace them with the ones given below.

db.UpdateNode(12, {name:'foobar2'}, function(err, node){
    if(err) throw err;

    if(node === true){
        // node updated
    } else {
        // node not found, hence not updated
    }
});

Delete a Node

db.DeleteNode(12, function(err, node){
    if(err) throw err;

    if(node === true){
        // node deleted
    } else {
        // node not deleted because not found or because of existing relationships
    }
});

Relationship operations

Insert a Relationship

db.InsertRelationship(root_node_id, other_node_id, 'RELATIONSHIP_TYPE', {
    age: '5 years',
    sideeffects: {
        positive: 'happier',
        negative: 'less time'
    }}, function(err, relationship){
        if(err) throw err;

        // Output relationship properties.
        console.log(relationship.data);

        // Output relationship id.
        console.log(relationship.id);

        // Output relationship start_node_id.
        console.log(relationship.start_node_id);

        // Output relationship end_node_id.
        console.log(relationship.end_node_id);
});

Read a Relationship

db.ReadRelationship(relationship_id, function(err, relationship){
    if(err) throw err;

    // Same properties for relationship object as with InsertRelationship
});

Update a Relationship

Will remove any assigned properties and replace them with the ones given below.

db.UpdateRelationship(relationship_id, {
        age: '6 years'
    }, function(err, relationship){
        if(err) throw err;

        if(relationship === true){
            // relationship updated
        } else {
            relationship not found, hence not updated.
        }
});

Delete a Relationship

db.DeleteRelationship(relationship_id, function(err, relationship){
    if(err) throw err;

    if(relationship === true){
        // relationship deleted
    } else {
        // relationship not deleted because not found.
    }
});

Advanced relationship operations

Get all relationship types used within the Neo4j database

Will also return types of those relationships that have been deleted.

db.ReadRelationshipTypes(function(err, result){
    if(err) throw err;

    console.log(result); // eg. ['RELATED_TO', 'LOVES', 'KNOWNS']
});

Get all relationships of a node

Will return incoming aswell as outgoing nodes.

db.ReadAllRelationshipsOfNode(node_id, function(err, relationships){
    if(err) throw err;

    console.log(relationships); // delivers an array of relationship objects.
});

Get all incoming relationships of a node

db.ReadIncomingRelationshipsOfNode(node_id, function(err, relationships){
    if(err) throw err;

    console.log(relationships); // delivers an array of relationships objects.
});

Get all outgoing relationships of a node

db.ReadOutgoingRelationshipsOfNode(node_id, function(err, relationships){
    if(err) throw err;

    console.log(relationships); // delivers an array of relationships objects.
});

Run a cyper query against Neo4j

db.CypherQuery("START user = node(123) MATCH user-[:RELATED_TO]->friends RETURN friends", function(err, result){
    if(err) throw err;

    console.log(result.data); // delivers an array of query results
    console.log(result.columns); // delivers an array of names of objects getting returned
});

Tests

This API wrapper relies on mocha for testing, therefore when you want to run the tests follow the steps below.

$ git clone git://github.com/philippkueng/node-neo4j.git
$ cd node-neo4j/
$ npm install
$ npm test

Issues or Feature Requests?

In case you run into an issue while using the wrapper or you have a feature request please let me know by creating a new issue or contacting me via twitter.

License

Copyright (C) 2012 Philipp Küng

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.