mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
finalized sqlocal, added BkndBrowserApp, updated react example
This commit is contained in:
48
app/src/data/connection/sqlite/sqlocal/SQLocalConnection.ts
Normal file
48
app/src/data/connection/sqlite/sqlocal/SQLocalConnection.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Kysely, ParseJSONResultsPlugin } from "kysely";
|
||||
import { SqliteConnection, SqliteIntrospector, type DB } from "bknd";
|
||||
import type { SQLocalKysely } from "sqlocal/kysely";
|
||||
|
||||
const plugins = [new ParseJSONResultsPlugin()];
|
||||
|
||||
export class SQLocalConnection extends SqliteConnection<SQLocalKysely> {
|
||||
private connected: boolean = false;
|
||||
|
||||
constructor(client: SQLocalKysely) {
|
||||
// @ts-expect-error - config is protected
|
||||
client.config.onConnect = () => {
|
||||
// we need to listen for the connection, it will be awaited in init()
|
||||
this.connected = true;
|
||||
};
|
||||
super({
|
||||
kysely: new Kysely<any>({
|
||||
dialect: {
|
||||
...client.dialect,
|
||||
createIntrospector: (db: Kysely<DB>) => {
|
||||
return new SqliteIntrospector(db as any, {
|
||||
plugins,
|
||||
});
|
||||
},
|
||||
},
|
||||
plugins,
|
||||
}) as any,
|
||||
});
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
override async init() {
|
||||
if (this.initialized) return;
|
||||
let tries = 0;
|
||||
while (!this.connected && tries < 100) {
|
||||
tries++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
}
|
||||
if (!this.connected) {
|
||||
throw new Error("Failed to connect to SQLite database");
|
||||
}
|
||||
this.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
export function sqlocal(instance: InstanceType<typeof SQLocalKysely>): SQLocalConnection {
|
||||
return new SQLocalConnection(instance);
|
||||
}
|
||||
Reference in New Issue
Block a user