mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
inlined libsql dialect, rewrote d1 to use generic sqlite
This commit is contained in:
@@ -1,42 +1,76 @@
|
||||
/// <reference types="@cloudflare/workers-types" />
|
||||
|
||||
import { SqliteConnection } from "bknd/data";
|
||||
import type { ConnQuery, ConnQueryResults } from "data/connection/Connection";
|
||||
import { D1Dialect } from "kysely-d1";
|
||||
import {
|
||||
genericSqlite,
|
||||
type GenericSqliteConnection,
|
||||
} from "data/connection/sqlite/GenericSqliteConnection";
|
||||
import type { QueryResult } from "kysely";
|
||||
|
||||
export type D1SqliteConnection = GenericSqliteConnection<D1Database>;
|
||||
|
||||
export type D1ConnectionConfig<DB extends D1Database | D1DatabaseSession = D1Database> = {
|
||||
binding: DB;
|
||||
};
|
||||
|
||||
export class D1Connection<
|
||||
DB extends D1Database | D1DatabaseSession = D1Database,
|
||||
> extends SqliteConnection<DB> {
|
||||
override name = "sqlite-d1";
|
||||
export function d1Sqlite(config: D1ConnectionConfig<D1Database>) {
|
||||
const db = config.binding;
|
||||
|
||||
protected override readonly supported = {
|
||||
batching: true,
|
||||
softscans: false,
|
||||
};
|
||||
return genericSqlite(
|
||||
"d1-sqlite",
|
||||
db,
|
||||
(utils) => {
|
||||
const getStmt = (sql: string, parameters?: any[] | readonly any[]) =>
|
||||
db.prepare(sql).bind(...(parameters || []));
|
||||
|
||||
constructor(private config: D1ConnectionConfig<DB>) {
|
||||
super({
|
||||
const mapResult = (res: D1Result<any>): QueryResult<any> => {
|
||||
if (res.error) {
|
||||
throw new Error(res.error);
|
||||
}
|
||||
|
||||
const numAffectedRows =
|
||||
res.meta.changes > 0 ? utils.parseBigInt(res.meta.changes) : undefined;
|
||||
const insertId = res.meta.last_row_id
|
||||
? utils.parseBigInt(res.meta.last_row_id)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
insertId,
|
||||
numAffectedRows,
|
||||
rows: res.results,
|
||||
// @ts-ignore
|
||||
meta: res.meta,
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
db,
|
||||
batch: async (stmts) => {
|
||||
const res = await db.batch(
|
||||
stmts.map(({ sql, parameters }) => {
|
||||
return getStmt(sql, parameters);
|
||||
}),
|
||||
);
|
||||
return res.map(mapResult);
|
||||
},
|
||||
query: utils.buildQueryFn({
|
||||
all: async (sql, parameters) => {
|
||||
const prep = getStmt(sql, parameters);
|
||||
return mapResult(await prep.all()).rows;
|
||||
},
|
||||
run: async (sql, parameters) => {
|
||||
const prep = getStmt(sql, parameters);
|
||||
return mapResult(await prep.run());
|
||||
},
|
||||
}),
|
||||
close: () => {},
|
||||
};
|
||||
},
|
||||
{
|
||||
supports: {
|
||||
batching: true,
|
||||
softscans: false,
|
||||
},
|
||||
excludeTables: ["_cf_KV", "_cf_METADATA"],
|
||||
dialect: D1Dialect,
|
||||
dialectArgs: [{ database: config.binding as D1Database }],
|
||||
});
|
||||
}
|
||||
|
||||
override async executeQueries<O extends ConnQuery[]>(...qbs: O): Promise<ConnQueryResults<O>> {
|
||||
const compiled = this.getCompiled(...qbs);
|
||||
|
||||
const db = this.config.binding;
|
||||
|
||||
const res = await db.batch(
|
||||
compiled.map(({ sql, parameters }) => {
|
||||
return db.prepare(sql).bind(...parameters);
|
||||
}),
|
||||
);
|
||||
|
||||
return this.withTransformedRows(res, "results") as any;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user