import type { Expand } from '../../../shared/src/expand.ts'; import type { Schema } from '../../../zero-schema/src/builder/schema-builder.ts'; import type { SchemaValueToTSType, TableSchema } from '../../../zero-schema/src/table-schema.ts'; import type { Query } from '../query/query.ts'; type ClientID = string; export type Location = 'client' | 'server'; export type TransactionReason = 'optimistic' | 'rebase' | 'authoritative'; export interface TransactionBase { readonly location: Location; readonly clientID: ClientID; /** * The ID of the mutation that is being applied. */ readonly mutationID: number; /** * The reason for the transaction. */ readonly reason: TransactionReason; readonly mutate: SchemaCRUD; readonly query: SchemaQuery; } export type Transaction = ServerTransaction | ClientTransaction; export interface ServerTransaction extends TransactionBase { readonly location: 'server'; readonly reason: 'authoritative'; readonly dbTransaction: DBTransaction; } /** * An instance of this is passed to custom mutator implementations and * allows reading and writing to the database and IVM at the head * at which the mutator is being applied. */ export interface ClientTransaction extends TransactionBase { readonly location: 'client'; readonly reason: 'optimistic' | 'rebase'; } export interface Row { [column: string]: unknown; } export interface DBConnection { transaction: (cb: (tx: DBTransaction) => Promise) => Promise; } export interface DBTransaction extends Queryable { readonly wrappedTransaction: T; } interface Queryable { query: (query: string, args: unknown[]) => Promise>; } export type SchemaCRUD = { [Table in keyof S['tables']]: TableCRUD; }; export type TableCRUD = { /** * Writes a row if a row with the same primary key doesn't already exists. * Non-primary-key fields that are 'optional' can be omitted or set to * `undefined`. Such fields will be assigned the value `null` optimistically * and then the default value as defined by the server. */ insert: (value: InsertValue) => Promise; /** * Writes a row unconditionally, overwriting any existing row with the same * primary key. Non-primary-key fields that are 'optional' can be omitted or * set to `undefined`. Such fields will be assigned the value `null` * optimistically and then the default value as defined by the server. */ upsert: (value: UpsertValue) => Promise; /** * Updates a row with the same primary key. If no such row exists, this * function does nothing. All non-primary-key fields can be omitted or set to * `undefined`. Such fields will be left unchanged from previous value. */ update: (value: UpdateValue) => Promise; /** * Deletes the row with the specified primary key. If no such row exists, this * function does nothing. */ delete: (id: DeleteID) => Promise; }; export type SchemaQuery = { readonly [K in keyof S['tables'] & string]: Query; }; export type DeleteID = Expand>; type PrimaryKeyFields = { [K in Extract]: SchemaValueToTSType; }; export type InsertValue = Expand & { [K in keyof S['columns'] as S['columns'][K] extends { optional: true; } ? K : never]?: SchemaValueToTSType | undefined; } & { [K in keyof S['columns'] as S['columns'][K] extends { optional: true; } ? never : K]: SchemaValueToTSType; }>; export type UpsertValue = InsertValue; export type UpdateValue = Expand & { [K in keyof S['columns']]?: SchemaValueToTSType | undefined; }>; export declare function customMutatorKey(namespace: string, name: string): string; export declare function splitMutatorKey(key: string): [string, string]; export {}; //# sourceMappingURL=custom.d.ts.map