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 - [drizzle](https://github.com/drizzle-team/drizzle-orm) - TypeScript ORM for SQL database - [MikroORM](https://mikro-orm.io/) - TypeScript ORM for Node.js based on Data Mapper, Unit of Work, and Identity Map patterns. See [Node-ORM-MikroOrm](./Node-ORM-MikroOrm.md) and the [spinningideas/mikro-orm-poc](https://github.com/spinningideas/mikro-orm-poc). * sequelize * prisma * typeorm * knex - ORM with Knex, use [Objection](https://vincit.github.io/objection.js/) - https://npmcompare.com/compare/knex,prisma,sequelize,typeorm,drizzle ## Other ORMs Below are other ORMs that are noteworthy but are not yet evaluated in the set below - [objection](https://vincit.github.io/objection.js/) - https://kysely.dev/ - The type-safe SQL query builder for TypeScript - https://www.atdatabases.org/ - https://joist-orm.io/ - [Other Reviews](https://www.prisma.io/dataguide/database-tools/top-nodejs-orms-query-builders-and-database-libraries) --- # Drizzle - https://github.com/drizzle-team/drizzle-orm - https://orm.drizzle.team/ Drizzle is a TypeScript ORM for SQL databases that provides a type-safe and flexible way to interact with your database. --- # Sequelize - https://sequelize.org/ 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. - See https://github.com/sequelize/sequelize/issues/15389 ### Community Repos - https://github.com/sequelize/sequelize-typescript/blob/master/src/sequelize/repository/repository.ts - https://stackoverflow.com/questions/69051499/typescript-repository-pattern-with-sequelize ## Migrations Support - https://gist.github.com/manuelbieh/ae3b028286db10770c81 - https://stackoverflow.com/questions/27835801/how-to-auto-generate-migrations-with-sequelize-cli-from-sequelize-models - https://gist.github.com/jsumners/e6b5d0a699f16ba980a3 ## Pagination Has support for skip and limit and can page data. ## Model Management - https://github.com/sequelize/sequelize-auto ### POCs - https://github.com/spinningideas/sequelize-ts-poc - https://github.com/spinningideas/sequelize --- # Prisma - https://www.prisma.io/ Next-generation Node.js and TypeScript ORM - [Prisma in Production: A review (jackpordi.com)](https://jackpordi.com/posts/prisma-in-production) - https://www.reddit.com/r/node/comments/zc6moq/prisma_in_production_a_review/ ## 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 - https://www.npmjs.com/package/@krsbx/prisma-repo - https://github.com/prisma-utils/prisma-utils/tree/main/libs/prisma-crud-generator ### Issues - https://github.com/prisma/prisma/discussions/3929 - https://github.com/prisma/prisma/issues/5273 - https://github.com/prisma/prisma/issues/5273#issuecomment-1233475825 ## Migrations Support Prisma has first class support for migrations - https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate ## Model Management - https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names ## Pagination Has support for standard pagination - https://www.prisma.io/docs/concepts/components/prisma-client/pagination ### POCs - https://github.com/spinningideas/prisma-poc --- # TypeORM - https://typeorm.io/ TypeORM is an [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping) 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](https://typeorm.io/active-record-data-mapper#what-is-the-active-record-pattern) and [Data Mapper](https://typeorm.io/active-record-data-mapper#what-is-the-data-mapper-pattern) 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. - https://typeorm.io/working-with-repository ## Migrations Support TypeORM has first class support for migrations - https://typeorm.io/migrations ### POCs - https://github.com/spinningideas/typeorm-poc --- # Knex - https://knexjs.org/ Knex.js (pronounced [/kəˈnɛks/](https://youtu.be/19Av0Lxml-I?t=521)) 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](https://knexjs.org/#Interfaces-Callbacks) as well as a [promise](https://knexjs.org/#Interfaces-Promises) interface for cleaner async flow control, [a stream interface](https://knexjs.org/#Interfaces-Streams), full-featured [query](https://knexjs.org/#Builder) and [schema](https://knexjs.org/#Schema) builders, [transaction support (with savepoints)](https://knexjs.org/#Transactions), connection [pooling](https://knexjs.org/#Installation-pooling) and standardized responses between different query clients and dialects. ## Repository Support - https://knexjs.org/guide/query-builder.html - https://innovativeteams.net/nodejs-database-repository-using-knex/ ## Migrations Support Knex has support for migrations - https://knexjs.org/guide/migrations.html --- # 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](https://www.digitalocean.com/community/conceptual-articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design) 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 ## 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 - https://khalilstemmler.com/articles/typescript-domain-driven-design/repository-dto-mapper/