Migrating · Schema translation
Translating a Convex schema
Draft a Napalm schema from convex/schema.ts. Validator IDs become typed links, ID arrays become multi links and literal unions become enums.
- API
- —
- In the schema
- —
Running schema draft#
Pass convex/schema.ts or the convex directory to napalm schema draft.
# Draft directly from convex/schema.tsnapalm schema draft --from convex/schema.ts # Or pass the convex directorynapalm schema draft --from convexThe command parses schema.ts statically without running TypeScript or Node, and resolves top-level const validators shared across tables.
Static validator parsing
The CLI reads Convex table definitions and shared validators directly from disk.
Typed document links
Document ID fields and ID arrays become direct links and computed backlinks.
Example project tracker#
The Convex schema below models a small project tracker with users, projects, tasks and an array of member IDs on each project.
import { defineSchema, defineTable } from 'convex/server'import { v } from 'convex/values' const role = v.union(v.literal('member'), v.literal('admin')) export default defineSchema({ users: defineTable({ tokenIdentifier: v.string(), email: v.string(), role, }).index('by_token', ['tokenIdentifier']), projects: defineTable({ ownerId: v.id('users'), name: v.string(), memberIds: v.array(v.id('users')), }).index('by_owner', ['ownerId']), tasks: defineTable({ projectId: v.id('projects'), title: v.string(), notes: v.optional(v.string()), updatedAt: v.number(), }) .index('by_project', ['projectId']) .searchIndex('search_title', { searchField: 'title' }),})napalm schema draft generates an object type for each Convex table.
# This first schema was drafted from schema.ts by napalm schema draft. Read every line, then check the schema on a# branch with napalm schema push <project> --branch <branch> --plan.## Each table is a type, and field names stay the same. Each v.id is a link, and an array of ids is a multi link. Each# link has a computed link back on the target type. _creationTime is the created property. 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 { # Drafted from the role validator. scalar type Role extending enum<member, admin>; type User { required tokenIdentifier: str; required email: str; required role: Role; required created: datetime { default := datetime_current(); }; old_id: str { constraint exclusive; }; # The other side of each link to User. projects_as_owner := .<owner[is Project]; projects_as_members := .<members[is Project]; index on (.tokenIdentifier); } type Project { required owner: User; required name: str; required created: datetime { default := datetime_current(); }; # Drafted from the projects.memberIds list of ids. multi members: User { on target delete allow; }; old_id: str { constraint exclusive; }; # The other side of each link to Project. tasks := .<project[is Task]; } type Task { required project: Project; required title: str; notes: str; required updatedAt: float64; required created: datetime { default := datetime_current(); }; old_id: str { constraint exclusive; }; # Drafted from the search_title search index. index fts::index on (fts::with_options(.title, language := fts::Language.eng)); }}The reverse links, enum and full-text search index follow the rules in Generated schema constructs.
Tables, fields and IDs#
Convex schemas express relationships with typed document IDs and ID arrays instead of SQL foreign keys or join tables. Scalar field names stay unchanged in the draft.
| Convex | In the draft | Note |
|---|---|---|
users: defineTable(…) | type User | NoteUses the singular PascalCase form of the table name. |
ownerId: v.id('users') | required owner: User | NoteStrips the Id suffix when naming the object link. |
memberIds: v.array(v.id(…)) | multi members: User | NoteConverts an array of document IDs into a multi link. |
const role = v.union(v.literal(…), …) | scalar type Role extending enum<…> | NoteConverts literal unions into enums and shares one enum when multiple fields reference the same const. |
_id | old_id: str | NoteStores each Convex document ID in old_id during migration. |
_creationTime | required created: datetime | NotePreserves the original creation timestamp during import. |
v.optional(…) | notes: str | NoteOmits required for optional fields and unions that include v.null(). |
.searchIndex(…) | index fts::index on (…) | NoteConverts the table’s search index into a full-text search index. |
Untranslated items#
The command marks each untranslated Convex construct below with a # draft: comment. Untranslated items covers password hash fields and vector indexes, which emit notes across all sources.
- Nested objects
- Nested
v.objectfields, arrays of objects and unions of object shapes map tojson. Extract a linked object type when you want strict schema validation on nested fields. - Mixed scalar unions
- Unions or arrays mixing distinct scalar types map to
json. - Polymorphic tables
- A table defined as a union of document shapes becomes one type with every shape’s fields and marks non-shared fields optional. Split those shapes into subtypes under an abstract base type when you want strict per-variant fields.
- File storage IDs
- Fields holding
_storageIDs becomestrproperties. In Napalm, files use dedicatedext::files::Filetypes. Files & object storage covers file schemas and imports. - System table IDs
- References to other Convex system tables become
strproperties. - Imported table spreads
- Because the parser reads
schema.tsstandalone, tables or fields spread in from external modules such as Convex Auth’sauthTablesemit# draft:notes.