-
-
Notifications
You must be signed in to change notification settings - Fork 3
Neo4jDB Class
propertyKeys()
labels()
relationshipTypes()
version()
graph(cypher, [opts, callback])
queryOne(cypher, [opts])
querySync(cypher, [opts])
queryAsync(cypher, [opts, callback])
query(cypher, [opts, callback])
cypher(cypher, [opts, callback])
batch(tasks, [settings, callback])
- [
Neo4jTransaction
]:transaction([settings, opts])
- [
Neo4jNode
]:nodes([id, reactive])
- [
Neo4jRelationship
]:relationship.create(from, to, type, [properties])
- [
Neo4jRelationship
]:relationship.get(id, [reactive])
constraint.create(label, keys, [type])
constraint.drop(label, key, [type])
constraint.get([label, key, type])
index.create(label, keys)
index.get([label])
index.drop(label, key)
Connect to DB
-
url
{String} - URL to Neo4j like:http://localhost:7474
, this package supportshttps://
protocol. This argument is optional, you may put URL into environment variableNEO4J_URL
orGRAPHENEDB_URL
-
opts
{Object} - This argument is optional, you may omit it if your Neo4j has no authentication. -
opts.username
{String} - Username. Alias:user
-
opts.password
{String} - Password. Alias:pass
-
opts.base
{String} - Base path at Neo4j server, default:db/data
-
opts.headers
{Object} - Additional headers, will be used in every request to Neo4j, default headers:{Accept: 'application/json; charset=UTF-8', 'X-Stream': 'true', 'Content-Type': 'application/json'};
- Returns: {Neo4jDB}
const Neo4jDB = require('neo4j-fiber').Neo4jDB;
const db = new Neo4jDB('http://localhost:7474', {username: 'neo4j', password: '1234'});
List all property keys ever used in the database
- Returns: {[String]}
db.propertyKeys();
List all labels ever used in the database
- Returns: {[String]}
db.labels();
List all relationship types ever used in the database
- Returns: {[String]}
db.relationshipTypes();
Return version of Neo4j server we connected to
- Returns: {String}
db.version();
Request results as graph representation, read reference for more info
-
settings
{Object | String} - Cypher query as String or object of settings -
settings.cypher
{String} - Cypher query, alias:settings.query
-
settings.opts
{Object} - Map of cypher query parameters, aliases:settings.parameters
,settings.params
-
settings.reactive
{Boolean} - Reactive nodes updates while retrieve data fromNeo4jCursor
. Default:false
. Alias:settings.reactiveNodes
-
settings.callback
{Function} - Callback function. If passed, the method runs asynchronously. Alias:settings.cb
-
opts
{Object} - Map of cypher query parameters -
callback
{Function} - Callback function. If passed, the method runs asynchronously - Returns: {Neo4jCursor}
db.graph("MATCH ()-[r]-() RETURN DISTINCT r").fetch();
db.graph("MATCH ()-[r {props}]-() RETURN DISTINCT r", {props: p1: 'v1', p2: 'v2'}).fetch();
db.graph "MATCH ()-[r]-() RETURN DISTINCT r", (error, cursor) => {
cursor.fetch();
// Returns array of arrays nodes and relationships:
// [{nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]},
// {nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]},
// {nodes: [{...}, {...}, {...}], relationships: [{...}, {...}, {...}]}]
});
Shortcut for query
, which returns first result from your query as plain Object
-
cypher
{String} - Cypher query as String -
opts
{Object} - Map of cypher query parameters - Returns: {Object}
db.queryOne("MATCH (n) LIMIT 1 RETURN n").n;
Shortcut for query
(see below for more). Always runs synchronously, but without callback.
-
cypher
{String} - Cypher query as String -
opts
{Object} - Map of cypher query parameters - Returns: {Neo4jCursor}
db.querySync("MATCH (n) WHERE id(n) = {id} RETURN n", {id: id}).fetch();
Shortcut for query
(see below for more). Always runs asynchronously, even if no callback is passed. Best option for independent deletions.
-
cypher
{String} - Cypher query as String -
opts
{Object} - Map of cypher query parameters -
callback
{Function} - Callback function witherror
andcursor
arguments - Returns: {Neo4jCursor}
db.queryAsync("MATCH (n) WHERE id(n) = {id} RETURN n", {id: id}, (error, cursor) => {
cursor.fetch();
});
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 reference for more info.
-
settings
{Object | String} - Cypher query as String or object of settings -
settings.cypher
{String} - Cypher query, alias:settings.query
-
settings.opts
{Object} - Map of cypher query parameters, aliases:settings.parameters
,settings.params
-
settings.reactive
{Boolean} - Reactive nodes updates when retrieve data fromNeo4jCursor
. Default:false
. Alias:settings.reactiveNodes
-
settings.callback
{Function} - Callback function. If passed, the method runs asynchronously. Alias:settings.cb
-
opts
{Object} - Map of cypher query parameters -
callback
{Function} - Callback function. If passed, the method runs asynchronously - Returns: {Neo4jCursor}
db.query("MATCH (n) RETURN n").fetch();
db.query("MATCH (n) WHERE id(n) = {id} RETURN n", {id}).fetch();
db.query("MATCH (n) RETURN n", (error, cursor) => {
cursor.fetch();
});
Send query to Neo4j via cypher endpoint. This query 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 reference for more info.
-
settings
{Object | String} - Cypher query as String or object of settings -
settings.cypher
{String} - Cypher query, alias:settings.query
-
settings.opts
{Object} - Map of cypher query parameters, aliases:settings.parameters
,settings.params
-
settings.reactive
{Boolean} - Reactive nodes updates while retrieve data fromNeo4jCursor
. Default:false
. Alias:settings.reactiveNodes
-
settings.callback
{Function} - Callback function. If passed, the method runs asynchronously. Alias:settings.cb
-
opts
{Object} - Map of cypher query parameters -
callback
{Function} - Callback function. If passed, the method runs asynchronously - Returns: {Neo4jCursor}
Note: this is legacy method.
db.cypher("MATCH (n) RETURN n").fetch();
db.cypher("MATCH (n) WHERE id(n) = {id} RETURN n", {id}).fetch();
db.cypher("MATCH (n) RETURN n", (error, cursor) => {
cursor.fetch();
});
Send tasks to batch endpoint, this method allows to work directly with Neo4j REST API. Read reference for more info.
-
tasks
{[Object]} - Array of tasks -
tasks.$.method
{String} - HTTP(S) method used sending this task, one of: 'POST', 'GET', 'PUT', 'DELETE', 'HEAD' -
tasks.$.to
{String} - Endpoint (URL) for task -
tasks.$.id
{Number} - [Optional] Unique id to identify task. Should be always unique! -
tasks.$.body
{mix} - [Optional] JSONable object which will be sent as data to task -
settings
{Object} -
settings.reactive
{Boolean} - iftrue
and ifplain
is true data of node(s) will be updated before returning -
settings.plain
{Boolean} - iftrue
, results will be returned as simple objects instead ofNeo4jCursor
- Returns: {[Object]} - Array of
Neo4jCursor
s or array of Objects ifplain
istrue
const cursors = db.batch([{
method: "POST",
to: db.__service.cypher.endpoint,
body: {
query: "CREATE (n:BatchTest {data})",
params: data: BatchTest: true
}
}, {
method: "POST",
to: db.__service.cypher.endpoint,
body: query: "MATCH (n:BatchTest) RETURN n",
id: 999
}, {
method: "POST",
to: db.__service.cypher.endpoint,
body: query: "MATCH (n:BatchTest) DELETE n"
}]);
cursors.forEach( (cursor) => {
if (cursor._batchId === 999) {
cursor.fetch();
}
});
Open Neo4j Transaction. All methods on Neo4jTransaction
instance is chainable. Read reference for more info.
-
settings
{Function | Object | String | [String]} - [Optional] Cypher query as String or Array of Cypher queries or object of settings -
settings.cypher
{String | [String]} - Cypher query(ies), alias:settings.query
-
settings.opts
{Object} - Map of cypher query(ies) parameters, aliases:settings.parameters
,settings.params
-
settings.resultDataContents
{[String]} - Array of contents to return from Neo4j, like:REST
,row
orgraph
. Default:['REST']
-
settings.reactive
{Boolean} - Reactive nodes updates when retrieve data fromNeo4jCursor
. Default:false
. Alias:settings.reactiveNodes
-
opts
{Object} - [Optional] Map of cypher query(ies) parameters - Returns: {Neo4jTransaction} -
Neo4jTransaction
instance
db.transaction("MATCH (n) RETURN n").commit();
db.transaction(["CREATE (n:Person)", "MATCH (n:Person) SET n.id = {id} RETURN n"], {id}).commit()[1].fetch();
Create or get node object. If no arguments is passed, then new node will be created. If first argument is number, then node will be fetched from Neo4j. If first argument is Object, then new node will be created with passed properties. Read reference for more info.
-
id
{Number | Object} - [Optional], see description above -
reactive
{Boolean} - if passed astrue
- data of node will be updated before returning - Returns: {Neo4jNode} - Neo4jNode instance
db.nodes().get();
db.nodes({prop: 'value'}).get();
db.nodes(123).get();
Create relationship between two nodes. Read reference for more info.
-
from
{Number | Neo4jNode} - id or instance of node -
to
{Number | Neo4jNode} - id or instance of node -
type
{String} - Type (label) of relationship -
properties
{Object} - Relationship's properties -
properties._reactive
{Boolean} - SetNeo4jRelationship
instance to reactive mode - Returns: {Neo4jRelationship}
db.relationship.create(123, 124, "KNOWS").get();
const n1 = db.nodes();
const n2 = db.nodes();
db.relationship.create(n1, n2, "KNOWS", {prop: 'value'}).get();
db.relationship.create(123, 124, "KNOWS", {prop: 'value', _reactive: true}).get();
Get relationship object, by id. Read reference for more info.
-
id
{Number} - Relationship's id -
reactive
{Boolean} - SetNeo4jRelationship
instance to reactive mode - Returns: {Neo4jRelationship}
const r = db.relationship.get(56);
r.property('key', 'value');
r.get();
r.delete();
Create constraint for label. Read reference for more info.
-
label
{String} - Label name -
keys
{[String]} - Keys -
type
{String} - Constraint type, defaultuniqueness
- Returns: {Object}
db.nodes({uuid: 123}).labels.set('Person');
db.constraint.create('Person', ['uuid']);
Remove (drop) constraint for label. Read reference for more info.
-
label
{String} - Label name -
key
{String} - Key -
type
{String} - Constraint type, defaultuniqueness
- Returns: {[]} - Empty array
db.nodes({uuid: 123}).labels.set('Person');
db.constraint.create('Person', ['uuid']);
db.constraint.drop('Person', 'uuid');
Get constraint(s) for label, or get all DB's constraints. Read reference for more info.
-
label
{String} - Label name -
key
{String} - Key -
type
{String} - Constraint type, defaultuniqueness
- Returns: {[Object]}
db.nodes({uuid: 123}).labels.set('Person');
db.constraint.create('Person', ['uuid']);
db.constraint.get(); // All DB-wide constraints
db.constraint.get('Person'); // All constraints on label
db.constraint.get('Person', 'uuid'); // Certain constraint on label
Create index for label. Read reference for more info.
-
label
{String} - Label name -
keys
{[String]} - Index keys - Returns: {Object}
db.nodes({uuid: 123}).labels.set('Person');
db.index.create('Person', ['uuid']);
Get indexes for label. Read reference for more info.
-
label
{String} - Label name - Returns: {Object}
db.nodes({uuid: 123}).labels.set('Person');
db.index.create('Person', ['uuid']);
db.index.get('Person');
db.index.get(); // All DB-wide indexes
Remove (drop) index for label. Read reference for more info.
-
label
{String} - Label name -
key
{String} - Index key - Returns: {Object}
db.nodes({uuid: 123}).labels.set('Person');
db.index.create('Person', ['uuid']);
db.index.drop('Person', 'uuid');
propertyKeys()
labels()
relationshipTypes()
version()
graph()
queryOne()
querySync()
queryAsync()
query()
cypher()
batch()
transaction()
nodes()
relationship.create()
relationship.get()
constraint.create()
constraint.drop()
constraint.get()
index.create()
index.get()
index.drop()
get()
delete()
update()
degree()
to()
from()
path()
relationships()
property()
properties.get()
properties.set()
properties.delete()
properties.update()
label()
labels.set()
labels.replace()
labels.delete()
index.create()
index.get()
index.drop()