First of all, what is an ORM

Object-Relational Mapping (ORM) is a technique that lets you query and manipulates data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase “an ORM”.

Most used ORM in Node JS

Sequelize

Sequelize is a promise-based ORM for Node.js and io.js. It supports PostgreSQL, MySQL, MariaDB, SQLite and MSSQL and features transaction support, relations, read replication, and more. Starting from 4.0.0 Sequelize will only support Node v4 and above to use ES6 features.

Example:

const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory');

class User extends Model {}
User.init({

  // Model attributes are defined here

  firstName: {

    type: DataTypes.STRING,

    allowNull: false

  },

  lastName: {

    type: DataTypes.STRING

    // allowNull defaults to true

  }

}, {

  // Other model options go here

  sequelize, // We need to pass the connection instance

  modelName: 'User' // We need to choose the model name

});

// the defined model is the class itself
console.log(User === sequelize.models.User); // true

Node-ORM2

node-orm2 is another ORM tool that supports MySQL, PostgreSQL, Amazon Redshift, and SQLite. Features include:

– Create Models, sync, drop, bulk create, get, find, remove, count, aggregate functions

– Create Model associations, find, check, create and remove

– Define custom validations (several builtin validations, check instance properties before saving)

– Model instance caching and integrity (table rows fetched twice are the same object, changes to one change all)

Example:

orm.connect("mysql://test:test@localhost/text", function(err, db){

        // db is now available to use! ^__^

        var Person = db.define('person', {

        name      : String

    });

});

mode.find({

        field : [ "item 1", "item 2" ]

})

TypeORM

This is one of the best node JS ORM because of its unique capabilities. TypeORM is compatible with both active records and data mapper patterns. Currently, TypeORM is categorized in the Microframework category of tech stack.

Example:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";


@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    age: number;
}

Bookshelf

The Bookshelf also makes it in the list of 12 best Node.JS ORM. This JavaScript ORM for Node is developed on the Knex SQL query builder.

Despite being developed mainly for Node.js, most browsers depend on Bookshelf and other javascript environments that are compatible with the SQLite database.

$ npm install knex

$ npm install bookshelf

// Setting up the database connection

const knex = require('knex')({

  client: 'mysql',

  connection: {
    host     : '127.0.0.1',
    user     : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test',
    charset  : 'utf8'
  }
})

const bookshelf = require('bookshelf')(knex)

// Defining models
const User = bookshelf.model('User', {
  tableName: 'users'
})