ORM for JavaScript (Node.js): comparison and examples
When developing server-side applications on Node.js, interacting with a database is one of the key tasks. Object-Relational Mapping (ORM) allows you to work with relational databases through JavaScript objects, eliminating the need to write raw SQL. In this article, we will review three popular ORMs: Sequelize, TypeORM, and Prisma, compare their features, and provide practical examples.
1. Sequelize
Sequelize is one of the oldest and most mature ORMs for Node.js. It supports PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. It works with promises and async/await.
Installation and setup
npm install sequelize sequelize-clinpm install pg pg-hstore # for PostgreSQLModel and query example
const { Sequelize, DataTypes } = require('sequelize');const sequelize = new Sequelize('postgres://user:pass@localhost:5432/mydb');
// Defining a modelconst User = sequelize.define('User', { name: { type: DataTypes.STRING, allowNull: false }, email: { type: DataTypes.STRING, unique: true }});
// Creating a recordconst user = await User.create({ name: 'Ivan', email: 'ivan@example.com' });
// Searching with a filterconst users = await User.findAll({ where: { name: 'Ivan' } });
// Updatingawait User.update({ email: 'new@example.com' }, { where: { id: 1 } });Pros and cons
- + Large community, extensive documentation
- + Support for migrations and seeds
- - Verbose syntax for complex queries
- - No built-in TypeScript support
2. TypeORM
TypeORM is an ORM oriented towards TypeScript, but it also works with plain JavaScript. It supports decorators, Active Record, and Data Mapper. It works with most popular DBMS.
Installation and setup
npm install typeorm reflect-metadatanpm install pg # for PostgreSQLModel example with decorators
import { Entity, PrimaryGeneratedColumn, Column, createConnection } from 'typeorm';
@Entity()export class User { @PrimaryGeneratedColumn() id: number;
@Column() name: string;
@Column({ unique: true }) email: string;}
// Usageconst connection = await createConnection();const userRepo = connection.getRepository(User);const user = await userRepo.save({ name: 'Anna', email: 'anna@example.com' });const users = await userRepo.find({ where: { name: 'Anna' } });Pros and cons
- + Excellent TypeScript support
- + Two approaches: Active Record and Data Mapper
- - Complex configuration for beginners
- - Slow migrations in large projects
3. Prisma
Prisma is a modern ORM that uses its own schema language and generates a type-safe client. It is considered the most performant and developer-friendly.
Installation and setup
npm install @prisma/clientnpx prisma initSchema definition (schema.prisma)
model User { id Int @id @default(autoincrement()) name String email String @unique}Query example
import { PrismaClient } from '@prisma/client';const prisma = new PrismaClient();
// Creatingconst user = await prisma.user.create({ data: { name: 'Olga', email: 'olga@example.com' }});
// Searchingconst users = await prisma.user.findMany({ where: { name: 'Olga' } });
// Updatingawait prisma.user.update({ where: { id: 1 }, data: { email: 'new@example.com' }});Pros and cons
- + Full type safety