Goals If you are a JavaScript developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive it will introduce the different Drivers and link to the relevant resources. Prerequisites You should be… Read more →

Goals
If you are a JavaScript developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive it will introduce the different Drivers and link to the relevant resources.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have installed Neo4j and made yourself familiar with our Cypher Query language.
Intermediate

nodejs


Neo4j for JavaScript Developers

Neo4j can be installed on any system and then accessed via its binary and HTTP APIs.

You can use the official driver for JavaScript (neo4j-driver) or connect via HTTP using the request module or any of our community drivers.

Neo4j Javascript Driver

The Neo4j JavaScript driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to JavaScript, allowing you to subscribe to a stream of responses, errors and completion events.

npm install neo4j-driver
const neo4j = require('neo4j-driver').v1;

const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const session = driver.session();

const personName = 'Alice';
const resultPromise = session.run(
  'CREATE (a:Person {name: $name}) RETURN a',
  {name: personName}
);

resultPromise.then(result => {
  session.close();

  const singleRecord = result.records[0];
  const node = singleRecord.get(0);

  console.log(node.properties.name);

  // on application exit:
  driver.close();
});

Name

Version

Authors

neo4j-driver

1.7.2

The Neo4j Team

Package

Neo4j Online Community

Docs

API

Source

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Neo4j Community Drivers

The drivers below have been thankfully contributed by the Neo4j community. Many of these are fully featured and well maintained. But we don’t take any responsibility for their fitness for use with the most recent versions of Neo4j.

js2neo

As a demonstration of a minimal JavaScript based bolt driver, Nigel Small put together js2neo.

Using the HTTP-Endpoint directly

You can use something as simple as the request node-module to send queries to and receive responses from Neo4j. The endpoint protocol and formats are explained in detail in the Neo4j Manual. It enables you do to much more, e.g. sending many statements per request or keeping transactions open across multiple requests.

Here is a very simple example:

Simple Function Accessing the Remote Endpoint
var r=require("request");
var txUrl = "http://localhost:7474/db/data/transaction/commit";
function cypher(query,params,cb) {
  r.post({uri:txUrl,
          json:{statements:[{statement:query,parameters:params}]}},
         function(err,res) { cb(err,res.body)})
}
Running the Function
var query="MATCH (n:User) RETURN n, labels(n) as l LIMIT {limit}"
var params={limit: 10}
var cb=function(err,data) { console.log(JSON.stringify(data)) }

cypher(query,params,cb)

{"results":[
  {"columns":["n","l"],
   "data":[
     {"row":[{"name":"Aran"},["User"]]}
    ]
  }],
 "errors":[]}