Getting Started with neo4j-temp-db. Useful, session-based, self-cleaning, temporary Neo4j databases using the multi-db feature
Introduction
The library was originally created to be used with the Graph Examples Portal, which initially used in-memory Neo4j database library that had been around for seven years. But that is no longer feasible with Neo4j 4.x and also not scalable enough for our needs.
So now the @neo4j-labs/temp-db library allows you to create temporary databases on the fly.
With the advent of multi-database functionality, being able to quickly create and expire Neo4j databases ad hoc has become possible, and functionalities ranging from lightweight console-based applications to more efficient unit tests are feasible.
This post is a walkthrough of a minimal webapp leveraging the @neo4j-labs/temp-dbs JavaScript library.
Getting Started Example
With your Neo4j database running, create a new directory – for example, neo4j-temp-db-demo-app
, then cd
into it with cd neo4j-temp-db-demo-app
.
Run npm init
to get started. Picking all the defaults is OK.
Add and save some necessary dependencies:
npm install express dotenv neo4j-driver ejs — save
We also want to install the babel dependencies to be able to use JavaScript ES6:
npm install @babel/cli @babel/node @babel/cli @babel/core @babel/preset-env nodemon — save-dev
Next, install the @neo4j-labs/temp-dbs
library:
npm install @neo4j-labs/temp-dbs — save
Open the directory in your favorite text editor. Create the following three files to get started:
.babelrc
with your babel presets:
{ “presets”: \[“@babel/preset-env”\] }
.env
with your environment variables with the connection information to the Neo4j database that will host your temporary multi-databases:
NEO4J_URI=”bolt://localhost:7687"
NEO4J_USER=”neo4j”
NEO4J_PASSWORD=”123456"
index.js
with the index of a simple Express app:
import express from “express”;
import dotenv from “dotenv”;
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.listen({ port }, () => {
console.log(`Ready at https://localhost:${port}`);
});
In index.js
, instantiate @neo4j-labs/temp-dbs
using the environment variables sent in .env
:
Add a route that passes the tempDBName
to index.js
:
Create the views/index.ejs
so we have a way to interact with our databases. It’s just a form with two fields — one read-only text field, tempDbName
and one in which to enter Cypher queries, cypher
.
Try refreshing the page — each refresh creates a new temp db.
Try running a Cypher query — the tempdb stays the same.
Cleaning up
Create a file called clean-expired-dbs.js
at the root of the project. You can specify at what age in seconds to clean up your databases:
Add it to the scripts section of the package.json
file to call it from the terminal:
“clean-expired-dbs”: “babel-node ./clean-expired-dbs.js”,
It will return a number of databases found and whether or not they have expired:
Check out the full code of the example app here.
Next Steps: Temporary Databases for Unit Tests
Consider using the @neo4j-labs/temp-dbs library to ease your unit testing. Check out a basic example in this fork of the JavaScript movies example.
About the Authors
Cristina and Alisson work at The SilverLogic, a software development company based in Boca Raton.
Alisson is a software engineer at The SilverLogic. Passionate about plants, he is the driving force behind Que Planta, a GraphQL-based social network for plants.
Resources
A Library for Temporary Neo4j Databases was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.