Skip to main content

Class: TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

server.TableDefinition

The definition of a table within a schema.

This should be produced by using defineTable.

Type parameters

NameType
DocumentTypeextends Validator<any, any, any> = Validator<any, any, any>
Indexesextends GenericTableIndexes =
SearchIndexesextends GenericTableSearchIndexes =
VectorIndexesextends GenericTableVectorIndexes =

Properties

validator

validator: DocumentType

Defined in

server/schema.ts:199

Methods

indexes

▸ ** indexes**(): { indexDescriptor: string ; fields: string[] }[]

This API is experimental: it may change or disappear.

Returns indexes defined on this table. Intended for the advanced use cases of dynamically deciding which index to use for a query. If you think you need this, please chime in on ths issue in the Convex JS GitHub repo. https://github.com/get-convex/convex-js/issues/49

Returns

{ indexDescriptor: string ; fields: string[] }[]

Defined in

server/schema.ts:222


index

index<IndexName, FirstFieldPath, RestFieldPaths>(name, indexConfig): TableDefinition<DocumentType, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths[], "_creationTime"]>>, SearchIndexes, VectorIndexes>

Define an index on this table.

Indexes speed up queries by allowing efficient lookups on specific fields. Use .withIndex() in your queries to leverage them.

Index fields must be queried in the same order they are defined. If you need to query by field2 then field1, create a separate index with that field order.

Example

defineTable({
userId: v.id("users"),
status: v.string(),
updatedAt: v.number(),
})
// Name indexes after their fields:
.index("by_userId", ["userId"])
.index("by_status_updatedAt", ["status", "updatedAt"])

Best practice: Always include all index fields in the index name (e.g., "by_field1_and_field2").

See

https://docs.convex.dev/database/reading-data/indexes

Type parameters

NameType
IndexNameextends string
FirstFieldPathextends any
RestFieldPathsextends ExtractFieldPaths<DocumentType>[]

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe index configuration object.
indexConfig.fields[FirstFieldPath, ...RestFieldPaths[]]The fields to index, in order. Must specify at least one field.
indexConfig.staged?falseWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths[], "_creationTime"]>>, SearchIndexes, VectorIndexes>

A TableDefinition with this index included.

Defined in

server/schema.ts:257

index<IndexName, FirstFieldPath, RestFieldPaths>(name, fields): TableDefinition<DocumentType, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths[], "_creationTime"]>>, SearchIndexes, VectorIndexes>

Define an index on this table.

To learn about indexes, see Defining Indexes.

Type parameters

NameType
IndexNameextends string
FirstFieldPathextends any
RestFieldPathsextends ExtractFieldPaths<DocumentType>[]

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
fields[FirstFieldPath, ...RestFieldPaths[]]The fields to index, in order. Must specify at least one field.

Returns

TableDefinition<DocumentType, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths[], "_creationTime"]>>, SearchIndexes, VectorIndexes>

A TableDefinition with this index included.

Defined in

server/schema.ts:290

index<IndexName, FirstFieldPath, RestFieldPaths>(name, indexConfig): TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

Define a staged index on this table.

For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later.

If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

To learn about indexes, see Defining Indexes.

Type parameters

NameType
IndexNameextends string
FirstFieldPathextends any
RestFieldPathsextends ExtractFieldPaths<DocumentType>[]

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe index configuration object.
indexConfig.fields[FirstFieldPath, ...RestFieldPaths[]]The fields to index, in order. Must specify at least one field.
indexConfig.stagedtrueWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

A TableDefinition with this index included.

Defined in

server/schema.ts:326


searchIndex

searchIndex<IndexName, SearchField, FilterFields>(name, indexConfig): TableDefinition<DocumentType, Indexes, Expand<SearchIndexes & Record<IndexName, { searchField: SearchField ; filterFields: FilterFields }>>, VectorIndexes>

Define a search index on this table.

To learn about search indexes, see Search.

Type parameters

NameType
IndexNameextends string
SearchFieldextends any
FilterFieldsextends any = never

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe search index configuration object.
indexConfig.searchFieldSearchFieldThe field to index for full text search. This must be a field of type string.
indexConfig.filterFields?FilterFields[]Additional fields to index for fast filtering when running search queries.
indexConfig.staged?falseWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Indexes, Expand<SearchIndexes & Record<IndexName, { searchField: SearchField ; filterFields: FilterFields }>>, VectorIndexes>

A TableDefinition with this search index included.

Defined in

server/schema.ts:379

searchIndex<IndexName, SearchField, FilterFields>(name, indexConfig): TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

Define a staged search index on this table.

For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later.

If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

To learn about search indexes, see Search.

Type parameters

NameType
IndexNameextends string
SearchFieldextends any
FilterFieldsextends any = never

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe search index configuration object.
indexConfig.searchFieldSearchFieldThe field to index for full text search. This must be a field of type string.
indexConfig.filterFields?FilterFields[]Additional fields to index for fast filtering when running search queries.
indexConfig.stagedtrueWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

A TableDefinition with this search index included.

Defined in

server/schema.ts:423


vectorIndex

vectorIndex<IndexName, VectorField, FilterFields>(name, indexConfig): TableDefinition<DocumentType, Indexes, SearchIndexes, Expand<VectorIndexes & Record<IndexName, { vectorField: VectorField ; dimensions: number ; filterFields: FilterFields }>>>

Define a vector index on this table.

To learn about vector indexes, see Vector Search.

Type parameters

NameType
IndexNameextends string
VectorFieldextends any
FilterFieldsextends any = never

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe vector index configuration object.
indexConfig.vectorFieldVectorFieldThe field to index for vector search. This must be a field of type v.array(v.float64()) (or a union)
indexConfig.dimensionsnumberThe length of the vectors indexed. This must be between 2 and 2048 inclusive.
indexConfig.filterFields?FilterFields[]Additional fields to index for fast filtering when running vector searches.
indexConfig.staged?falseWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Indexes, SearchIndexes, Expand<VectorIndexes & Record<IndexName, { vectorField: VectorField ; dimensions: number ; filterFields: FilterFields }>>>

A TableDefinition with this vector index included.

Defined in

server/schema.ts:470

vectorIndex<IndexName, VectorField, FilterFields>(name, indexConfig): TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

Define a staged vector index on this table.

For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later.

If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

To learn about vector indexes, see Vector Search.

Type parameters

NameType
IndexNameextends string
VectorFieldextends any
FilterFieldsextends any = never

Parameters

NameTypeDescription
nameIndexNameThe name of the index.
indexConfigObjectThe vector index configuration object.
indexConfig.vectorFieldVectorFieldThe field to index for vector search. This must be a field of type v.array(v.float64()) (or a union)
indexConfig.dimensionsnumberThe length of the vectors indexed. This must be between 2 and 2048 inclusive.
indexConfig.filterFields?FilterFields[]Additional fields to index for fast filtering when running vector searches.
indexConfig.stagedtrueWhether the index should be staged. For large tables, index backfill can be slow. Staging an index allows you to push the schema and enable the index later. If staged is true, the index will be staged and will not be enabled until the staged flag is removed. Staged indexes do not block push completion. Staged indexes cannot be used in queries.

Returns

TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

A TableDefinition with this vector index included.

Defined in

server/schema.ts:513


self

Protected self(): TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

Work around for https://github.com/microsoft/TypeScript/issues/57035

Returns

TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>

Defined in

server/schema.ts:556