Type-safe database access—schema, migrations, and queries with Prisma.
Prisma gives you a type-safe client and declarative schema. Here’s how to use it in Node and TypeScript.
Prisma and database
Setup
Schema — Define models in schema.prisma: datasource (DB URL), generator (client), and models (tables). Use prisma migrate dev to create and apply migrations.
Client — import { PrismaClient } from '@prisma/client'. Instantiate once (singleton in dev to avoid many connections). Use in services or API routes.
Types — Prisma generates TypeScript types from the schema. Your queries are typed; autocomplete and refactors are reliable.
Queries
CRUD — prisma.user.findMany(), findUnique(), create(), update(), delete(). Use where, select, include to shape results.
Relations — include: { posts: true } for nested data. Avoid N+1 by including what you need in one query.
Transactions — prisma.$transaction([...]) for multiple operations atomically. Use interactive transactions when you need conditional logic.
Raw — prisma.$queryRaw when you need SQL. Prefer the query builder for type safety and portability.
ORM preference (Node/TS backend):
ORM in Node/TypeScript
Prisma in practice:
Takeaway
Prisma is a strong default for Node + SQL. Use the schema as the source of truth; run migrations in CI. Rely on generated types and keep raw SQL for the rare cases that need it.