v(i)b(e)c(o)d(e)4u.com
Назад на форум vbcd4u.com
🔥 Нейросети2026-11-17T10:12:00.000Z▲ 0 upvotes

Prisma or Drizzle: scheme, migrations, types and serverless without fictitious benchmarks

@murdasoftРедакция vbcd4u · MurdaSoft
Открыть все обсуждения →

Without numbers from other people's benchmarks

ORM comparisons on the Internet usually show query and cold start times taken on someone else’s hardware, someone else’s circuit, and someone else’s versions. You cannot transfer them to your project: both libraries are actively changing, and in real workloads the time is usually determined by queries to the database, not the ORM. Therefore, below is a comparison of the device and workflow and a way to measure a cold start at home.

Scheme

Prisma is a separate DSL in the schema.prisma file from which the client is generated:

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  posts     Post[]
  createdAt DateTime @default(now()) @map("created_at")
  @@map("users")
}

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  authorId Int    @map("author_id")
  author   User   @relation(fields: [authorId], references: [id])
  @@index([authorId])
  @@map("posts")
}

Drizzle is a TypeScript schema, types are derived from it directly without a generation step:

// src/db/schema.ts
import { pgTable, serial, text, integer, timestamp, index } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  email: text("email").notNull().unique(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const posts = pgTable("posts", {
  id: serial("id").primaryKey(),
  title: text("title").notNull(),
  authorId: integer("author_id").notNull().references(() => users.id),
}, (t) => [index("posts_author_idx").on(t.authorId)]);

export type User = typeof users.$inferSelect;

The difference in practice: in Prisma, after any schema change you need prisma generate (usually in postinstall and in CI), otherwise the client types will not match the schema. In Drizzle, the types are updated along with the code, but the schema is more verbose.

Requests

// Prisma
const user = await prisma.user.findUnique({
  where: { email },
  include: { posts: { select: { id: true, title: true } } },
});

// Drizzle, SQL-подобный стиль
import { eq } from "drizzle-orm";
const rows = await db.select().from(users).where(eq(users.email, email));

// Drizzle, реляционный API (нужно описать relations для схемы)
const withPosts = await db.query.users.findFirst({
  where: eq(users.email, email),
  with: { posts: true },
});

Prisma hides SQL behind its own API - easier for a team with no SQL experience, but a complex query (window functions, CTE) goes into $queryRaw. Drizzle is closer to SQL: what is written will be executed, and for raw fragments there is a typed template sql.

Migrations

Prisma: prisma migrate dev compares the schema with the development base and creates an SQL migration to prisma/migrations, prisma migrate deploy applies it in production.

Drizzle: drizzle-kit generate creates an SQL file based on the difference between the schema and the previous snapshot, drizzle-kit migrate applies. For prototypes there is drizzle-kit push - applies the scheme directly without migration files; it is not used in production.

// drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/db/schema.ts",
  out: "./drizzle",
  dialect: "postgresql",
  dbCredentials: { url: process.env.DATABASE_URL! },
});

General rule for both: read the generated SQL before using it. The generator can turn renaming a column into deletion and creation, that is, into data loss. Drizzle Kit in such cases asks interactively whether this is a rename or a new column; in Prisma, renaming is done manually in the migration file.

Serverless and cold start

The cold start of a function is affected by the size of the bundle, initialization of the client, and establishment of a connection to the database.

  • Prisma historically used a separate query engine for Rust, which increased the deployment size and required the correct binary for the platform. In the latest versions, Prisma switches to an implementation without the Rust engine, to TypeScript with driver adapters. Check what mode your version has - both the size and support for Edge will depend on this.
  • Drizzle - a thin layer on top of the selected driver (pg, postgres, Neon serverless drivers and others), without a separate engine.

More important than ORM are connections. Each function instance opens its own connection, and when there is a surge in traffic, Postgres runs into max_connections. Use a pooler (PgBouncer, provider pooler) in transaction mode or provider HTTP drivers. Prisma with PgBouncer in transaction mode requires the pgbouncer=true parameter in the connection string in versions that use prepared expressions - check the documentation.

How to measure yourself

  1. 1.Deploy two minimal functions with the same request: one on Prisma, one on Drizzle, on the same provider and region, to the same database.
  2. 2.Call each after a period of inactivity to get a cold start, 20-30 times.
  3. 3.Look at the platform metrics: on AWS Lambda, the REPORT line in the logs contains Init Duration; other platforms have their own duration metrics.
  4. 4.Compare the median and the worst values, not just one good measurement.

This is an hour of work, and the result will be about your stack, not someone else's.

Common mistakes

  • New PrismaClient with each hot reload in next dev - hundreds of connections. Save the instance in globalThis in development mode.
  • drizzle-kit push against the production base.
  • Forgotten prisma generate in CI: the assembly crashes with a type error that does not exist locally.

What to choose

Prisma - when mature tools are important (Studio, clear migrations), a team of different levels and requests are mainly CRUD. Drizzle - when a team confidently writes SQL, you want to see every request and not have a generation step, and the project lives on serverless or Edge. Changing the ORM in a live project is expensive, so it is better to make the choice according to the convenience of the team, and not according to someone else’s measurements.

Обсуждение (0)

Ещё на vbcd4u.com