ORM For JavaScript

Md Tawfiqul Hasan Khan
4 min readJun 27, 2021

Databases are the backbone of any professional app and how easily they can be set up, accessed, and updated can be the determining factors for its scalability and performance. In the world of programming, database, and accessing data from it has been a hot topic throughout the years. There are many libraries or models and ORM or Object-Relational Mapping technique is one of them which has great use and gaining popularity each year. Today, I am going to talk about ORM and the one I prefer to use for my NodeJs app.

What is ORM?

To understand ORM, we need to understand Objects. Objects are a collection of key and value pairs that is very popular for storing data because of their ease of use. A basic example of an Object is as follows

let person = {
name: "Jane Doe",
age: "25",
gender: "female"
};

As you can see, with objects it is very easy to store data for almost anything. ORM it’s a technique that allows us to query and change data from the database in an object-oriented way which basically means it treats the data table as an object so that you can easily use and manipulate data. ORM allows programmers to work with databases without modifying the object-oriented paradigm.

Popular ORMs in JavaScript

Although there are many ORM libraries to choose from, I have decided to highlight four of the most popular ones according to npm download statistics. Those are Sequezlie, Mongoose, TypeORM, and bookshelf.

As you can see from the chart, among these four, mongoose has been a very popular library over the years among developers. All of these libraries mentioned in the chart and also the ones not mentioned here have their pros and cons; however, today I am going to give you a brief overview of Mongoose and why it is the go-to model for me personally.

What is Mongoose exactly?

Mongoose is a MongoDB object modeling tool that is designed to work in an asynchronous environment. It supports both promises and callbacks. A typical way of declaring schema and creating a data insertion can be seen below.

let blog = new Schema({ title: String, description: String, published: Boolean,});let Blog = mongoose.model("Blog", blog);// Create a new blog postlet article = new Blog({ title: "First Post!", description: "look at my Post!", published: true,});// Insert the article in our MongoDB databasearticle.save();// Find a single blog postBlog.findOne({}, (err, post) => { console.log(post);});

From the above snippet, you probably can guess that it feels like you are working with Objects. Mongoose manages relationships between data, provides schema validation, and it can translate between objects in code and the representation of those objects in MongoDB. This makes managing data transfer between the front and back end very efficient. Also because MongoDB is a schema-less NoSQL database, it reduces deployment complexity and speeds up the application development considerably.

Advantages of Using Mongoose

One of the main advantages of using Mongoose is Data validation. This is especially important if you are working on a large team. With mongoose you can not save data in the wrong field, for example, it will prevent you from storing dates in string fields or booleans in an array. This helps you greatly in making sure that your schema is consistent when inserting/updating/finding documents from collections.

Also, in a traditional relational database, changing schema can be a lot of work and can feel like a project itself. This is especially true if the database is not well maintained and in the end, it will probably end up with lots of confusing columns. However in a NoSQL model like Mongoose, it is very difficult to change anything in the schema which would cause an issue for your app. when you add a new schema, your old data will still be there and the new schema will take effect immediately. You will probably have to make some changes in your application logic and how you are using the returned data from the database, but other than that you will not have to make too drastic changes to how you are handling the data.

Another cool feature of Mongoose, in the abstraction that it creates. It feels like you are working with objects rather than a completely different type of data, which helps you visualize what you are doing.

Arguments Against Mongoose or ORM overall

One of the drawbacks of ORM is that developers lose the understanding of what the code is doing. It abstracts the database so you don’t ever write query languages which in the long run can make you unfamiliar with a traditional database. While ORM may make initial dive into programming feel familiar, because of the abstraction they take a lot of power away from you which a traditional database can provide.

In conclusion, I believe, ORM can provide a lot of power and understanding in your development as a programmer but in the ever-changing world of programming, one should always challenge themselves to remain updated on all the available ways of doing the same thing.

--

--