Tables
A Convex deployment stores documents in tables. A document is an object of Convex values: strings, numbers, booleans, null, 64-bit integers, bytes, arrays and nested objects. Convex adds two fields to every document: _id, its ID, and _creationTime.
A table has a schema, which Convex checks every write against, and indexes. An index keeps the table’s documents sorted by the fields you list, then by creation time, so a query that looks documents up by those fields reads only the ones it returns.
| channel | _creationTime | _id | body |
|---|---|---|---|
| general | 1764000000100 | j57a… | hello |
| general | 1764000000200 | j57b… | hi there |
| random | 1764000000150 | j57c… | lunch? |
| support | 1764000000300 | j57d… | help |
Declare a table
Section titled “Declare a table”Describe the documents with an Effect Schema, and the indexes with Convex’s own builder:
import * as Convex from "@alchemy.run/convex/Server";import * as Schema from "effect/Schema";
export const Messages = Convex.Table( "messages", Schema.Struct({ channel: Schema.String, body: Schema.NonEmptyString, sentAt: Schema.DateFromMillis, }), (table) => table.index("by_channel", ["channel"]),);table is Convex’s defineTable result, so search indexes, vector indexes and staged indexes work the same way.
Code that Convex bundles imports @alchemy.run/convex/Server, which leaves out everything that only runs at deploy.
Put it in the schema
Section titled “Put it in the schema”convex/schema.ts exports the schema Convex pushes:
import * as Convex from "@alchemy.run/convex/Server";import { Messages } from "../src/messages.ts";
export default Convex.schema(Messages);To mix in a table defined with Convex’s v validators, call defineSchema yourself and pass Messages.definition for this one.
Stored values and decoded values
Section titled “Stored values and decoded values”Convex stores a schema’s encoded form, and your code sees the decoded one. sentAt above is a number in the database and a Date in your functions. Checks such as NonEmptyString aren’t part of what Convex validates. They run when a document is written, and again when it’s read.
A field that points at another document uses Convex.Id:
const Reply = Schema.Struct({ message: Convex.Id("messages"), body: Schema.String,});Convex.Bytes stores binary data, and Convex.Int64 a bigint.
Read documents
Section titled “Read documents”A table’s methods run on the database of the function that calls them. Get one document by its ID:
const message = yield* Messages.get(id);It is an Option: None when no document has that ID.
Query with Convex’s query builder, typed by the table’s indexes, and finish with collect, first, unique, take or paginate:
const recent = yield* Messages.query((q) => q.withIndex("by_channel", (r) => r.eq("channel", channel)).order("desc"),).take(10);An index name the table doesn’t have doesn’t type-check.
Write documents
Section titled “Write documents”const id = yield* Messages.insert({ channel, body, sentAt: new Date() });yield* Messages.patch(id, { body: "edited" });yield* Messages.delete(id);patch changes some fields, and replace the whole document. A value that fails the schema’s checks fails with SchemaError, and nothing is written. Writing needs a mutation: in a query, insert doesn’t type-check.
See Queries, mutations and actions.
Limits
Section titled “Limits”A schema must encode to Convex values. Tuples, recursive schemas, and classes with no encoding fail with UnsupportedSchema when the module loads. Field names must be strings.
When you push a new schema, Convex checks the documents already in the table against it, and refuses the push if any don’t match. Add a field as optional first, fill it in, then make it required.