mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
feat: adding env aware endpoint to obtain sqlite connection, remove libsql hard dependency
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { Connection, type FieldSpec, type SchemaResponse } from "./Connection";
|
||||
|
||||
export class DummyConnection extends Connection {
|
||||
override name: string = "dummy";
|
||||
|
||||
protected override readonly supported = {
|
||||
batching: true,
|
||||
};
|
||||
|
||||
@@ -5,11 +5,13 @@ export {
|
||||
type IndexSpec,
|
||||
type DbFunctions,
|
||||
type SchemaResponse,
|
||||
type ConnQuery,
|
||||
type ConnQueryResults,
|
||||
customIntrospector,
|
||||
} from "./Connection";
|
||||
|
||||
// sqlite
|
||||
export { LibsqlConnection, type LibSqlCredentials } from "./sqlite/LibsqlConnection";
|
||||
//export { libsql, LibsqlConnection, type LibSqlCredentials } from "./sqlite/LibsqlConnection";
|
||||
export { SqliteConnection } from "./sqlite/SqliteConnection";
|
||||
export { SqliteIntrospector } from "./sqlite/SqliteIntrospector";
|
||||
export { SqliteLocalConnection } from "./sqlite/SqliteLocalConnection";
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
import { createClient, type Client, type Config, type InStatement } from "@libsql/client";
|
||||
import type { Client, Config, InStatement } from "@libsql/client";
|
||||
import { createClient } from "libsql-stateless-easy";
|
||||
import { LibsqlDialect } from "@libsql/kysely-libsql";
|
||||
import { $console } from "core";
|
||||
import { FilterNumericKeysPlugin } from "data/plugins/FilterNumericKeysPlugin";
|
||||
import type { ConnQuery, ConnQueryResults } from "../Connection";
|
||||
import { SqliteConnection } from "./SqliteConnection";
|
||||
import { type ConnQuery, type ConnQueryResults, SqliteConnection } from "bknd/data";
|
||||
|
||||
export const LIBSQL_PROTOCOLS = ["wss", "https", "libsql"] as const;
|
||||
export type LibSqlCredentials = Config & {
|
||||
protocol?: (typeof LIBSQL_PROTOCOLS)[number];
|
||||
};
|
||||
|
||||
function getClient(clientOrCredentials: Client | LibSqlCredentials): Client {
|
||||
if (clientOrCredentials && "url" in clientOrCredentials) {
|
||||
let { url, authToken, protocol } = clientOrCredentials;
|
||||
if (protocol && LIBSQL_PROTOCOLS.includes(protocol)) {
|
||||
console.info("changing protocol to", protocol);
|
||||
const [, rest] = url.split("://");
|
||||
url = `${protocol}://${rest}`;
|
||||
}
|
||||
|
||||
return createClient({ url, authToken });
|
||||
}
|
||||
|
||||
return clientOrCredentials as Client;
|
||||
}
|
||||
|
||||
export class LibsqlConnection extends SqliteConnection<Client> {
|
||||
override name = "libsql";
|
||||
protected override readonly supported = {
|
||||
@@ -17,22 +31,8 @@ export class LibsqlConnection extends SqliteConnection<Client> {
|
||||
softscans: true,
|
||||
};
|
||||
|
||||
constructor(client: Client);
|
||||
constructor(credentials: LibSqlCredentials);
|
||||
constructor(clientOrCredentials: Client | LibSqlCredentials) {
|
||||
let client: Client;
|
||||
if (clientOrCredentials && "url" in clientOrCredentials) {
|
||||
let { url, authToken, protocol } = clientOrCredentials;
|
||||
if (protocol && LIBSQL_PROTOCOLS.includes(protocol)) {
|
||||
$console.log("changing protocol to", protocol);
|
||||
const [, rest] = url.split("://");
|
||||
url = `${protocol}://${rest}`;
|
||||
}
|
||||
|
||||
client = createClient({ url, authToken });
|
||||
} else {
|
||||
client = clientOrCredentials;
|
||||
}
|
||||
const client = getClient(clientOrCredentials);
|
||||
|
||||
super({
|
||||
excludeTables: ["libsql_wasm_func_table"],
|
||||
@@ -56,3 +56,7 @@ export class LibsqlConnection extends SqliteConnection<Client> {
|
||||
return this.withTransformedRows(await this.client.batch(stms)) as any;
|
||||
}
|
||||
}
|
||||
|
||||
export function libsql(credentials: LibSqlCredentials): LibsqlConnection {
|
||||
return new LibsqlConnection(credentials);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user