rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
50 lines (49 loc) • 2.03 kB
JavaScript
import { DurableObject } from "cloudflare:workers";
import { DODialect } from "kysely-do";
import { Kysely, ParseJSONResultsPlugin, } from "kysely";
import debug from "../debug.js";
import { createMigrator } from "./index.js";
const log = debug("sdk:do-db");
// Base class for Durable Objects that need Kysely database access
export class SqliteDurableObject extends DurableObject {
constructor(ctx, env, migrations, migrationTableName = "__migrations") {
super(ctx, env);
this.initialized = false;
this.migrations = migrations;
this.migrationTableName = migrationTableName;
this.kysely = new Kysely({
dialect: new DODialect({ ctx }),
plugins: [new ParseJSONResultsPlugin()],
});
}
async initialize() {
if (this.initialized) {
log("Database already initialized, skipping");
return;
}
log("Initializing Durable Object database");
const migrator = createMigrator(this.kysely, this.migrations, this.migrationTableName);
const result = await migrator.migrateToLatest();
if (result.error) {
console.log("rwsdk/db: Migrations failed, rolling back and throwing with the migration error: %O", result.results);
await migrator.migrateDown();
throw result.error;
}
log("Migrations results", result.results);
this.initialized = true;
log("Database initialization complete");
}
// RPC method for executing queries - must be on prototype for RPC to work
async kyselyExecuteQuery(compiledQuery) {
await this.initialize();
log("Executing SQL: %s with params: %o", compiledQuery.sql, compiledQuery.parameters);
// Forward to the internal Kysely database
const result = await this.kysely.executeQuery({
sql: compiledQuery.sql,
parameters: compiledQuery.parameters,
query: {},
queryId: {},
});
return result;
}
}