UNPKG

rwsdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

67 lines (66 loc) 1.98 kB
import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler, } from "kysely"; import debug from "../debug"; const log = debug("sdk:db:do-worker-dialect"); export class DOWorkerDialect { constructor(config) { this.config = config; } createAdapter() { return new SqliteAdapter(); } createDriver() { return new DOWorkerDriver(this.config); } createQueryCompiler() { return new SqliteQueryCompiler(); } createIntrospector(db) { return new SqliteIntrospector(db); } } class DOWorkerDriver { constructor(config) { this.config = config; } async init() { } async acquireConnection() { return new DOWorkerConnection(this.config.kyselyExecuteQuery); } async beginTransaction(conn) { return await conn.beginTransaction(); } async commitTransaction(conn) { return await conn.commitTransaction(); } async rollbackTransaction(conn) { return await conn.rollbackTransaction(); } async releaseConnection(_conn) { } async destroy() { } } class DOWorkerConnection { constructor(executeQuery) { this._executeQuery = executeQuery; } async executeQuery(compiledQuery) { log("Forwarding query to Durable Object: %s", compiledQuery.sql); // Call the DO's kyselyExecuteQuery method const result = await this._executeQuery({ sql: compiledQuery.sql, parameters: compiledQuery.parameters, }); return result; } async beginTransaction() { throw new Error("Transactions are not supported yet."); } async commitTransaction() { throw new Error("Transactions are not supported yet."); } async rollbackTransaction() { throw new Error("Transactions are not supported yet."); } async *streamQuery(_compiledQuery, _chunkSize) { throw new Error("DO Driver does not support streaming"); } }