Skip to main content

Interface: DatabaseWriter<DataModel>

server.DatabaseWriter

An interface to read from and write to the database within Convex mutation functions.

Convex guarantees that all writes within a single mutation are executed atomically, so you never have to worry about partial writes leaving your data in an inconsistent state. See the Convex Guide for the guarantees Convex provides your functions.

If you're using TypeScript, use the DatabaseReader type in convex/_generated/server.ts which is typed for your data model.

Type parameters

NameType
DataModelextends GenericDataModel

Hierarchy

Methods

get

get<TableName>(id): Promise<null | DocumentByName<DataModel, TableName>>

Fetch a single document from the database by its GenericId.

Type parameters

NameType
TableNameextends string

Parameters

NameTypeDescription
idGenericId<TableName>The GenericId of the document to fetch from the database.

Returns

Promise<null | DocumentByName<DataModel, TableName>>

Inherited from

DatabaseReader.get


table

table<TableName>(tableName): Table<NamedTableInfo<DataModel, TableName>>

Begin a query for the given table name.

Queries don't execute immediately, so calling this method and extending its query are free until the results are actually used.

Type parameters

NameType
TableNameextends string

Parameters

NameTypeDescription
tableNameTableNameThe name of the table to query.

Returns

Table<NamedTableInfo<DataModel, TableName>>

  • A Table object to start building a query.

Inherited from

DatabaseReader.table


insert

insert<TableName>(table, value): GenericId<TableName>

Insert a new document into a table.

Type parameters

NameType
TableNameextends string

Parameters

NameTypeDescription
tableTableNameThe name of the table to insert a new document into.
valueExpand<Omit<DocumentByName<DataModel, TableName>, "_creationTime" | "_id">>The Value to insert into the given table.

Returns

GenericId<TableName>


patch

patch<TableName>(id, value): void

Patch an existing document, merging its value with a new values.

Any overlapping fields in the two documents will be overwritten with their new value.

Type parameters

NameType
TableNameextends string

Parameters

NameTypeDescription
idGenericId<TableName>The GenericId of the document to patch.
valuePartial<Expand<Expand<Omit<DocumentByName<DataModel, TableName>, "_creationTime" | "_id">> & Partial<Pick<DocumentByName<DataModel, TableName>, "_creationTime" | "_id">>>>The partial GenericDocument to merge into the specified document. If this new value specifies system fields like _id, they must match the document's existing field values.

Returns

void


replace

replace<TableName>(id, value): void

Replace the value of an existing document, overwriting its old value.

Type parameters

NameType
TableNameextends string

Parameters

NameTypeDescription
idGenericId<TableName>The GenericId of the document to replace.
valueExpand<Expand<Omit<DocumentByName<DataModel, TableName>, "_creationTime" | "_id">> & Partial<Pick<DocumentByName<DataModel, TableName>, "_creationTime" | "_id">>>The new GenericDocument for the document. This value can omit the system fields, and the database will fill them in.

Returns

void


delete

delete(id): void

Delete an existing document.

Parameters

NameTypeDescription
idGenericId<TableNamesInDataModel<DataModel>>The GenericId of the document to remove.

Returns

void