Node.js ORM Comparison: Practical Criteria for Choosing TypeORM, Sequelize, or Prisma
When handling an RDBMS in a Node.js backend, choosing an ORM affects the architecture of the entire project. Most Node.js ORMs aim for a high level of abstraction, rather than being dedicated SQL mappers like myBatis. This article compares TypeORM, Sequelize, and Prisma in a NestJS + TypeScript environment and explains why TypeORM was chosen in practice.
Comparison Criteria
Section titled “Comparison Criteria”| Criterion | Description |
|---|---|
| NestJS + TypeScript compatibility | Decorator-based entity definitions and DI container integration |
| Raw SQL support | Whether queries that require stepping outside ORM abstraction for performance optimization can be executed |
| Documentation quality | Completeness of the official documentation and the volume of community references |
| Oracle compatibility | Whether Oracle, as well as PostgreSQL, can be supported |
Ultimately, TypeORM was chosen. Sequelize had inadequate Oracle support, while Prisma still lacked references and did not support Oracle.
TypeORM vs Sequelize
Section titled “TypeORM vs Sequelize”Feature Comparison
Section titled “Feature Comparison”| TypeORM | Sequelize | |
|---|---|---|
| Table schema definition | ○ | ○ |
| Table record manipulation | ○ | ○ |
| Transaction handling | ○ | ○ |
| Raw SQL | ○ | ○ |
| Active Record support | ○ | ○ |
| Data Mapper support | ○ | ✗ |
| Oracle support | ○ | △ (requires a separate package; unstable) |
Active Record vs Data Mapper
Section titled “Active Record vs Data Mapper”Active Record
Section titled “Active Record”The entity itself has CRUD methods, which provides a high level of convenience but limits extensibility.
const user = new User();user.firstName = 'noisonnoiton';await user.save(); // The Entity has a save() methodData Mapper
Section titled “Data Mapper”Entities and repositories are separated, and data is manipulated through methods provided by the repository. The repository can be customized and extended.
const userRepository = dataSource.getRepository(User);const user = new User();user.firstName = 'noisonnoiton';await userRepository.save(user); // The Repository provides a save() methodTypeORM supports both patterns, but Sequelize supports only Active Record. In large-scale projects, the Data Mapper pattern is more advantageous: business logic does not accumulate in entities and is separated into the service layer.
Oracle Support: The Decisive Difference
Section titled “Oracle Support: The Decisive Difference”To support Oracle, Sequelize requires installation of a separate package (sequelize-oracle); because it is not officially supported, it is less stable. TypeORM supports Oracle out of the box.
TypeORM vs Prisma
Section titled “TypeORM vs Prisma”Prisma Characteristics
Section titled “Prisma Characteristics”Prisma introduces itself in its official documentation as a next-generation ORM. Its official documentation also provides comparison material with existing ORMs.
- Prisma Client + CLI: a type-safe client based on code generation
- Prisma Studio: GUI-based data management
- High abstraction starting with data model definitions: declare every entity and relation in a schema file, then automatically generate the client
- Provides a structure similar to Data Mapper
Why Prisma Was Not Chosen
Section titled “Why Prisma Was Not Chosen”- There were still too few references: there were few real-world adoption cases, and the path to a solution when problems occurred was unclear.
- Prisma’s benefits (modeling and migration) were not needed: the existing schema was already defined, and migrations were managed with a separate tool.
- It does not support Oracle: it supports only PostgreSQL, MySQL, SQLite, and SQL Server.
From a performance perspective, it was expected to outperform existing ORMs, but its risk in real-world adoption was higher than TypeORM’s.
TypeORM Implementation Approach: Without @nestjs/typeorm
Section titled “TypeORM Implementation Approach: Without @nestjs/typeorm”There are two ways to use TypeORM in NestJS.
1. Use the @nestjs/typeorm Package (Standard Approach)
Section titled “1. Use the @nestjs/typeorm Package (Standard Approach)”Import TypeOrmModule from @nestjs/typeorm and connect it simply with forRoot and forFeature.
Advantages:
- It has official NestJS support, so there is less boilerplate.
Disadvantages:
- It increases NestJS module dependencies.
- There may be version conflicts between
@nestjs/typeormandtypeorm.
2. Use the Pure typeorm Package (Chosen Approach)
Section titled “2. Use the Pure typeorm Package (Chosen Approach)”Install only the typeorm package and the database driver, then define the module and provider that manage the database connection directly.
Advantages:
- Fewer NestJS module dependencies: it does not depend on
@nestjs/typeorm. - Easy multiple data source management: it is flexible when connecting PostgreSQL and Oracle simultaneously.
Disadvantages:
- The inconvenience of defining the database module directly (a one-time initial task).
Actual TypeORM Implementation: Defining the Database Module
Section titled “Actual TypeORM Implementation: Defining the Database Module”Directory Structure
Section titled “Directory Structure”Directorysrc/
Directorydatabase/
Directorypostgresql/
- postgresql.module.ts
- postgresql.providers.ts
Directoryapi/
Directoryv1/
Directoryusers/
Directoryentities/
- users.entity.ts
- users.providers.ts
- users.controller.ts
- users.module.ts
- users.service.ts
postgresql.providers.ts
Section titled “postgresql.providers.ts”import { DataSource } from 'typeorm';
export const postgresqlProviders = [ { provide: 'DATA_SOURCE', useFactory: async () => { const dataSource = new DataSource({ type: 'postgres', host: process.env.DATABASE_HOST, port: Number(process.env.DATABASE_PORT), username: process.env.DATABASE_USERNAME, password: process.env.DATABASE_PASSWORD, database: 'postgres', schema: process.env.SCHEMA_NAME, entities: ['dist/**/*.entity{.ts,.js}'], synchronize: Boolean(process.env.DATABASE_SYNCHRONIZE), // false in production });
return dataSource.initialize(); }, },];Key points:
provide: 'DATA_SOURCE': inject it into other modules using this name.- Define useFactory as async: an async factory is needed to use
process.envenvironment variables. - Use synchronize only in development: use a migration tool in production.
postgresql.module.ts
Section titled “postgresql.module.ts”import { Module } from '@nestjs/common';import { postgresqlProviders } from './postgresql.providers';
@Module({ providers: [...postgresqlProviders], exports: [...postgresqlProviders],})export class PostgresqlModule {}Import this module into each resource module to use TypeORM repositories.
Defining Entities and Repositories
Section titled “Defining Entities and Repositories”users.entity.ts
Section titled “users.entity.ts”import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('users')export class User { @PrimaryGeneratedColumn() id: number;
@Column({ name: 'userid', length: 255 }) userId: string;
@Column({ length: 255 }) password: string;
@Column({ name: 'firstname', length: 255 }) firstName: string;
@Column({ name: 'lastname', length: 255 }) lastName: string;
@Column({ length: 255, nullable: true }) email: string;}Because this uses the Data Mapper approach, the entity defines only the data structure. The repository provides CRUD methods.
users.providers.ts
Section titled “users.providers.ts”import { DataSource } from 'typeorm';import { User } from './users.entity';
export const userProviders = [ { provide: 'USER_REPOSITORY', useFactory: (dataSource: DataSource) => dataSource.getRepository(User), inject: ['DATA_SOURCE'], },];It injects DATA_SOURCE to create USER_REPOSITORY.
users.module.ts
Section titled “users.module.ts”import { Module } from '@nestjs/common';import { PostgresqlModule } from 'src/database/postgresql/postgresql.module';import { userProviders } from './entities/users.providers';import { UsersController } from './users.controller';import { UsersService } from './users.service';
@Module({ imports: [PostgresqlModule], controllers: [UsersController], providers: [...userProviders, UsersService],})export class UsersModule {}users.service.ts: Using the Repository
Section titled “users.service.ts: Using the Repository”import { Inject, Injectable } from '@nestjs/common';import { Repository } from 'typeorm';import { User } from './entities/users.entity';
@Injectable()export class UsersService { constructor( @Inject('USER_REPOSITORY') private userRepository: Repository<User>, ) {}
async findAll(): Promise<User[]> { return this.userRepository.find(); }
async create(userId: string, password: string): Promise<User> { const user = new User(); user.userId = userId; user.password = password; return this.userRepository.save(user); }
async rawQuery(sql: string, parameters?: any[]): Promise<any> { return this.userRepository.query(sql, parameters); }}By injecting the repository with @Inject('USER_REPOSITORY'), you can execute both CRUD operations and Raw SQL.
Practical Application Principles
Section titled “Practical Application Principles”- Use pure
typeormwithout@nestjs/typeorm: reduce version conflicts and dependencies. - Use the Data Mapper pattern: Active Record is useful only for small projects.
- Use synchronize only in development: manage migration scripts separately in production.
- Use Repository.query() for Raw SQL: execute SQL directly when you need to leave ORM abstraction behind.
Next Steps
Section titled “Next Steps”Patterns for using Redis as a cache and queue on top of this ORM structure are covered in NestJS Redis Patterns. The implementation of Web Push Notifications continues in NestJS Web Push Notification.