1001Ferramentas
Generators

Prisma Model Generator from Table

Generates a Prisma model block (schema.prisma) with id, name, email and timestamps from a table name.

Prisma schema: declarative models, relations, and migrations

The Prisma schema is a single .prisma file written in a purpose-built declarative DSL. Unlike imperative ORMs where models live in TypeScript classes or JavaScript objects, Prisma puts the database shape into a dedicated file and uses it as the single source of truth: prisma generate turns it into a fully typed client and prisma migrate turns schema changes into SQL DDL. The trade-off is a new mini-language to learn — the upside is unbeatable type safety and a workflow that scales from solo projects to large teams.

This generator scaffolds the basic structure for a model; the guide below covers the full attribute catalogue, scalar types, relations, the generator pipeline, and how Prisma stacks up against TypeORM, Sequelize, and Drizzle.

Anatomy of a schema

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
}

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

Three blocks dominate every schema: datasource (connection + provider), generator (what artifacts Prisma emits — typically the JS client), and one or more model declarations. The trailing ? marks a field optional; arrays denote 1:N relations from the parent side.

Field attributes, scalar types, defaults

Field attributes: @id, @unique, @default(...), @updatedAt, @map("col_name") (rename in DB), @relation(fields, references) (foreign key). Block attributes: @@id([cols]) for composite primary keys, @@unique, @@index, @@map("table_name"). Scalar types: String, Int, BigInt, Float, Decimal, Boolean, DateTime, Json, Bytes, plus user-defined enum. Default generators: autoincrement(), now(), uuid(), cuid(), and dbgenerated("expr") for SQL-native defaults like sequences or computed columns.

Relations: 1:1, 1:N, N:N

Prisma supports the three classical cardinalities. 1:1 requires @relation on the owning side plus a @unique foreign key. 1:N is implicit: declare the array on the parent and the FK on the child. N:N comes in two flavours: implicit (Prisma manages a hidden junction table) or explicit (you model the junction yourself, which lets you add columns to it).

// implicit N:N
model Article { tags Tag[] }
model Tag     { articles Article[] }

// explicit N:N — needed when the join carries data
model ArticleTag {
  article    Article @relation(fields: [articleId], references: [id])
  articleId  Int
  tag        Tag     @relation(fields: [tagId], references: [id])
  tagId      Int
  addedAt    DateTime @default(now())
  @@id([articleId, tagId])
}

Generator, migrations, ecosystem

  • Generator pipeline: prisma generate reads the schema and emits a typed Client into node_modules/@prisma/client. Re-run after every schema edit.
  • Migrations: prisma migrate dev diffs the schema against the database, writes a SQL migration file, and applies it. The schema is the source of truth — never edit the DB directly.
  • Prisma Studio: a built-in GUI (prisma studio) for browsing data — handy for QA without writing queries.
  • Database support: PostgreSQL, MySQL, SQLite, SQL Server, CockroachDB, and MongoDB (still maturing). Multi-schema is PostgreSQL-only at the moment.
  • vs TypeORM: Prisma is declarative and schema-first; TypeORM is imperative and class-first. Prisma's type inference is sharper out of the box.
  • vs Drizzle: Drizzle is lighter and stays closer to SQL — no generator step, no proprietary DSL. Prisma offers a higher-level abstraction with sharper DX; Drizzle wins on bundle size and SQL transparency.
  • Performance: the historical Rust-based query engine has been rewritten in TypeScript in recent releases, removing the binary dependency.

FAQ

Does Prisma replace traditional ORMs? For most modern TypeScript projects, yes — it covers schema, migrations, and a typed client in one tool. Legacy projects with complex stored procedures or vendor-specific SQL may still prefer raw queries or TypeORM's flexibility.

Is the client fully type-safe? Yes. Every model, field, and relation becomes part of the TypeScript types, so misspelled fields, wrong types, and broken includes are caught at compile time. Autocomplete in the IDE follows the schema in real time.

Is the learning curve high? Lower than TypeORM or Sequelize. The Prisma DSL is small and self-contained; the docs are good; and most teams are productive within a day. The hard part is unlearning imperative ORM habits.

What about advanced SQL features? Prisma covers common cases; for window functions, recursive CTEs, or vendor extensions, fall back to prisma.$queryRaw (tagged template SQL) or $executeRaw for DDL. Most projects use raw queries in a handful of places and the typed client everywhere else.

Related Tools