In the Cypher Fundamentals course, we cover how to query Neo4j using a language called Cypher. To execute a Cypher statement against a Neo4j database you will use an object called a Driver.
The Driver object is a thread-safe, application-wide fixture from which all Neo4j interaction derives.
The Driver API is topology independent, so you can run the same code against a Neo4j cluster or a single DBMS.
To connect to and query Neo4j from within a Node.js application, you use the Neo4j JavaScript Driver.
The Neo4j JavaScript Driver is one of five officially supported drivers, the others are Java, .NET, Python, and Go. There are also a wide range of Community Drivers available for other languages including PHP and Ruby.
You should create a single instance of the Driver in your application per Neo4j cluster or DBMS, which can then be shared across your application.
Installing the Driver
The Neo4j JavaScript Driver is available through the npm registry and can be installed with either npm
or yarn
.
To install using NPM, run the following command.
npm install --save neo4j-driver
The --save
option saves a reference to the neo4j-driver
package in your package.json
file.
To install using Yarn, run the following command.
yarn add --save neo4j-driver
The --save
option will save a reference to the neo4j-driver
package in your package.json
file.
Creating a Driver Instance
Each driver instance will connect to one DBMS, or Neo4j cluster, depending on the value provided in the connection string.
The neo4j-driver
package exports a default neo4j
object. This object provides a driver()
method for creating a new driver instance. The driver()
method requires two arguments:
-
A connection string for the Neo4j cluster or DBMS - for example
neo4j://localhost:7687
orneo4j+s://dbhash.databases.neo4j.io:7687
-
An authentication token - Neo4j supports basic username and password authentication, kerberos tokens or custom authentication. You can create an authentication token by calling one of the functions provided by
neo4j.auth
.
Here is an example for how to create a driver instance:
// Import the neo4j dependency from neo4j-driver
import neo4j from 'neo4j-driver'
// Create a new Driver instance
const driver = neo4j.driver('neo4j://localhost:7687',
neo4j.auth.basic('neo4j', 'neo'))
The above example creates an unencrypted connection to the Neo4j server at localhost
on the default port number of 7687
.
The driver then attempts to authenticate against the server using a basic authentication with the username neo4j
and password neo
.
Verifying Connectivity
You can verify that the connection details used during driver instantiation are correct by calling the verifyConnectivity()
method.
This function returns a Promise which resolves if the connection details are correct, or rejects with a Neo.ClientError.Security.Unauthorized
error if a connection could not be made.
// Verify the connection details
await driver.verifyConnectivity()
Check Your Understanding
1. Which of the following programming languages have officially supported drivers?
-
✓ .NET
-
✓ Go
-
✓ Java
-
✓ JavaScript
-
✓ Python
-
❏ PHP
-
❏ Ruby
Hint
Five languages are officially supported by Neo4j.
Solution
The five supported languages are .NET, Go, Java, JavaScript and Python.
2. What name is the Neo4j JavaScript Driver registered under on NPM?
-
❏
@neo4j/driver
-
❏
neo4j
-
✓
neo4j-driver
-
❏
neo4j-javascript-driver
Hint
The package is registered on npm as neo4j-driver
.
Solution
The package is registered on npm as neo4j-driver
.
Lesson Summary
In this lesson, you learned about the Neo4j JavaScript Driver and how it can be used to connect to Neo4j from within a Node.js application.
In the next lesson, we will take a closer look at the first argument in the neo4j.driver()
method, the connection string.