Adding the Driver

This is your first challenge of this course. Your challenge here is to modify the code to create a new instance of the Driver that can be used across the application.

As we discussed in the About the Driver lesson, it is best practice to create a single instance of the driver in our application per Neo4j cluster or DBMS.

Inside src/neo4j.js, you will see an initDriver() function, and above a let driver declaration.

js
src/neo4j.js
let driver

export async function initDriver(uri, username, password) {
  // TODO: Create an instance of the driver here
}

This function should use the uri, username and password parameters supplied to create an instance of the Neo4j JavaScript Driver and assign it to the driver variable.

Below, the getDriver() function will provide other modules in the application with access to the driver variable set in initDriver().

js
src/neo4j.js
export function getDriver() {
  return driver
}

Challenge: Implement the initDriver function.

Your first challenge is to modify the initDriver() function in src/neo4j.js to create an instance of the driver and assign it to the driver variable declared above. To do this, we will need to:

  1. Install the neo4j-driver dependency.

  2. Import the dependency into neo4j.js.

  3. Create the driver instance with the uri, username and password parameters passed to the function and use the verifyConnectivity method to assert that the credentials are correct.

Open src/neo4j.js

1. Install the neo4j-driver Dependency

First, you will need to install the neo4j-driver dependency. Run the following command in a new terminal session:

Learn how to interact with Neo4j from Node.js using the Neo4j JavaScript DriverNPM
sh
Install neo4j-driver using NPM
npm install --save neo4j-driver
Learn how to interact with Neo4j from Node.js using the Neo4j JavaScript DriverYarn
sh
Install neo4j-driver using Yarn
yarn add --save neo4j-driver

Using the --save option adds the entry to the list of dependencies in our package.json file.

2. Importing the Dependency

To include the Driver dependency in our module, you will add an import command to the top of the file. Copy and paste the following code at the top of src/neo4j.js.

js
// Import the neo4j dependency from neo4j-driver
import neo4j from 'neo4j-driver'

3. Creating the Driver Instance

Create the driver instance by calling the neo4j.driver() method. The first argument will be the uri passed as the first parameter to the function. The second argument will be an authentication token which can be created using the neo4j.auth.basic() function, this takes two arguments; username and password

Replace the initDriver placeholder function with the following code:

js
src/neo4j.js
let driver

export async function initDriver(uri, username, password) {
  driver = neo4j.driver(
    uri,
    neo4j.auth.basic(
      username,
      password
    )
  )

  await driver.verifyConnectivity()

  return driver
}

In the last line of the function above, the verifyConnectivity() function will verify that the connection details are correct.

If the connection cannot be made for any reason, the Promise will be rejected. If this occurs, your application will be unable to communicate with Neo4j. Manual investigation will be required to diagnose the issue.

If the connection has been successfully verified, the Promise will resolve with an instance of the driver.

Testing

To test that this functionality has been correctly implemented, run the following code in a new terminal session:

sh
Running the test
npm run test 01

The test file is located at test/challenges/01-connect-to-neo4j.spec.js.

Are you stuck? Click here for help

If you get stuck, you can see a working solution by checking out the 01-connect-to-neo4j branch by running:

sh
Check out the 01-connect-to-neo4j branch
git checkout 01-connect-to-neo4j

You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.

Check Your Understanding

How many tests are run in the npm run test 01 command?

  • ❏ 1

  • ❏ 2

  • ✓ 3

  • ❏ 4

Hint

You can run the test/challenges/01-connect-to-neo4j.spec.js test suite by running the following command:

sh
npm run test 01

Solution

There are 4 tests in the test suite.

Lesson Summary

You have implemented the code to create a new driver instance with server credentials held in the project’s environment variables. The code also verifies that the driver can connect to the DBMS before the application starts.

In the next module we will look at how to use the driver to query the DBMS.