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.
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()
.
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:
-
Install the
neo4j-driver
dependency. -
Import the dependency into
neo4j.js
. -
Create the driver instance with the
uri
,username
andpassword
parameters passed to the function and use theverifyConnectivity
method to assert that the credentials are correct.
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:
npm install --save neo4j-driver
yarn add --save neo4j-driver
--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
.
// 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:
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:
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:
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:
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.