How to Make a Simple CRUD Example using ExpressJS and Neo4j

Sharing is caring!

Overview

Today I will write something different in this article. Most of my articles are based on Go and Java. Now I will try to write something with Javascript, a must known programming language for most developers. More precise, I will write a simple CRUD service with NodeJS and Neo4j as a persistence data store.

How to install NodeJS, NVM and Neo4j are beyond the scope of this article.

ExpressJS

ExpressJS is a server-side web framework for NodeJS. It builds on the top of NodeJS. One of the important strength of ExpressJS is, it makes routing very easy. Furthermore, it is the most popular web framework in NodeJS world.

Neo4j

Neo4j is a graph database management system developed by Neo4j, Inc. Described by its developers as an ACID-compliant transactional database with native graph storage and processingยน. It supports Cypher Query Language (CQL) to manipulate data in graph database. For this article, I will use docker to run neo4j instance.

Use Case

For this use case, I will create a service that can insert, update, list and delete a company data. The data will be stored as a graph inside Neo4j.

Step 1: Create Express App

I will create an express app with name expressjs-neo4j. First, I need to install express-generator in order to install a wizard to create project setup.

We can create a directory outline just by typing:

It will generate files and folders that are necessary for express to run. Then type

Open a browser http://localhost:3000 to validate that your express is up and running.

Step 2: Installing Modules

There are some modules that are necessary in order to make our service run based on its functionalities. For example, neo4j-driver module for database driver. There are many more modules to be installed.

Step 3: Setup Configuration

Setup the configuration file to configure database connection parameters and several initial values.

There are several steps needs to be configured inside app.js. For instance, to enable authorization by using PassportJS, compress payload and expose swagger endpoint. See the source code for more details.

Step 4: Database Connection Utility

For getting database session easier, I will create a utility file for database connectivity.

Step 5: Model

Now create a model as a persistence layer to graph database.

The other one to extract data from query results.

Note: I extend the date property because neo4j date property is a little bit different. See how to get date in the neo4j/dbUtils.js for more details.

Step 6: Routes

Create a function to route service endpoint.

Note: I embed the swagger-code-gen to generate documentation in swagger-ui.

And add the router to app.js

Step 7: Execute and Run

Since we activate the authorization, so we need to pass the JWT in the request header.

One more interesting information, go to your browser and type http://localhost:3000/api-docs. You will get the list of your swagger documentation.

Sample Code

That’s from me now. You can see complete source code, including how to run testing using karma and chai, on my github.

Author: ru rocker

I have been a professional software developer since 2004. Java, Python, NodeJS, and Go-lang are my favorite programming languages. I also have an interest in DevOps. I hold professional certifications: SCJP, SCWCD, PSM 1, AWS Solution Architect Associate, and AWS Solution Architect Professional.

2 thoughts on “How to Make a Simple CRUD Example using ExpressJS and Neo4j”

Leave a Reply

Your email address will not be published. Required fields are marked *