import { Kysely, KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs, RootOperationNode, UnknownRow, QueryResult, } from "kysely"; /** Map of table name -> Set of boolean column names */ export type BooleanColumnsMap = Map>; /** * Introspects a Kysely database to find all BOOLEAN/BOOL columns. * Call this BEFORE using the database for queries to avoid deadlocks. * * @param db - The Kysely database instance to introspect * @returns A map of table names to sets of boolean column names */ export async function introspectBooleanColumns(db: Kysely): Promise { const cache = new Map>(); try { const tables = await db.introspection.getTables(); for (const table of tables) { const booleanCols = new Set(); for (const column of table.columns) { const dataType = column.dataType.toUpperCase(); if (dataType === "BOOLEAN" || dataType === "BOOL") { booleanCols.add(column.name); } } if (booleanCols.size > 0) { cache.set(table.name, booleanCols); } } } catch (error) { console.error("introspectBooleanColumns: Error during introspection:", error); } return cache; } /** * Plugin that converts SQLite boolean values (1/0) to JavaScript booleans (true/false). * * SQLite doesn't have a native boolean type, so booleans are stored as INTEGER (1 or 0). * This plugin uses pre-computed table metadata to identify columns defined with BOOLEAN/BOOL type * and converts their values in query results. * * IMPORTANT: The boolean columns map must be provided via a Promise that resolves BEFORE * any queries with results are executed. Use `introspectBooleanColumns()` to build this map. */ export class ParseBooleanResultsPlugin implements KyselyPlugin { #booleanColumnsPromise: Promise; #booleanColumnsCache: BooleanColumnsMap | null = null; constructor(booleanColumnsPromise: Promise) { this.#booleanColumnsPromise = booleanColumnsPromise; } /** * Updates the boolean columns cache with new data. * Useful for tests or when tables are created after the database is initialized. */ updateCache(booleanColumns: BooleanColumnsMap): void { this.#booleanColumnsCache = booleanColumns; } transformQuery(args: PluginTransformQueryArgs): RootOperationNode { return args.node; } async transformResult(args: PluginTransformResultArgs): Promise> { const { result } = args; if (!result.rows || result.rows.length === 0) { return result; } const booleanColumns = await this.#ensureCache(); if (booleanColumns.size === 0) { return result; } return { ...result, rows: result.rows.map((row) => this.#transformRow(row, booleanColumns)), }; } async #ensureCache(): Promise { if (this.#booleanColumnsCache) { return this.#booleanColumnsCache; } this.#booleanColumnsCache = await this.#booleanColumnsPromise; return this.#booleanColumnsCache; } #transformRow(row: UnknownRow, booleanColumns: BooleanColumnsMap): UnknownRow { const transformed: Record = {}; for (const [key, value] of Object.entries(row)) { transformed[key] = this.#transformValue(key, value, booleanColumns); } return transformed; } #transformValue(columnName: string, value: unknown, booleanColumns: BooleanColumnsMap) { if (value === null || value === undefined) { return value; } // Check if this column is a boolean in any table // Since we don't know which table this row came from, we check all tables for (const booleanCols of booleanColumns.values()) { if (booleanCols.has(columnName)) { if (value === 1) return true; if (value === 0) return false; // Already a boolean or non-standard value, cast to boolean return !!value; } } return value; } }