Learning objectives
After reading this article you will be able to:
- Understand what an ORM solves and what it hides
- Compare Prisma, Drizzle, TypeORM and Sequelize on objective criteria
- Pick between an ORM, a query builder and raw SQL for your scenario
Almost every Node.js project that talks to a relational database hits the same question: write SQL by hand or use an ORM? And if an ORM, which one? Four names dominate the searches: Prisma, Drizzle, TypeORM and Sequelize. They solve the same problem with very different philosophies, and the choice shapes how you define tables, write queries and ship migrations.
This guide compares the four from the point of view of someone writing TypeScript or JavaScript, with the same example model in each, and shows when none of them is needed. If you have not picked the database yet, start with the best database for Node.js.
What does an ORM do (and what does it hide)?#
An ORM (object-relational mapper) is a library that maps database tables and rows to objects and types in your language, and builds the SQL from method calls. Instead of SELECT ... WHERE email = $1, you call something like findOne({ email }).
The gains are real: types that follow the schema, relations loaded without hand-written JOINs, versioned migrations and parameterized queries by default, which closes the door on SQL injection for most of your code.
The cost is that the ORM hides the SQL that runs, and that is where performance problems start: the classic N+1 (one query for the list, then one more per item), columns loaded for nothing and queries that skip your indexes. Every serious ORM shows the generated SQL and accepts raw SQL when the API falls short.
Between the bare driver and the ORM sits the query builder: an API that builds SQL from method calls without mapping objects or handling relations for you. Knex and Kysely are the best-known examples.
Which criteria matter?#
- Where the schema lives. In its own file (Prisma), in TypeScript code (Drizzle), in decorated classes (TypeORM) or in model definitions (Sequelize).
- Distance from SQL. The closer to SQL, the easier it is to predict the query that runs. The further away, the less code for common tasks.
- Types. In TypeScript, a query result should have the right type with no manual annotation.
- Migrations. Generating, reviewing and applying schema changes during a deploy, as covered in database migrations on deploy.
- Supported databases. All four cover PostgreSQL and MySQL; MongoDB is where they differ most.
- Maintenance. Recent releases and a clear path between major versions.
When should you pick Prisma?#
Prisma starts from a schema file written in its own language. You describe models and relations, and Prisma derives the TypeScript types, the query API and the migrations from it:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
orders Order[]
}
model Order {
id Int @id @default(autoincrement())
total Int
userId Int
user User @relation(fields: [userId], references: [id])
}After you change the schema, a Prisma command regenerates the types your code imports. In production, that step has to run before the app starts, or the generated files have to ship with the code.
Pros: the schema doubles as readable documentation of the database, types come ready, the CLI handles migrations and the docs are extensive.
Cons: the schema language is one more thing to learn, and queries outside what the API covers need raw SQL. Prisma also changes a lot between major versions: the official migration guides show changes to the schema file's name, the generation command and the shape of queries, and MongoDB support varies from one major to the next. Install the major explicitly (npm install prisma@<major>), check in package.json what you got and follow that major's docs. The example above uses the stable line's schema syntax.
When should you pick Drizzle?#
Drizzle defines the schema in plain TypeScript and queries through an API that mirrors SQL. Types come from the schema itself, so there is no generation step. It also offers a relational API to load records together with their relations:
// npm install drizzle-orm pg
// npm install -D drizzle-kit @types/pg
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/node-postgres";
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
email: varchar({ length: 255 }).notNull().unique(),
name: varchar({ length: 255 }),
});
const db = drizzle(process.env.DATABASE_URL!);
const [user] = await db
.select()
.from(users)
.where(eq(users.email, "ana@example.com"));
console.log(user);Migrations belong to drizzle-kit: generate writes SQL files from the schema, migrate applies the pending ones and push pushes the schema straight to the database, which is handy in development.
Pros: anyone who knows SQL can read a query and know what will run; the docs claim zero dependencies; it supports PostgreSQL, MySQL and SQLite, among other databases listed in its docs.
Cons: you need to know SQL, because the API does not hide the relational model. The project ships releases often and adjusts API details between them, so pin the version and read the release notes when you upgrade. It does not support MongoDB.
Are TypeORM and Sequelize still worth it?#
TypeORM#
TypeORM describes tables as decorated classes and supports both the Active Record and Data Mapper patterns. According to its README, it supports more databases than any other JavaScript or TypeScript ORM, including MongoDB, Oracle, SQL Server and SAP HANA. The project keeps shipping regular releases.
// npm install typeorm reflect-metadata pg
import "reflect-metadata";
import { Column, DataSource, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity("users")
export class User {
@PrimaryGeneratedColumn()
id!: number;
@Column({ type: "varchar", unique: true })
email!: string;
@Column({ type: "varchar", nullable: true })
name!: string | null;
}
const dataSource = new DataSource({
type: "postgres",
url: process.env.DATABASE_URL,
entities: [User],
synchronize: false, // use migrations in production
});
await dataSource.initialize();
const user = await dataSource
.getRepository(User)
.findOneBy({ email: "ana@example.com" });
console.log(user);The docs ask for experimentalDecorators and emitDecoratorMetadata in tsconfig.json, plus the reflect-metadata package. Decorator metadata comes from the TypeScript compiler, and not every tool that runs .ts files emits it: see how to run TypeScript in production. Declaring each column's type, as above, keeps the model explicit and saves TypeORM from needing that metadata to infer the type; without it, a column with no type fails with Column type for User#email is not defined and cannot be guessed. And turn synchronize off in production: the docs themselves say it is typically unsafe once the database holds data.
Sequelize#
Sequelize is one of the oldest Node.js ORMs, designed for JavaScript first. You define models with sequelize.define or with classes and query with option objects (where, include). For PostgreSQL it needs the pg and pg-hstore packages:
// npm install sequelize pg pg-hstore
import { DataTypes, Sequelize } from "sequelize";
const sequelize = new Sequelize(process.env.DATABASE_URL, { logging: false });
const User = sequelize.define(
"User",
{
email: { type: DataTypes.STRING, allowNull: false, unique: true },
name: DataTypes.STRING,
},
{ tableName: "users" },
);
const user = await User.findOne({ where: { email: "ana@example.com" } });
console.log(user?.toJSON());The README labels version 6 as stable and version 7 as alpha, asks for new maintainers to finish the next major and lists better TypeScript support among the open work. For an existing project that works, there is no rush to migrate; for a new TypeScript project, the other options weigh more.
When is raw SQL or Knex enough?#
If the project has a handful of tables and predictable queries, the driver is enough. With pg, a pool and parameters ($1) give you safety and speed with no extra layer:
import pg from "pg";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const { rows } = await pool.query(
"SELECT id, email, name FROM users WHERE email = $1",
["ana@example.com"],
);
console.log(rows[0]);When queries become dynamic (optional filters, user-chosen sorting), a query builder saves you from concatenating strings. Knex works in JavaScript, ships migrations and supports PostgreSQL, MySQL, SQLite, SQL Server and more; its docs call the typings "best-effort". Kysely is a query builder built for TypeScript, with types at every step of the query. The connection walkthrough is in how to connect PostgreSQL to Node.js.
For MongoDB, the equivalent layer is Mongoose, which adds schemas and validation on top of the official driver, as shown in how to connect MongoDB to Node.js.
Which one fits each scenario?#
| Criterion | Prisma | Drizzle | TypeORM | Sequelize |
|---|---|---|---|---|
| Schema | its own file | TypeScript code | decorated classes | JS/TS models |
| Queries | its own API | SQL-like, plus a relational API | repository, Active Record or query builder | option objects |
| Type generation | yes | no | no | no |
| Migrations | Prisma CLI | drizzle-kit | TypeORM CLI | Sequelize CLI |
| MongoDB | varies by major | no | yes | no |
| Maintenance | active, with big changes between majors | active | active | v6 stable, v7 in alpha |
| Scenario | Recommendation |
|---|---|
| New TypeScript API, team comfortable with SQL | Drizzle |
| Team that wants a declarative schema as the source of truth | Prisma, with the major pinned |
| Team that prefers decorated classes | TypeORM |
| Existing JavaScript project on Sequelize | Stay on version 6 |
| Bot or script with few queries | pg or mysql2 driver with parameterized SQL |
| Dynamic queries without an ORM | Knex (JavaScript) or Kysely (TypeScript) |
| Data in MongoDB | Mongoose or the official driver |
Whatever you pick, three habits apply to all of them. Each ORM opens a connection pool, and the total is the pool size times the number of processes (the classic failure is covered in too many connections). Turn on SQL logging in development to catch N+1 early. And create indexes for the columns your filters use, because the ORM will not do it for you.
How to do this on Square Cloud#
Square Cloud offers PostgreSQL, MySQL, MongoDB and Redis hosting from the Standard plan (Hobby does not include databases). Any ORM above connects the same way: you create the database in the dashboard, copy the full connection URL into an environment variable such as DATABASE_URL and point the ORM at it. TLS is mandatory: the driver must trust the downloaded CA and, on PostgreSQL and MongoDB, also present the .pem certificate.
Each database accepts up to 256 connections, with no pooler in front. Your ORM's pool is what controls that number, so add up the pool of every process and every application that points at the same database.
There is no build step. On start, the platform runs npm install when there is no node_modules, with NODE_ENV=production: postinstall runs and devDependencies are left out. Prisma's generation step (prisma generate, or whichever command your major's docs name) goes in postinstall or in the start command, with the prisma package in dependencies. The same goes for drizzle-kit if migrations run on start: the start command runs as a shell command, so npm run migrate && npm start works.
Before a risky migration, take a manual snapshot of the database. It can be restored into the same database, another one of the same engine or a new database. See engines and plans on Databases.
Database hosting
MongoDB, PostgreSQL, MySQL and Redis next to your app
FAQs
What is the best ORM for Node.js?
There is no single best one. In TypeScript, Drizzle suits people who want to write close to SQL, and Prisma suits people who prefer to describe models in a schema and get types and migrations from it. TypeORM and Sequelize remain valid in projects that already use them.
Drizzle or Prisma?
Drizzle defines the schema in TypeScript and builds SQL-like queries, with no type generation step. Prisma uses its own schema file and generates types from it. Choose the style your team reads best, and pin Prisma's major version, since its workflow changes between majors.
Is Sequelize still maintained?
The project README labels version 6 as stable and version 7, published as @sequelize/core, as alpha, and asks for new maintainers to finish the next major. Before starting a new project on it, check the date of the latest release in the repository.
Do I need an ORM to use PostgreSQL with Node.js?
No. The pg driver with parameterized queries covers many projects, and a query builder such as Knex or Kysely helps with dynamic queries. An ORM pays off when you want types, relations and migrations in one place.
Can I use TypeORM synchronize or Sequelize sync in production?
It is not recommended. Both docs advise against syncing the schema automatically in production, because a model change can alter or drop data. Use migrations instead.
More in Databases
See all 32 articles- How much does database hosting cost?Cloud database hosting costs from zero to hundreds of dollars a month. See what sets the price: instance, disk, traffic, snapshots, and how to estimate yours.
- How to host a database for your appTo host a database, pick an engine, create the instance, store the connection string in an environment variable and test the connection. Step-by-step guide.
- What is Redis used for?Redis is an in-memory data store that keeps key-value pairs and rich data structures. Learn how it works, what it is used for and when not to use it.
- How to back up your databaseBack up MySQL, PostgreSQL and MongoDB with mysqldump, pg_dump and mongodump, then restore the dump. See the commands, how to verify and common mistakes.