Migrating · Schema translation
Translating a Prisma schema
Draft a Napalm schema from a schema.prisma file or a folder of .prisma files. The draft keeps the names of the models, the relation fields and the enum values.
- API
- —
- In the schema
- —
The command#
Name the schema.prisma file or the folder of a schema split across files. In a folder, the command reads every .prisma file.
Single-file or multi-file Prisma schemas
napalm schema draft reads .prisma models, enums and @updatedAt attributes directly from disk, including MongoDB Prisma schemas.
Bidirectional model relations
Forward @relation fields become typed links, opposite-side model arrays become .<link[is Model] backlinks and implicit join tables become multi links.
# A schema in one filenapalm schema draft --from prisma/schema.prisma # A schema split across files: name the foldernapalm schema draft --from prisma/schemaA sample project#
Here is a small project tracker as a Prisma schema. Three models hold users, projects and tasks, and a many-to-many relation holds the members of each project.
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")}The command writes a type for each 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 shared type, the reverse links, the rewrite and the enum follow the rules under Additions in every draft.
Models, fields and relations#
A Prisma schema names models, relations and enum values, and the draft keeps those names.
| Prisma | In the draft | Note |
|---|---|---|
model User | type User | NoteThe type keeps the model’s name. |
@@map("users") | # Stored in the users table. | NoteA comment names the table that holds the rows. |
owner User @relation(…) | required owner: User | NoteThe link keeps the relation field’s name. |
ownedProjects Project[] | ownedProjects := .<owner[is Project] | NoteThe field on the other side names the reverse link. |
members User[] | multi members: User | NoteA many-to-many relation becomes a link to many. A comment names Prisma’s table and the A and B columns. |
@updatedAt | rewrite update using (…) | NoteUpdates set the timestamp automatically unless the statement supplies an explicit value. |
@default(cuid()) | old_id: str | NoteEach id becomes old_id. |
MEMBER @map("member") | enum<MEMBER, ADMIN> | NoteThe enum keeps the Prisma names. A comment lists the stored values. |
A MongoDB schema drafts the same way. Two lists of ids that name each other become one multi link.
Untranslated items#
A comment that starts with draft: marks each item below. Password hashes and vectors also stay notes, as Untranslated items explains.
- Unsupported types
- A field of type
Unsupported("…")takes the Postgres type in the quotes. A type with no match in Napalm stays a note. - Composite types
- A MongoDB field of a composite type stays a note.
- Full-text indexes
- A
@@fulltextindex stays a note. Text search in Napalm is anfts::indexon the type. - Relations without ids
- A many-to-many relation needs a one-field id on both models. Without both ids, the relation stays a note.
- Polymorphic pairs
- A
commentableTypefield besidecommentableIdpoints at several models. The note names the link and the abstract type to write.