Module: schema
Utilities for defining the schema of your Convex project.
Usage
Schemas should be placed in a schema.ts
file in your convex/
directory.
Schema definitions should be built using defineSchema, defineTable, and v. Make sure to export the schema as the default export.
import { defineSchema, defineTable } from "convex/schema";
import { v } from "convex/values";
export default defineSchema({
messages: defineTable({
body: v.string(),
user: v.id("users"),
}),
users: defineTable({
name: v.string(),
}),
});
To learn more about schemas, see Defining a Schema.
Classes
Interfaces
Type Aliases
GenericSchema
Ƭ GenericSchema: Record
<string
, TableDefinition
>
A type describing the schema of a Convex project.
This should be constructed using defineSchema, defineTable, and v.
Defined in
DataModelFromSchemaDefinition
Ƭ DataModelFromSchemaDefinition<SchemaDef
>: MaybeMakeLooseDataModel
<{ [TableName in keyof SchemaDef["tables"] & string]: SchemaDef["tables"][TableName] extends TableDefinition<infer Document, infer FieldPaths, infer Indexes, infer SearchIndexes> ? Object : never }, SchemaDef
["strictTableNameTypes"
]>
Internal type used in Convex code generation!
Convert a SchemaDefinition into a GenericDataModel.
Type parameters
Name | Type |
---|---|
SchemaDef | extends SchemaDefinition <any , boolean > |
Defined in
Functions
defineTable
▸ defineTable<DocumentSchema
>(documentSchema
): TableDefinition
<ExtractDocument
<DocumentSchema
>, ExtractFieldPaths
<DocumentSchema
>>
Define a table in a schema.
You can either specify the schema of your documents as an object like
defineTable({
field: v.string()
});
or as a schema type like
defineTable(
v.union(
v.object({...}),
v.object({...})
)
);
Type parameters
Name | Type |
---|---|
DocumentSchema | extends Validator <Record <string , any >, false , any , DocumentSchema > |
Parameters
Name | Type | Description |
---|---|---|
documentSchema | DocumentSchema | The type of documents stored in this table. |
Returns
TableDefinition
<ExtractDocument
<DocumentSchema
>, ExtractFieldPaths
<DocumentSchema
>>
A TableDefinition for the table.
Defined in
▸ defineTable<DocumentSchema
>(documentSchema
): TableDefinition
<ExtractDocument
<ObjectValidator
<DocumentSchema
>>, ExtractFieldPaths
<ObjectValidator
<DocumentSchema
>>>
Define a table in a schema.
You can either specify the schema of your documents as an object like
defineTable({
field: v.string()
});
or as a schema type like
defineTable(
v.union(
v.object({...}),
v.object({...})
)
);
Type parameters
Name | Type |
---|---|
DocumentSchema | extends Record <string , Validator <any , any , any >> |
Parameters
Name | Type | Description |
---|---|---|
documentSchema | DocumentSchema | The type of documents stored in this table. |
Returns
TableDefinition
<ExtractDocument
<ObjectValidator
<DocumentSchema
>>, ExtractFieldPaths
<ObjectValidator
<DocumentSchema
>>>
A TableDefinition for the table.
Defined in
defineSchema
▸ defineSchema<Schema
, StrictTableNameTypes
>(schema
, options?
): SchemaDefinition
<Schema
, StrictTableNameTypes
>
Define the schema of this Convex project.
This should be exported from a schema.ts
file in your convex/
directory
like:
export default defineSchema({
...
});
Type parameters
Name | Type |
---|---|
Schema | extends GenericSchema |
StrictTableNameTypes | extends boolean = true |
Parameters
Name | Type | Description |
---|---|---|
schema | Schema | A map from table name to TableDefinition for all of the tables in this project. |
options? | DefineSchemaOptions <StrictTableNameTypes > | Optional configuration. See DefineSchemaOptions for a full description. |
Returns
SchemaDefinition
<Schema
, StrictTableNameTypes
>
The schema.