Skip to the page

Migrating · Schema translation

Translating a Convex schema

Draft a Napalm schema from convex/schema.ts. Each v.id becomes a link, each array of ids becomes a multi link and each union of literals becomes an enum.

API
—
In the schema
—

The command#

Name convex/schema.ts or the convex folder. The command reads the file without running any code. A field that names a const takes the const’s validator.

Static validator translation

napalm schema draft parses defineSchema, defineTable and shared v.* constants straight from convex/schema.ts without running code.

Links ready for deployed queries

v.id fields and arrays become typed object links and reverse backlinks that deployed EdgeQL queries traverse in one round trip.

Terminal
# The schema filenapalm schema draft --from convex/schema.ts # Or the convex foldernapalm schema draft --from convex

A sample project#

Here is a small project tracker as a Convex schema. Three tables hold users, projects and tasks, and a list of ids holds the members of each project.

convex/schema.ts
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' }),})

The command writes a type for each table.

dbschema/default.gel
# 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, the enum and the search index follow the rules under Additions in every draft.

Tables, fields and ids#

A Convex schema has no foreign keys and no join tables, because ids and lists of ids do that job. Field names stay the same in the draft.

ConvexIn the draftNote
users: defineTable(…)type UserNoteThe type takes the table’s name in the singular.
ownerId: v.id('users')required owner: UserNoteThe link drops Id from the field’s name.
memberIds: v.array(v.id(…))multi members: UserNoteAn array of ids becomes a link to many.
const role = v.union(v.literal(…), …)scalar type Role extending enum<…>NoteA union of literals becomes an enum, and a shared const produces one enum reused across referencing fields.
_idold_id: strNoteThe move keeps each document’s id in old_id.
_creationTimerequired created: datetimeNoteThe move sets the time from the document.
v.optional(…)notes: strNoteA property is optional unless marked required. A union with v.null() is optional too.
.searchIndex(…)index fts::index on (…)NoteThe search index becomes the type’s text search index.

Untranslated items#

A comment that starts with draft: marks each item below. Password hashes and vectors also stay notes, as Untranslated items explains.

Objects
A nested v.object, a list of objects or a union of object shapes becomes json. A separate type behind a link keeps the fields typed.
Mixed values
A union of different value types becomes json, and so does a list that mixes value types.
Document shapes
A table of several document shapes becomes one type with every shape’s fields, marking each field optional unless every shape includes it. Defining one subtype per shape under a shared abstract type keeps each shape distinct.
File storage
A field of _storage ids keeps the ids as str values. In Napalm each file is a record of a file type, as Files & object storage explains.
System tables
An id from another Convex system table stays a str.
Other files
The draft reads schema.ts alone. Convex Auth’s authTables and any other table or field spread in from another file stay notes.

Type a word or two to search every docs page.