mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-02 08:06:00 +00:00
36e61cab3f
Updated the Bun and Node SQLite connection implementations to support additional configuration options, including `onCreateConnection`. Introduced tests for connection creation to validate database instance types and ensure proper callback execution. Improved type exports for better integration with existing code.
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { Database } from "bun:sqlite";
|
|
import {
|
|
genericSqlite,
|
|
type GenericSqliteConnection,
|
|
type GenericSqliteConnectionConfig,
|
|
} from "bknd";
|
|
import { omitKeys } from "bknd/utils";
|
|
|
|
export type BunSqliteConnection = GenericSqliteConnection<Database>;
|
|
export type BunSqliteConnectionConfig = Omit<
|
|
GenericSqliteConnectionConfig<Database>,
|
|
"name" | "supports"
|
|
> &
|
|
({ database?: Database; url?: never } | { url?: string; database?: never });
|
|
|
|
export function bunSqlite(config?: BunSqliteConnectionConfig) {
|
|
let db: Database | undefined;
|
|
if (config) {
|
|
if ("database" in config && config.database) {
|
|
db = config.database;
|
|
} else if (config.url) {
|
|
db = new Database(config.url);
|
|
}
|
|
}
|
|
|
|
if (!db) {
|
|
db = new Database(":memory:");
|
|
}
|
|
|
|
return genericSqlite(
|
|
"bun-sqlite",
|
|
db,
|
|
(utils) => {
|
|
const getStmt = (sql: string) => db.prepare(sql);
|
|
|
|
return {
|
|
db,
|
|
query: utils.buildQueryFn({
|
|
all: (sql, parameters) => getStmt(sql).all(...(parameters || [])),
|
|
run: (sql, parameters) => {
|
|
const { changes, lastInsertRowid } = getStmt(sql).run(...(parameters || []));
|
|
return {
|
|
insertId: utils.parseBigInt(lastInsertRowid),
|
|
numAffectedRows: utils.parseBigInt(changes),
|
|
};
|
|
},
|
|
}),
|
|
close: () => db.close(),
|
|
};
|
|
},
|
|
omitKeys(config ?? ({} as any), ["database", "url", "name", "supports"]),
|
|
);
|
|
}
|