add sqlocal connection including example

This commit is contained in:
dswbx
2025-03-15 14:40:41 +01:00
parent 5697b7891a
commit 622a7b2b9a
22 changed files with 1047 additions and 56 deletions

View File

@@ -0,0 +1,51 @@
import { Kysely, ParseJSONResultsPlugin } from "kysely";
import { SqliteConnection, SqliteIntrospector } from "bknd/data";
import { SQLocalKysely } from "sqlocal/kysely";
import type { ClientConfig } from "sqlocal";
const plugins = [new ParseJSONResultsPlugin()];
export type SQLocalConnectionConfig = Omit<ClientConfig, "databasePath"> & {
// make it optional
databasePath?: ClientConfig["databasePath"];
};
export class SQLocalConnection extends SqliteConnection {
private _client: SQLocalKysely | undefined;
constructor(private config: SQLocalConnectionConfig) {
super(null as any, {}, plugins);
}
override async init() {
if (this.initialized) return;
await new Promise((resolve) => {
this._client = new SQLocalKysely({
...this.config,
databasePath: this.config.databasePath ?? "session",
onConnect: (r) => {
this.kysely = new Kysely<any>({
dialect: {
...this._client!.dialect,
createIntrospector: (db: Kysely<any>) => {
return new SqliteIntrospector(db, {
plugins,
});
},
},
plugins,
});
this.config.onConnect?.(r);
resolve(1);
},
});
});
super.init();
}
get client(): SQLocalKysely {
if (!this._client) throw new Error("Client not initialized");
return this._client!;
}
}