Migrating · Schema translation
Translating a Prisma schema
Draft a Napalm schema from schema.prisma or a directory of .prisma files while preserving model names, relation links and enum values.
- API
- —
- In the schema
- —
Running schema draft#
Pass schema.prisma or a directory holding a multi-file Prisma schema. When given a directory, napalm schema draft merges every .prisma file inside.
# Draft from a single Prisma schema filenapalm schema draft --from prisma/schema.prisma # Draft from a multi-file Prisma schema directorynapalm schema draft --from prisma/schemaThe command parses .prisma files statically without running Prisma CLI generators or connecting to your database, and supports both SQL and MongoDB datasources.
Direct Prisma parsing
The CLI reads single-file and multi-file Prisma schemas directly from disk.
Typed model relations
Relation fields become forward links, computed backlinks and join-free multi links.
Example project tracker#
The Prisma schema below models a small project tracker with users, projects, tasks and a many-to-many project membership relation.
enum Role { MEMBER @map("member") ADMIN @map("admin")} model User { id String @id @default(cuid()) email String @unique passwordHash String role Role @default(MEMBER) ownedProjects Project[] @relation("owner") projects Project[] @relation("members") createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("users")} model Project { id String @id @default(uuid()) @db.Uuid owner User @relation("owner", fields: [ownerId], references: [id]) ownerId String name String members User[] @relation("members") tasks Task[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("projects")} model Task { id Int @id @default(autoincrement()) project Project @relation(fields: [projectId], references: [id], onDelete: Cascade) projectId String @db.Uuid title String notes String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("tasks")}napalm schema draft generates an object type for each Prisma model.
# This first schema was drafted from schema.prisma by napalm schema draft. Read every line, then check the schema on a# branch with napalm schema push <project> --branch <branch> --plan.## Each model is a type with the same name, and field names stay the same. Each relation field is a link, and a# many-to-many relation is a multi link. The field on the other side of a relation is a computed link on the target# type. Three or more types that share properties extend one abstract type. old_id keeps each row's old key for the# move. A type with no access rule answers nothing. A comment that starts with draft: marks an item the draft couldn't# translate. using extension napalm;using future simple_scoping; module default { # The database stores MEMBER as member and ADMIN as admin. scalar type Role extending enum<MEMBER, ADMIN>; # The properties User, Project and Task share. abstract type Timestamped { required createdAt: datetime { default := datetime_current(); }; required updatedAt: datetime { default := datetime_of_statement(); rewrite update using (datetime_of_statement() if not __specified__.updatedAt else .updatedAt); }; } # Stored in the users table. type User extending Timestamped { required email: str { constraint exclusive; }; # draft: passwordHash stays behind, because a project can't take another system's password hashes. The guide at # https://napalmdata.com/developers/migrating/moving-users gives people their rows back. required role: Role { default := <Role>'MEMBER'; }; old_id: str { constraint exclusive; }; # The other side of each link to User. ownedProjects := .<owner[is Project]; projects := .<members[is Project]; } # Stored in the projects table. type Project extending Timestamped { required owner: User; required name: str; # Stored in Prisma's _members table. Column A holds a Project's id, and column B holds the id of a User in that # Project's members. multi members: User { on target delete allow; }; old_id: str { constraint exclusive; }; # The other side of each link to Project. tasks := .<project[is Task]; } # Stored in the tasks table. type Task extending Timestamped { required project: Project { on target delete delete source; }; required title: str; notes: str; old_id: str { constraint exclusive; }; }}The abstract base type, reverse links, timestamp rewrite and enum follow the rules in Generated schema constructs.
Models, fields and relations#
Prisma schemas already name models, relation fields and enum variants, and the draft preserves those names directly.
| Prisma | In the draft | Note |
|---|---|---|
model User | type User | NotePreserves the exact model name. |
@@map("users") | # Stored in the users table. | NoteRecords the underlying SQL table name in a comment for your import script. |
owner User @relation(…) | required owner: User | NotePreserves the relation field name. |
ownedProjects Project[] | ownedProjects := .<owner[is Project] | NoteUses the opposite relation field name for the computed backlink. |
members User[] | multi members: User | NoteConverts implicit many-to-many relations into multi links and notes Prisma’s join table and A and B columns. |
@updatedAt | rewrite update using (…) | NoteStamps the statement time on every update unless an explicit value is supplied. |
@default(cuid()) | old_id: str | NoteStores the original Prisma ID in old_id during migration. |
MEMBER @map("member") | enum<MEMBER, ADMIN> | NoteKeeps the Prisma variant names and records mapped database values in a comment. |
MongoDB Prisma schemas translate the same way and collapse paired ID-array relations into a single multi link.
Untranslated items#
The command marks each untranslated Prisma construct below with a # draft: comment. Untranslated items covers password hash and vector fields, which emit notes across all sources.
- Unsupported types
- Fields declared as
Unsupported("…")unwrap the inner Postgres type when supported and emit a# draft:note otherwise. - Composite types
- MongoDB composite
typeblocks emit a# draft:note so you can convert each block into a linked object type orjson. - Full-text indexes
- Prisma
@@fulltextattributes emit a note. Full-text search in Napalm uses a declarativefts::indexon the type. - Composite-key relations
- Implicit many-to-many relations require a single-field
@idon both models and emit a note when a model uses a composite key. - Polymorphic pairs
- Paired fields like
commentableTypeandcommentableIdemit a note suggesting a link to a shared abstract base type.