About the Driver

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.

Example 1. NPM

To install using NPM, run the following command.

shell
NPM
npm install --save neo4j-driver

The --save option saves a reference to the neo4j-driver package in your package.json file.

Example 2. Yarn

To install using Yarn, run the following command.

shell
Yarn
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:

  1. A connection string for the Neo4j cluster or DBMS - for example neo4j://localhost:7687 or neo4j+s://dbhash.databases.neo4j.io:7687

  2. 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:

js
Creating a Driver Instance
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/example/index.js[tag=import]

Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/example/index.js[tag=driver,indent=0]

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.

js
Verify Connectivity
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/example/index.js[tag=verifyConnectivity,indent=0]

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.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?