Javascript

Javascript is one of the Neo4j supported languages and an official driver is provided. Official drivers connect to the databases using BOLT protocol (HTTP/HTTPS is not available).

📘

About connection parameters

We've used test credentials for all the following examples. Please replace them with your real connection parameters.

You'll find all the required information to connect to your database on our admin panel if you navigate to the Connection section. More on connecting to your database here

This section is intended to provide a small working example to connect to a GrapheneDB database using the Javascript driver. To get more detailed information, visit the driver web page: http://neo4j.com/docs/api/javascript-driver/current/

Install neo4j-driver via npm:

$ npm install neo4j-driver

📘

Check the driver releases for the newest driver version available.

Setting up the connection:

var neo4j = require('neo4j-driver');

var driver = neo4j.driver("bolt://hobby-geefdaeefcom.dbs.graphenedb.com:24786", neo4j.auth.basic("v303", "GtGq5rldxu"), {encrypted: 'ENCRYPTION_ON'});

You can also use the new bolt+s URI scheme to enable encryption with a full certificate check:

var driver = neo4j.driver("bolt+s://db-jidgsytyxo1r31sd4ker.graphenedb.com:24786", neo4j.auth.basic("admin", "b.P0shGn2gvpUD.9RAHyuY4QXkkUK7w"));

You can test the connection by running a simple query of the Movies dataset example:

var session = driver.session();
session
  .run("MATCH (:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(movies) RETURN movies.title AS title")
  .subscribe({ 
    onNext: function(record) {
      console.log(record.get('title'));
    },
    onCompleted: function() {
        session.close();
    },
    onError: function() {
        console.log(error);
    }
  });

If you need to use the driver on the browser, there is a special version which supports connection to your Neo4j database using WebSockets.

One of the following tags need to be included in the HTML code (change X.Y.Z with the driver version, e.g. 4.0.2):

<!-- Direct reference -->
<script src="lib/browser/neo4j-web.min.js"></script>

<!-- unpkg CDN non-minified -->
<script src="https://unpkg.com/neo4j-driver"></script>
<!-- unpkg CDN minified for production use, version X.Y.Z -->
<script src="https://unpkg.com/[email protected]/lib/browser/neo4j-web.min.js"></script>

<!-- jsDelivr CDN non-minified -->
<script src="https://cdn.jsdelivr.net/npm/neo4j-driver"></script>
<!-- jsDelivr CDN minified for production use, version X.Y.Z -->
<script src="https://cdn.jsdelivr.net/npm/neo4j-dri[email protected]/lib/browser/neo4j-web.min.js"></script>

Connect to the Neo4j instance and run a simple query from the browser:

var driver = neo4j.driver(
  "bolt+s://db-jidgsytyxo1r31sd4ker.graphenedb.com:24786",
  neo4j.auth.basic("admin", "b.P0shGn2gvpUD.9RAHyuY4QXkkUK7w")
);

var session = driver.session();
session
.run("MATCH (:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(movies) RETURN movies.title AS title")
.then(function(result) {
    result.records.forEach(function(record) {
        console.log(record.get('title'))
    });
    session.close();
})
.catch(function(error) {
    console.log(error);
});