graphaware / node-neo4j-bolt-adapter

An adapter for the official neo4j-javascript-driver, allowing it to be used as a drop-in replacement for the node-neo4j community driver.
5 stars 0 forks source link
adapter community javascript neo4j official

An adapter to allow the official Neo4j bolt driver to be used as a drop in replacement for the node-neo4j community driver.

Usage

Given the node-neo4j community driver declared as follows:

const Promise = require('bluebird')
const neo4j = require('node-neo4j')
const dev = require('./dev')
const db = Promise.promisifyAll(new neo4j(
  `http://${process.env.DB_USER}:${process.env.DB_PW}@${process.env.DB_HOST}:7474`))

module.exports = db;

And used like:


const db = require('db')

db.cypherQueryAsync(`MATCH (u:User) WHERE u.applicationToken = {applicationToken} RETURN U`, 
    {applicationToken: 1234})
    .then(result => {
        //result.columns describes format
        //When a single record is return result.data contains an object, otherwise an array of objects.  
    });

We can define an adapter for the official bolt driver:

const neo = require('neo4j-driver').v1;
const authToken = neo.auth.basic(userName, password);
const db = new BoltAdapter(neo.driver(`bolt://localhost`, authToken));

And use it as an API compatible drop-in replacement:

For a read transaction:

db.cypherQueryAsync(`MATCH (u:User) WHERE u.applicationToken = {applicationToken} RETURN U`, 
    {applicationToken: 1234})
    .then(result => {
        //result.columns describes format
        //When a single record is return result.data contains an object, otherwise an array of objects.  
    });

A session will be opened and a transaction initiated, with auto commit or rollback if an error is thrown. On completion the session will be closed.

For a write transaction

//For write transactions 
db.writeQueryAsync(`CREATE (u:User {name: 'Fred'}) return u`)
    .then(result => {
        //result.columns describes format
        //When a single record is return result.data contains an object, otherwise an array of objects.  
    });

Similar to a read transaction, a session will be opened and a transaction initiated. The session is closed on completion.

Closing

//We can keep a reference, or alternatively, to close the underlying bolt driver
db.close() 

Note

Currently access mode and read/write transaction functions in the official driver are mostly hints. They are not yet translated to access mode on the server - only used by routing driver to decide where to point the request. Until that behavior changes, it is possible to write in a read transaction.