A helper library to set up bulk data inserts for Neo4j databases
The main goal of the neo4j-bulk library is to create an easy and intuitive way of setting up the data model for the neo4j bulk API so to set up an initial database state for test suites.
First, install neo4j-bulk
as a development dependency:
npm install --save-dev neo4j-bulk
Then, in your pre-test setup script create a Bulk instance and add data. In this example I'll set up a graph representing the relational status between two purely fictional *cough* *cough* people:
var Bulk = require('neo4j-bulk');
var bulk = new Bulk();
var thomas = bulk.addNode({
name: 'Thomas Vervest'
});
var annemarie = bulk.addNode({
name: 'Annemarie Jellema'
});
bulk.addLabel('Male', thomas);
bulk.addLabel('Male', annemarie);
bulk.addRelation(thomas, annemarie, 'ENGAGED_TO');
bulk.addRelation(annemarie, thomas, 'ENGAGED_TO');
var batchObject = bulk.generateBatch();
The batchObject
variable now contains an array which, when represented as JSON, can be posted to the Neo4j batch API resulting in the following psuedo-cypher dataset:
(t:Male { name: "Thomas Vervest" }),
(a:Female { name: "Annemarie Jellema" }),
(t)-[:ENGAGED_TO]->(a),
(a)-[:ENGAGED_TO]->(t)
Returns a new instance of the neo4j-bulk class.
Creates a new node and returns the node's reference ID.
data (object): An object containing the node's properties
The newly created node's reference ID
Adds the label to the specified node, multiple labels can be added to a single node by calling this method multiple times.
label (string): The name of the label nodeID (number, optional): The reference ID of a previously generated node, by default the last created node reference ID is used
Has no return value. An error is thrown if the reference ID is not an existing node, or no nodes have been created.
Adds a named relationship between two nodes, starting at startNode and pointing to endNode. Optionally, the relation can be given properties using the data parameter.
startNode (number): The reference ID of a previously generated node, from which the relationship will point endNode (number): The reference ID of a previously generated node, to which the relationship will point relationName (string): The name of the relationship data (object, optional): The properties of the relationship
Has no return value. An error is thrown if the startNode or endNode reference ID is not an existing node, or no nodes have been created.
Adds a named relationship between two nodes, starting at the node with id startNodeID
and pointing to the node with id endNode
. Optionally, the relation can be given properties using the data parameter.
startNodeID (number): The ID of a previously inserted node, from which the relationship will point endNodeID (number): The ID of a previously inserted node, to which the relationship will point relationName (string): The name of the relationship data (object, optional): The properties of the relationship
Has no return value. An error is thrown if the startNode or endNode reference ID is an invalid node ID.
Using this method a batch operation can be duplicated. This can be useful when a centralized dataset is used which is then extended per test case. Using this method the core dataset will remain unaltered.
A copy of the Batch instance.
Generates a batch operation object, compatible with the Neo4j batch API when serialised to JSON.
An array containing the batch operations.
Searches the collection of nodes for a node matching the predicate, and then returns the matching node's reference ID.
predicate (function): The predicate function used to test a match. The function will take a single argument, which is the iterated node, and should return true if the predicate matches, or false otherwise.
The reference ID of the first node that matches the predicate, false otherwise.