Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20230115.0",
"@tsconfig/node14": "^1.0.3",
"kysely": "^0.23.3",
"kysely": "^0.26.3",
"prettier": "^2.7.1",
"typescript": "^4.8.4"
}
Expand Down
64 changes: 61 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import {
CompiledQuery,
DatabaseConnection,
DatabaseIntrospector,
DatabaseMetadataOptions,
DEFAULT_MIGRATION_LOCK_TABLE,
DEFAULT_MIGRATION_TABLE,
Dialect,
Driver,
Kysely,
QueryCompiler,
QueryResult,
SqliteAdapter,
SqliteIntrospector,
SqliteQueryCompiler,
QueryCompiler,
QueryResult,
TableMetadata,
} from 'kysely';
import type { D1Database } from '@cloudflare/workers-types';

Expand All @@ -20,6 +24,60 @@ export interface D1DialectConfig {
database: D1Database;
}

/**
* 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<any>, config: D1DialectConfig) {
super(db);

this.#config = config;
}

async #getTableMetadata(name: string, isView: boolean): Promise<TableMetadata> {
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<TableMetadata[]> {
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')));
}
}

/**
* 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`.
Expand Down Expand Up @@ -53,7 +111,7 @@ export class D1Dialect implements Dialect {
}

createIntrospector(db: Kysely<any>): DatabaseIntrospector {
return new SqliteIntrospector(db);
return new D1Introspector(db, this.#config);
}
}

Expand Down