DgraphORM for NodeJS and JavaScript

Ashok Vishwakarma

The official JavaScript client for Dgraph is dgraph-js which lets you connect, query and mutate Dgraph database using NodeJS. So why I needed to build a full fledges ORM from scratch?

Background

My use case was completely different than building a sample app where everything can be written in one file. Which left me thinking where do I need to make the connection, where do I keep my client and client stub object and how to pass these objects in almost all the controllers which needed to interact with the database.

Using dgraph-js directly, I found it a little frustrating as I have not used any database client directly for a long time. I am using ORMs like Mongoose and Sequilize for almost my every NodeJS projects and I was missing them for Dgraph.

Preparation

Before getting started with the code, I have tried to list down all the possible use cases for our project to cover in the ORM. These use cases helped me to architect the ORM in a better way.

  1. It should follow syntaxes like Mongoose or Sequilize.
  2. It should generate the optimized query for a given object.
  3. It should support default values for fields and parameters.
  4. It should let us create a schema.
  5. It should let us query the schema.
  6. It should let us create/update/delete values.
  7. It should support the relation in the schema and query.

The ORM

Based on the first level of use cases, I had to get started from schema first so I have taken the inspiration from Sequilize (An ORM for RDBMS) and divided the ORM into two parts

The Schema Builder

Schema builder is a simple class which takes two parameters in its constructor a name in a string and an object for schema options to do the followings:

  1. Validate the provided schema options and their values.
  2. Generate Dgraph schema syntax from the schema options.
  3. Returns the original and Dgraph schema syntax.
// Sample User schema
import dgraph from 'dgraph-orm';
const UserSchema = new dgraph.Schema('name', {
name: {
type: dgraph.Types.STRING,
index: true,
token: {
term: true
}
},
email: {
type: dgraph.Types.STRING,
unique: true,
index: true,
token: {
exact: true
}
},
});

The Model Generator

The Model Generator is a method of ORM which takes schema as an argument and generate model around the same schema.

const User = dgraph.model(UserSchema);

For the given Schema, Model Generator first creates the schema into Dgraph Database (the schema name prefixed in every predicate eg. user.name and user.email).

After the schema is set into the database the Model Generator generates required methods for the model object along with the relation.

Follow below link to check out, the code and read the full docs

Conclusion

I am not sure if it will help you as it helps me but I am pretty sure it will help you to organize your code so that other developers can understand it better.

It hides the Dgraph database complexity inside its Query Generator which lets you focus on your application and speed up your development.

If you are from ORM background like Mongoose or Sequilize than I am sure you will love it.


Share your thoughts in comments and don’t forget the claps :)

Also, share among your NodeJS developers.

Issues and pull requests are welcome for

  • Unit test cases
  • Feature and query method implementation
  • Bug fixes

Ashok Vishwakarma

Written by

@GoogleDevExpert — #WebTechnologies & @angular | #Principal #Architect at @Naukri | #Entrepreneur | #TechEnthusiast | #Speaker

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade