Convex keeps its schema in code, in convex/schema.ts. It's a good format for an app and a hard one to see at a glance. Diagrams now read and write it, from any engine.
Export
Export → ORM now lists Convex beside Prisma, Drizzle and SQLAlchemy. The output is a schema.ts with a defineTable per table and validators for each column.
To get one, click Export in the editor's top bar (it's under the More actions menu on a phone). The Export schema window opens on a format select: pick Convex from the ORM / code group. The preview fills with the file, and the two buttons beside the select copy it or download it as schema.ts. Prisma and Drizzle only appear for engines they support; Convex is offered whatever engine the diagram uses, because nothing in the output depends on one.
A relational schema doesn't map onto Convex one for one, so a few rules decide the translation:
- A single-column primary key becomes the document's
_id, and a foreign key to it becomesv.id("table"). - A nullable column becomes
v.optional(...). - An enum becomes a union of literals, exported as a const so your functions can use it too.
- Tables outside the default schema are named
<schema>_<table>. - Indexes are written as
.index()calls.
What Convex has no place for (unique constraints, defaults, foreign key actions, composite keys, check constraints, RLS, triggers and views) is written as a comment next to the table, so nothing is dropped without a trace.
A worked example
Take a small PostgreSQL diagram with three tables and an enum:
teams:id uuidprimary key,name text not null,slug text not null unique.projects:id uuidprimary key,team_id uuid not nullreferencingteams.idwithON DELETE CASCADE,title text not null,status project_status not null default 'active',due_on date. It has an indexby_teamonteam_idand a checktitle_not_blankoflength(title) > 0.audit.events, in its own schema:id uuidprimary key,project_id uuid not nullreferencingprojects.id,payload jsonb not null,created_at timestamptz not null default now().- The enum
project_statushasactiveandarchived, and there's a viewactive_projects.
The Convex export is:
// Convex schema generated by Bright Web Data Modeller.
// Convex doesn't enforce uniqueness, defaults or referential actions: the comments note them to keep in your mutations.
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export const project_status = v.union(v.literal("active"), v.literal("archived"));
export default defineSchema({
// Table "audit.events" is named audit_events: Convex has no schemas and names tables with letters, digits and _.
audit_events: defineTable({
project_id: v.id("projects"),
payload: v.any(),
created_at: v.number(), // timestamptz: epoch milliseconds; default now()
}),
// Check title_not_blank: length(title) > 0
projects: defineTable({
team_id: v.id("teams"), // on delete cascade
title: v.string(),
status: project_status, // default 'active'
due_on: v.optional(v.number()), // date: epoch milliseconds
})
.index("by_team", ["team_id"]),
teams: defineTable({
name: v.string(),
slug: v.string(), // unique
}),
});
// View "active_projects" isn't written: Convex has no views; read it with a query.
A few things to notice. The id columns are gone, because each one is the document's _id. If a primary key had another name, a comment would say which column became _id. Tables are sorted by name, which is why audit_events comes first. Dates and timestamps become numbers, with a note that they hold epoch milliseconds. jsonb becomes v.any().
The comments are the part to act on. Convex won't stop two teams sharing a slug, won't fill in status, and won't delete a team's projects when the team goes. Those rules now belong in your mutations, and the comments list them where you'll see them.
How column types map
One table of types drives both directions, so what the export writes is what the import reads back:
| Column types | Validator | Read back as |
|---|---|---|
integers, serial, real, numeric, money, dates and timestamps | v.number() | double precision |
bigint, bigserial | v.int64() | bigint |
boolean, bit | v.boolean() | boolean |
bytea, blob, varbinary and the like | v.bytes() | bytea |
json, jsonb | v.any() | json |
everything else (text, varchar, uuid and so on) | v.string() | text |
An array column (text[]) is wrapped in v.array(...).
Import
Paste a Convex schema into Import and you get a diagram: v.id() references come back as foreign keys to the target's _id, literal unions as enums.
In the editor, open Import in the top bar and choose Convex schema under Add tables from. Paste the file into the Import Convex window. As you type, a line under the box counts what it found ("3 tables, 2 relationships, 1 enum, 2 indexes detected") and any warnings are listed above it. Tick Replace existing to swap out the branch's tables instead of adding to them, then click Import.
Every table gets an _id column as its primary key. A field written v.optional(x) or v.union(x, v.null()) comes in nullable. A union of string literals becomes an enum, named after its const if you declared one, or <table>_<field> if it was written inline. Objects, records and mixed unions become json columns.
Search indexes and vector indexes have no table equivalent, so the import lists them as warnings rather than guessing. Nested object fields come in as one json column, and an index on a field inside one is skipped with a warning. Given this schema:
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export const role = v.union(v.literal("owner"), v.literal("member"));
export default defineSchema({
users: defineTable({
name: v.string(),
email: v.string(),
profile: v.object({ bio: v.string(), avatarUrl: v.optional(v.string()) }),
}).index("by_email", ["email"]),
memberships: defineTable({
userId: v.id("users"),
role: role,
joinedAt: v.number(),
}).index("by_user", ["userId"]),
messages: defineTable({
authorId: v.id("users"),
body: v.string(),
embedding: v.array(v.float64()),
})
.searchIndex("search_body", { searchField: "body" })
.vectorIndex("by_embedding", { vectorField: "embedding", dimensions: 1536 }),
});
you get three tables, two relationships to users._id, an enum role, the two .index() indexes, profile as a json column and embedding as double precision[]. The warnings read:
messages: searchIndex("search_body") skipped: only .index() maps to a database index.
messages: vectorIndex("by_embedding") skipped: only .index() maps to a database index.
A v.id() that names a table the schema doesn't define is kept as plain text with a warning, except Convex's own tables such as _storage, which are left quietly as text.
Check out from a repository
A repository with a convex/schema.ts can now be checked out like any migrations folder. Link it, pick a commit, and the branch shows that commit's schema. Track a branch and you'll see when the schema file changes upstream, then review the difference before pulling it in.
The app picks the format from the files it finds. A repository holding convex/schema.ts is read with the Convex schema (convex/schema.ts) preset, which is checked before Drizzle's so the same file isn't mistaken for a Drizzle schema. Nothing is replayed: the file is parsed exactly as a paste would be, so the same warnings apply.
To set one up, open the Branches panel and choose New branch from a Git commit…, or start a fresh diagram from New diagram → Import from a repository.
When you'd use it
If you're moving an app from a SQL database to Convex, import the database (or its SQL) into a diagram and export it as Convex. You get a first schema.ts and, more usefully, a list of every rule you'll now enforce in code.
If you already run on Convex, the import is a quick way to see a large schema.ts as tables and arrows, and the repository checkout keeps that picture in step with the code.
Because the export works from any engine, this is also a way to sketch a Convex app's data model with the same tools as a SQL one: branches, reviewed merges and comments on the tables.