Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
483 lines (326 sloc) 14.8 KB

« Documentation Home

Graph Class

A graph class represents a connection to a particular neo4j database.

Properties

Methods

Node Related Methods

Relationship Related Methods

Auto-Indexing Related Methods

Other Methods

Constructor

The Graph constructor is internal. A Graph object is retruned from connect as follows:

var neo4j = require('neo4j-js');
neo4j.connect('http://localhost:7474/db/data/', function (error, graph) {
    // do something with graph object
})

After calling connect, the returned Graph object should be stored in a way which makes it accessible within your application without calling connect again.

Since neo4j uses a REST API, it is inherently stateless, meaning, if the connection goes down, requests will fail until access is restored, but there is no need to 'reconnect' as would be the case in a socket-based API protocol.

Properties

version

String Graph.version

The version string as reported by neo4j.

Methods

addNodeAutoIndexProperty

Graph.addNodeAutoIndexProperty ( [Batch,] String property, Function callback )

Adds a property which will be automatically indexed for nodes (if automatic indexes are enabled.) See http://docs.neo4j.org/chunked/stable/auto-indexing.html

property

  • The name of the property to index.

callback

  • Signature: Function (error)

addRelationshipAutoIndexProperty

Graph.addRelationshipAutoIndexProperty ( [Batch,] String property, Function callback )

The relationships version of addNodeAutoIndexProperty

createBatch

(Batch) Graph.createBatch (  )

Instantiates a Batch object which can be used to manually group API calls into a single batch request. Most library methods accept a Batch object as an optional first parameter.

createNode

Graph.createNode ( [Batch,] Function callback )
Graph.createNode ( [Batch,] Object data, Function callback )

Creates a node in neo4j.

data

callback

  • Signature: Function (error, node)
    • node If successful, a Node object

createNodeIndex

Graph.createNodeIndex ( [Batch,] String name, Function callback )
Graph.createNodeIndex ( [Batch,] String name, Object config, Function callback )
Graph.createNodeIndex ( [Batch,] Function callback )
Graph.createNodeIndex ( [Batch,] Object config, Function callback )

Creates a node index.

name

config

callback

  • Signature: Function (error)

createRelationshipIndex

Graph.createRelationshipIndex ( [Batch,] String name, Function callback )
Graph.createRelationshipIndex ( [Batch,] String name, Object config, Function callback )
Graph.createRelationshipIndex ( [Batch,] Function callback )
Graph.createRelationshipIndex ( [Batch,] Object config, Function callback )

Creates a relationship index. See createNodeIndex for parameter descriptions. name defaults to 'relationship_auto_index'.

deleteNode

Graph.deleteNode ( [Batch,], Mixed id, Function callback )

Deletes a node. If a node has relationships, attempting to delete it will fail. For this reason, it is generally recommended to delete nodes using Cypher queries.

id

  • A string or number representing the ID of the node to delete.

callback

  • Signature: Function (error)

deleteNodeIndex

Graph.deleteNodeIndex ( [Batch,] String name, Function callback )

Deletes a node index.

name

  • The name of the index to delete.

callback

  • Signature: Function (error)

deleteRelationship

Graph.deleteRelationship ( [Batch,], Mixed id, Function callback )

Deletes a relationship.

id

  • ID of the relationship to delete.

callback

  • Signature: Function (error)

deleteRelationshipIndex

Graph.deleteRelationshipIndex ( [Batch,] String name, Function callback )

Deletes a relationship index. See deleteNodeIndex for parameter descriptions.

getNode

Graph.getNode ( [Batch,] Mixed id, Function callback )
Graph.getNode ( [Batch,] Array ids, Function callback )

Gets a node by ID.

id

  • String or number representing the ID of the node to search for.

ids

  • An array of ID's. This provides automatic batching of get node requests. It is roughly equivalent to creating a batch, calling getNode multiple times with the batch parameter, then executing batch.run(), except for the fact that when using ids, the callback will only be called once with an array of nodes instead of being called once per node. It is important to note that if one request in a batch fails (such as 404 not found), the entire batch fails. This is true for both manual and automatic batching.

callback

  • If the node is not found error.code will equal 404. Signature: Function (error, node)
    • node If found, a Node object. (If the ids parameter was used, node will actually be an array of Nodes.)

getNodeAutoIndexingStatus

Graph.getNodeAutoIndexingStatus ( [Batch,] Function callback )

Gets whether automatic node indexing is enabled or not.

callback

  • Signature: Function (error, status)
    • status A Boolean representing the auto-indexing status.

getRelationship

Graph.getRelationship ( [Batch,] Mixed id, Function callback )
Graph.getRelationship ( [Batch,] Array ids, Function callback )

Gets a relationship by ID.

id

  • String or number representing the ID of the relationship to search for.

ids

  • See the equivalent description above in getNode.

callback

  • If the node is not found error.code will equal 404. Signature: Function (error, relationship)
    • relationship If found, a Relationship object. (If the ids parameter was used, relationship will actually be an array of Relationships.)

getRelationshipAutoIndexingStatus

Graph.getRelationshipAutoIndexingStatus ( [Batch,] Function callback )

