import type {MaybePromise} from '../../shared/src/types.ts'; import {formatPg, sql} from '../../z2s/src/sql.ts'; import type {CleanupResultsArg} from '../../zero-protocol/src/mutation.ts'; import type {Schema} from '../../zero-types/src/schema.ts'; import type { DBConnection, DBTransaction, Queryable, RetryPredicate, } from '../../zql/src/mutate/custom.ts'; import {asQueryInternals} from '../../zql/src/query/query-internals.ts'; import type { HumanReadable, Query, RunOptions, } from '../../zql/src/query/query.ts'; import {ClosableDBTransaction} from './closable-db-transaction.ts'; import {CRUDMutatorFactory, type TransactionImpl} from './custom.ts'; import {executePostgresQuery} from './pg-query-executor.ts'; import type { Database, TransactionProviderHooks, TransactionProviderInput, } from './process-mutations.ts'; /** * Implements a Database for use with PushProcessor that is backed by Postgres. * * This implementation also implements the same ZQL interfaces for reading and * writing data that the Zero client does, so that mutator functions can be * shared across client and server. */ export class ZQLDatabase< TSchema extends Schema, TWrappedTransaction, > implements Database> { readonly connection: DBConnection; readonly #crudFactory: CRUDMutatorFactory; readonly #schema: TSchema; constructor(connection: DBConnection, schema: TSchema) { this.connection = connection; this.#crudFactory = new CRUDMutatorFactory(schema); this.#schema = schema; } transaction( callback: ( tx: TransactionImpl, transactionHooks: TransactionProviderHooks, ) => MaybePromise, transactionInput?: TransactionProviderInput, ): Promise { // Icky hack. This is just here to have user not have to do this. // These interfaces need to be factored better. const { upstreamSchema = '', clientGroupID = '', clientID = '', mutationID = 0, addRetryPredicate, } = transactionInput ?? {}; return this.connection.transaction(async dbTx => { // What the mutator's `tx` uses. Closed once the callback settles, so a // `tx` that outlives its transaction, such as one captured by a // `tx.retryOn()` predicate, throws instead of reaching a connection // that may already be back in the pool. const mutatorTx = new ClosableDBTransaction(dbTx); try { const zeroTx = await this.#makeServerTransaction( mutatorTx, clientID, mutationID, addRetryPredicate, ); return await callback(zeroTx, { async updateClientMutationID() { const formatted = formatPg( sql`INSERT INTO ${sql.ident(upstreamSchema)}.clients as current ("clientGroupID", "clientID", "lastMutationID") VALUES (${clientGroupID}, ${clientID}, ${1}) ON CONFLICT ("clientGroupID", "clientID") DO UPDATE SET "lastMutationID" = current."lastMutationID" + 1 RETURNING "lastMutationID"`, ); const [{lastMutationID}] = (await dbTx.query( formatted.text, formatted.values, )) as {lastMutationID: bigint}[]; return {lastMutationID}; }, async writeMutationResult(result) { const formatted = formatPg( sql`INSERT INTO ${sql.ident(upstreamSchema)}.mutations ("clientGroupID", "clientID", "mutationID", "result") VALUES (${clientGroupID}, ${result.id.clientID}, ${result.id.id}, ${JSON.stringify( result.result, )}::text::json)`, ); await dbTx.query(formatted.text, formatted.values); }, async deleteMutationResults(args: CleanupResultsArg) { if ('type' in args && args.type === 'bulk') { // Bulk deletion: delete all mutations for multiple clients const formatted = formatPg( sql`DELETE FROM ${sql.ident(upstreamSchema)}."mutations" WHERE "clientGroupID" = ${args.clientGroupID} AND "clientID" = ANY(${args.clientIDs})`, ); await dbTx.query(formatted.text, formatted.values); } else { // Single client (explicit 'single' or legacy without type): delete up to mutation ID const formatted = formatPg( sql`DELETE FROM ${sql.ident(upstreamSchema)}."mutations" WHERE "clientGroupID" = ${args.clientGroupID} AND "clientID" = ${args.clientID} AND "mutationID" <= ${args.upToMutationID}`, ); await dbTx.query(formatted.text, formatted.values); } }, }); } finally { mutatorTx.close(); } }); } #makeServerTransaction( dbTx: DBTransaction, clientID: string, mutationID: number, addRetryPredicate: ((shouldRetry: RetryPredicate) => void) | undefined, ) { return this.#crudFactory.createTransaction( dbTx, clientID, mutationID, addRetryPredicate, ); } /** * Runs a single read query. * * When the {@link DBConnection} implements `query`, the compiled SQL is * issued as a bare statement with no `BEGIN`/`COMMIT` around it (plus a * one-time server schema lookup the first time this instance touches the * database). Postgres then releases the statement's locks the moment it * finishes instead of waiting for a `COMMIT` round-trip, which matters in * serverless environments where the process can be frozen or reclaimed * between the query resolving and the `COMMIT` being sent, leaving the * transaction and its locks open indefinitely. * * Because no transaction is opened, any per-transaction setup the adapter * performs inside `transaction` does not apply to these reads. If the * connection does not implement `query`, the read is wrapped in a * transaction as before. * * If you need multiple reads to observe a consistent snapshot, use * {@link transaction} and call `tx.run(...)` for each query instead. */ run( query: Query, _options?: RunOptions, ): Promise> { const {connection} = this; if (connection.query) { // TS narrows `connection.query`, not `connection` itself. return this.#runOn(connection as ReadTarget, query); } return connection.transaction(dbTx => this.#runOn(dbTx, query)); } async #runOn( target: ReadTarget, query: Query, ): Promise> { const {ast, format} = asQueryInternals(query); const serverSchema = await this.#crudFactory.getOrFetchServerSchema(target); return target.runQuery ? target.runQuery(ast, format, this.#schema, serverSchema) : executePostgresQuery( target, ast, format, this.#schema, serverSchema, ); } } /** * Something a read can be executed against: a `DBTransaction`, or a * `DBConnection` that implements `query`. */ type ReadTarget = Queryable & { runQuery?: DBTransaction['runQuery'] | undefined; };