mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
connection: rewrote query execution, batching, added generic sqlite, added node/bun sqlite, aligned repo/mutator results
This commit is contained in:
41
app/src/adapter/bun/connection/BunSqliteConnection.ts
Normal file
41
app/src/adapter/bun/connection/BunSqliteConnection.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
import {
|
||||
buildQueryFn,
|
||||
GenericSqliteConnection,
|
||||
parseBigInt,
|
||||
type IGenericSqlite,
|
||||
} from "data/connection/sqlite/GenericSqliteConnection";
|
||||
|
||||
export type BunSqliteConnectionConfig = {
|
||||
database: Database;
|
||||
};
|
||||
|
||||
function bunSqliteExecutor(db: Database, cache: boolean): IGenericSqlite<Database> {
|
||||
const fn = cache ? "query" : "prepare";
|
||||
const getStmt = (sql: string) => db[fn](sql);
|
||||
|
||||
return {
|
||||
db,
|
||||
query: buildQueryFn({
|
||||
all: (sql, parameters) => getStmt(sql).all(...(parameters || [])),
|
||||
run: (sql, parameters) => {
|
||||
const { changes, lastInsertRowid } = getStmt(sql).run(...(parameters || []));
|
||||
return {
|
||||
insertId: parseBigInt(lastInsertRowid),
|
||||
numAffectedRows: parseBigInt(changes),
|
||||
};
|
||||
},
|
||||
}),
|
||||
close: () => db.close(),
|
||||
};
|
||||
}
|
||||
|
||||
export function bunSqlite(config: BunSqliteConnectionConfig) {
|
||||
return new GenericSqliteConnection(
|
||||
config.database,
|
||||
() => bunSqliteExecutor(config.database, false),
|
||||
{
|
||||
name: "bun-sqlite",
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user