Skip to content

Node ORMs

Spinning Idea edited this page Jun 18, 2023 · 14 revisions

Below is a comparison of the major ORMs (Object Relational Mappers) available in the node js ecosystem and pointers to example POCs of their use and discussion on the trade-offs on each.

Comparisons

  • sequelize
  • prisma
  • typeorm
  • knex - ORM with Knex, use Objection

Other ORMs

Below are other ORMs that are noteworthy but are not yet evaluated in the set below


Sequelize

Sequelize is a modern TypeScript and Node.js ORM for Oracle, Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.

Repository Support

As of 12/2022 sequelize itself does not contain a repository but community work is emerging such a thing.

Community Repos

Migrations Support

Pagination

Has support for skip and limit and can page data.

Model Management

POCs


Prisma

Next-generation Node.js and TypeScript ORM

Repository Support

As of 12/2022 Prisma does not have first class support of "Repository of T" scenarios but conversation is underway and some work is being done by the community.

Community Repos

Issues

Migrations Support

Prisma has first class support for migrations

Model Management

Pagination

Has support for standard pagination

POCs


TypeORM

TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large scale enterprise applications with multiple databases.

TypeORM supports both Active Record and Data Mapper patterns, unlike all other JavaScript ORMs currently in existence, which means you can write high quality, loosely coupled, scalable, maintainable applications the most productive way.

Repository Support

Has first class support for "Repository of T" that can take a strongly typed model and do CRUD operations and more.

Migrations Support

TypeORM has first class support for migrations

POCs


Knex

Knex.js (pronounced /kəˈnɛks/) is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use.

It features both traditional node style callbacks as well as a promise interface for cleaner async flow control, a stream interface, full-featured query and schema builders, transaction support (with savepoints), connection pooling and standardized responses between different query clients and dialects.

Repository Support

Migrations Support

Knex has support for migrations


Base Repository aka "Repository of T"

What is the repository pattern?

The repository pattern is a strategy for abstracting data access so that one can swap in different implementations to retrieve data and improve the testability and evolutionary possibilities of the code base. The repository is meant to encapsulate data access and is made up of the code in an application that deals with the SINGLE RESPONSIBILITY of storing and retrieving data and should not contain logic or additional concerns. The repository pattern and approach aids SOLID principles by enabling the replacement of data access implementations.

Example

import BaseRepository from './BaseRepository';
import { User } from '../Models';

export default class UserRepository extends BaseRepository {
  constructor() {
    super();
    this.model = User;
  }

  async findByEmail(email) {
    return this.model.findOne({
      where: {
        email,
      },
    });
  }

  // other methods

Examples with Discussion

Clone this wiki locally