import { CompiledQuery, DatabaseConnection, DatabaseIntrospector, Dialect, Driver, Kysely, SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler, QueryCompiler, QueryResult, DatabaseMetadataOptions, TableMetadata, DEFAULT_MIGRATION_LOCK_TABLE, DEFAULT_MIGRATION_TABLE, } from "kysely"; import type { D1Database } from "@cloudflare/workers-types"; /** * Config for the D1 dialect. Pass your D1 instance to this object that you bound in `wrangler.toml`. */ export interface D1DialectConfig { database: D1Database; /** * Enable automatic read replica routing for SELECT queries. * When enabled, SELECT queries will use D1's read replicas via withSession(). * @default true */ useReadReplica?: boolean; } /** * D1 dialect that adds support for [Cloudflare D1][0] in [Kysely][1]. * The constructor takes the instance of your D1 database that you bound in `wrangler.toml`. * * ```typescript * new D1Dialect({ * database: env.DB, * useReadReplica: true, // optional, defaults to true * }) * ``` * * [0]: https://blog.cloudflare.com/introducing-d1/ * [1]: https://github.com/koskimas/kysely */ export class D1Dialect implements Dialect { #config: D1DialectConfig; constructor(config: D1DialectConfig) { this.#config = { useReadReplica: true, ...config, }; } createAdapter() { return new SqliteAdapter(); } createDriver(): Driver { return new D1Driver(this.#config); } createQueryCompiler(): QueryCompiler { return new SqliteQueryCompiler(); } createIntrospector(db: Kysely): DatabaseIntrospector { return new D1Introspector(db, this.#config); } } class D1Driver implements Driver { #config: D1DialectConfig; constructor(config: D1DialectConfig) { this.#config = config; } async init(): Promise {} async acquireConnection(): Promise { return new D1Connection(this.#config); } async beginTransaction(conn: D1Connection): Promise { return await conn.beginTransaction(); } async commitTransaction(conn: D1Connection): Promise { return await conn.commitTransaction(); } async rollbackTransaction(conn: D1Connection): Promise { return await conn.rollbackTransaction(); } async releaseConnection(_conn: D1Connection): Promise {} async destroy(): Promise {} } class D1Connection implements DatabaseConnection { #config: D1DialectConfig; #readReplicaSession?: D1DatabaseSession; constructor(config: D1DialectConfig) { this.#config = config; // Create read replica session once if enabled if (this.#config.useReadReplica !== false) { this.#readReplicaSession = this.#config.database.withSession("first-primary"); } } async executeQuery(compiledQuery: CompiledQuery): Promise> { const useReadReplica = this.#config.useReadReplica !== false; const isSelectQuery = this.#isSelectQuery(compiledQuery.sql); // Use read replica session for SELECT queries if enabled const db = useReadReplica && isSelectQuery && this.#readReplicaSession ? this.#readReplicaSession : this.#config.database; const results = await db .prepare(compiledQuery.sql) .bind(...compiledQuery.parameters) .all(); if (results.error) { throw new Error(results.error); } const numAffectedRows = results.meta.changes > 0 ? BigInt(results.meta.changes) : undefined; return { insertId: results.meta.last_row_id === undefined || results.meta.last_row_id === null ? undefined : BigInt(results.meta.last_row_id), rows: (results?.results as O[]) || [], numAffectedRows, }; } #isSelectQuery(sql: string): boolean { // Trim and get the first word const trimmed = sql.trim().toUpperCase(); return trimmed.startsWith("SELECT") || trimmed.startsWith("WITH"); } 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: CompiledQuery, _chunkSize: number, ): AsyncIterableIterator> { throw new Error("D1 Driver does not support streaming"); } } /** * A custom Kysely Introspector class for D1 that uses supported SQLite PRAGMA * statements to get the table metadata, instead of querying protected tables. */ class D1Introspector extends SqliteIntrospector { #config: D1DialectConfig; constructor(db: Kysely, config: D1DialectConfig) { super(db); this.#config = config; } async #getTableMetadata(name: string, isView: boolean): Promise { const result = await this.#config.database.prepare(`PRAGMA table_info(${name})`).run(); const rows = result.results as { name: string; type: string; notnull: 1 | 0; dflt_value: unknown; }[]; return { name, isView, columns: rows.map((row) => ({ name: row.name, dataType: row.type, isAutoIncrementing: false, isNullable: row.notnull === 0, hasDefaultValue: row.dflt_value !== null, })), }; } async getTables(options?: DatabaseMetadataOptions): Promise { const result = await this.#config.database.prepare("PRAGMA table_list").run(); // We filter out tables that start with "_cf_", as they are internal tables // which will cause errors when trying to introspect them. let tables = ( result.results as { name: string; type: "table" | "view"; }[] ).filter(({ name }) => !name.startsWith("_cf_")); if (!options?.withInternalKyselyTables) { tables = tables.filter( ({ name }) => name !== DEFAULT_MIGRATION_TABLE && name !== DEFAULT_MIGRATION_LOCK_TABLE, ); } return Promise.all(tables.map(({ name, type }) => this.#getTableMetadata(name, type === "view"))); } }