-
Notifications
You must be signed in to change notification settings - Fork 2
Node ORMs
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.
- sequelize
- prisma
- typeorm
- knex - ORM with Knex, use Objection
Below are other ORMs that are noteworthy but are not yet evaluated in the set below
-
mikro-orm - TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns.
-
https://kysely.dev/ - The type-safe SQL query builder for TypeScript
-
drizzle - TypeScript ORM for SQL database
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.
As of 12/2022 sequelize itself does not contain a repository but community work is emerging such a thing.
- https://github.com/sequelize/sequelize-typescript/blob/master/src/sequelize/repository/repository.ts
- https://stackoverflow.com/questions/69051499/typescript-repository-pattern-with-sequelize
Has support for skip and limit and can page data.
Next-generation Node.js and TypeScript ORM
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.
- https://www.npmjs.com/package/@krsbx/prisma-repo
- https://github.com/prisma-utils/prisma-utils/tree/main/libs/prisma-crud-generator
- https://github.com/prisma/prisma/discussions/3929
- https://github.com/prisma/prisma/issues/5273
- https://github.com/prisma/prisma/issues/5273#issuecomment-1233475825
Prisma has first class support for migrations
Has support for standard pagination
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.
Has first class support for "Repository of T" that can take a strongly typed model and do CRUD operations and more.
TypeORM has first class support for migrations
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.
- https://knexjs.org/guide/query-builder.html
- https://innovativeteams.net/nodejs-database-repository-using-knex/
Knex has support for migrations
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.
- https://deviq.com/design-patterns/repository-pattern
- https://medium.com/@erickwendel/generic-repository-with-typescript-and-node-js-731c10a1b98e
- https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design
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