The relationships version of getNodeAutoIndexingStatus.

getRelationshipTypes

Graph.getRelationshipTypes ( [Batch,] Function callback )

Gets a list of all relationship types. The list does not necessarily indicate that each relationship type is in use.

callback

  • Signature: Function (error, types)
    • types An array of strings representing the names of all relationship types.

isNode

(Boolean) Graph.isNode ( Mixed node )

Returns true if node is a Node object, otherwise false.

Each Graph object has its own unique copy of the Node class. This means only Nodes instantiated using this graph object will evaluate to true. Nodes instantiated through a different graph object will evaluate to false.

isRelationship

(Boolean) Graph.isRelationship ( Mixed relationship )

Returns true if relationship is a Relationship object, otherwise false.

Each Graph object has its own unique copy of the Relationship class. This means only Relationships instantiated using this graph object will evaluate to true. Relationships instantiated through a different graph object will evaluate to false.

isPath

(Boolean) Graph.isPath ( Mixed path )

Returns true if path is a Path object, otherwise false.

Each Graph object has its own unique copy of the Path class. This means only Paths instantiated using this graph object will evaluate to true. Paths instantiated through a different graph object will evaluate to false.

listNodeAutoIndexProperties

Graph.listNodeAutoIndexProperties ( [Batch,] Function callback )

Gets a list of properties currently being automatically indexed for nodes.

callback

  • Signature: Function (error, properties)
    • properties An array of strings representing the list of properties.

listNodeIndexes

Graph.listNodeIndexes ( [Batch,] Function callback )

Gets a list of node indexes.

callback

listRelationshipAutoIndexProperties

Graph.listRelationshipAutoIndexProperties ( [Batch,] Function callback )

The relationships version of listNodeAutoIndexProperties.

listRelationshipIndexes

Graph.listRelationshipIndexes ( [Batch,] Function callback )

Same as listNodeIndexes, except for relationship indexes.

nodeExactQuery

Graph.nodeExactQuery ( [Batch,] String key, Mixed value, Function callback )
Graph.nodeExactQuery ( [Batch,] String index, String key, Mixed value, Function callback )

Queries an index for nodes matching the key and value.

index

  • The name of the index to query. If omitted, "node_auto_index" will be used.

key

  • The key to search on.

value

  • The value to search for.

callback

  • Signature: Function (error, nodes)
    • nodes An array of Node objects. If zero nodes are found, nodes.length === 0.

query

Graph.query ( [Batch,] String query, Function callback )
Graph.query ( [Batch,] String query, Object params, Function callback )
Graph.query ( [Batch,] Boolean profile, String query, Function callback )
Graph.query ( [Batch,] Boolean profile, String query, Object params, Function callback )

Executes an arbitrary Cypher query.

profile

query

  • The query string.

params

  • If any parameters are used in the query string (for example START n=node({id})), params should be an object representing the key value pairs (for example { id:8 }).

callback

  • Signature: Function (error, results)
    • results An array of result rows. Each row is an object of key/value pairs. Values which appear to be Nodes / Relationships / Paths are automatically converted into the proper object types.
Query Example
var query = [
    'START n = node({id})',
    'MATCH m-[r]-n',
    'RETURN m, r'
];

graph.query(query.join('\n'), { id: 1 }, function (err, results) {
    if (err) {
        console.log(err);
        console.log(err.stack);
    }
    else {
        for (var i = 0; i < results.length; i++) {
            var relationship = results[i].r;
            var node = results[i].m;

            // ... do something with the nodes and relationships we just grabbed 
        }

        console.log(JSON.stringify(results, null, 5 )); // printing may help to visualize the returned structure
    }
});

reconnect

Allows a graph object to be re-associated with a different server.

This method is not stable yet, and should not be used. Specifically, it will cause all associated objects to become invalid.

relationshipExactQuery

Graph.relationshipExactQuery ( [Batch,] String key, Mixed value, Function callback )
Graph.relationshipExactQuery ( [Batch,] String index, String key, Mixed value, Function callback )

Queries an index for relationships matching the key and value.

Uses the same parameter descriptions as nodeExactQuery, except index defaults to "relationship_auto_index" and callback provides an array of Relationship objects instead of Nodes.

removeNodeAutoIndexProperty

Graph.removeNodeAutoIndexProperty ( [Batch,] String property, Function callback )

The opposite of addNodeAutoIndexProperty.

property

  • The name of the property to stop indexing.

callback

  • Signature: Function (error)

removeRelationshipAutoIndexProperty

Graph.removeRelationshipAutoIndexProperty ( [Batch,] String property, Function callback )

The relationships version of removeNodeAutoIndexProperty.

setNodeAutoIndexingStatus

Graph.setNodeAutoIndexingStatus ( [Batch,] Boolean enable, Function callback )

Enables or disables automatic node indexing.

enable

  • true to enable, false to disable.

callback

  • Signature: Function (error)

setRelationshipAutoIndexingStatus

Graph.setRelationshipAutoIndexingStatus ( [Batch,] Boolean enable, Function callback )

The relationships version of setNodeAutoIndexingStatus.