import { assertBatchLimits, assertStatementLimits, type RunResult, type SqlDriver, type SqlStatement, type SqlValue, StorageError, } from "../types.ts"; /** Cloudflare D1 adapter. Expects the `D1Database` binding from the Worker environment. */ export class D1Driver implements SqlDriver { readonly name = "d1"; readonly #db: D1Database; constructor(db: D1Database) { this.#db = db; } #prepare(sql: string, params: readonly SqlValue[] = []): D1PreparedStatement { assertStatementLimits(sql, params); return this.#db.prepare(sql).bind(...params); } async get>( sql: string, params?: readonly SqlValue[], ): Promise { const row = await this.#prepare(sql, params).first(); return row ?? undefined; } async all>( sql: string, params?: readonly SqlValue[], ): Promise { const res = await this.#prepare(sql, params).all(); return res.results; } async run(sql: string, params?: readonly SqlValue[]): Promise { const res = await this.#prepare(sql, params).run(); return toRunResult(res); } async batch(statements: readonly SqlStatement[]): Promise { assertBatchLimits(statements); const prepared = statements.map((s) => this.#prepare(s.sql, s.params)); let results: D1Result[]; try { results = await this.#db.batch(prepared); } catch (cause) { throw new StorageError(`batch failed: ${(cause as Error).message}`, cause); } return results.map(toRunResult); } async exec(sql: string): Promise { await this.#db.exec(sql); } async close(): Promise { // D1 bindings are owned by the runtime; nothing to release. } } function toRunResult(res: D1Result): RunResult { const meta = res.meta as { changes?: number; last_row_id?: number }; return { changes: meta.changes ?? 0, lastRowId: meta.last_row_id ?? null }; }