ORMs in Node
An ORM (object-relational mapper) lets you work with database rows as objects and methods instead of writing every SQL string. In Node the common names are Prisma, Sequelize, TypeORM, Drizzle, and Knex (closer to a query builder).
Detailed Explanation
Without an ORM you pass SQL to pg.query. With an ORM you write prisma.user.findMany({ where: { role: "admin" } }) and the library generates SQL, maps rows to objects, and (sometimes) tracks relations.
Prisma is schema-first: a schema.prisma file, a generated client, migrations. Sequelize and TypeORM are model-first: classes with decorators or define. Drizzle is TypeScript-first SQL-like. Mongoose is an ODM for Mongo, not SQL, but the idea (documents as objects) is the same.
What you gain: less boilerplate, typed results (Prisma/Drizzle), migrations, and a single place for relation loading (include). What you lose: control of the exact SQL, and a learning curve for N+1 (await user.orders in a loop fires one query per user). Good ORMs have include / join / with to batch. You still need to know SQL to debug EXPLAIN and to write the 5% of queries the ORM makes ugly.
An ORM is not a security boundary. Concatenating strings into prisma.$queryRawUnsafe is still injection. Parameterised APIs ($queryRaw tagged templates, bound params) are safe.
What it costs you: cold start (generate clients), leaky abstractions (a save() that writes 20 columns), and upgrades that rewrite query shapes. Small scripts can stay on pg plus a query builder.
Reach for an ORM when the app has many models and relations. Reach for a query builder when you want composable SQL without a full object graph. Reach for raw SQL for reporting and lock-sensitive updates.
Generated types are only as good as the schema they came from. A column you added by hand in staging and forgot to put in schema.prisma will exist in the database and not on the client. Treat the schema file as the source of truth and run migrations in CI against an empty database so drift fails the build.
Soft deletes (deletedAt) need to be in every default where or they leak into lists. Hooks that add that filter are easy to skip on raw queries. Document the rule next to the model.
Code Examples
Prisma-shaped (illustrative)
// Prisma-shaped (illustrative)
const admins = await prisma.user.findMany({
where: { role: "admin" },
select: { id: true, email: true },
});Raw, parameterised
// Raw, parameterised
const { rows } = await pool.query(
"SELECT id, email FROM users WHERE role = $1",
["admin"],
);N+1: avoid
// N+1: avoid
for (const user of users) {
user.orders = await prisma.order.findMany({ where: { userId: user.id } });
}
// Batch
const usersWithOrders = await prisma.user.findMany({ include: { orders: true } });Common Gotchas
Relations loaded lazily in a loop are N+1
use `include` / join, or one query for all child ids.
`$queryRawUnsafe` / string SQL is injection
use bound parameters or tagged raw helpers.
Migrations are part of the ORM story
deploying code without migrating crashes at query time.