bknd
Version:
Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.
104 lines (103 loc) • 4.47 kB
TypeScript
import { type AliasableExpression, type ColumnBuilderCallback, type ColumnDataType, type Compilable, type CompiledQuery, type DatabaseIntrospector, type Dialect, type Kysely, type KyselyPlugin, type OnModifyForeignAction, type QueryResult, type SelectQueryBuilder, type SelectQueryNode } from "kysely";
import type { jsonArrayFrom, jsonBuildObject, jsonObjectFrom } from "kysely/helpers/sqlite";
import type { BaseIntrospector, BaseIntrospectorConfig } from "./BaseIntrospector";
import type { DB } from "../..";
import type { Constructor } from "../../core/registry/Registry";
import { KyselyPluginRunner } from "../../data/plugins/KyselyPluginRunner";
import type { Field } from "../../data/fields/Field";
export type QB = SelectQueryBuilder<any, any, any>;
export type IndexMetadata = {
name: string;
table: string;
isUnique: boolean;
columns: {
name: string;
order: number;
}[];
};
export interface SelectQueryBuilderExpression<O> extends AliasableExpression<O> {
get isSelectQueryBuilder(): true;
toOperationNode(): SelectQueryNode;
}
export type SchemaResponse = [string, ColumnDataType, ColumnBuilderCallback] | undefined;
export declare const FieldSpecTypes: readonly ["text", "integer", "real", "blob", "date", "datetime", "timestamp", "boolean", "json"];
export type FieldSpec = {
type: (typeof FieldSpecTypes)[number];
name: string;
nullable?: boolean;
dflt?: any;
unique?: boolean;
primary?: boolean;
references?: string;
onDelete?: OnModifyForeignAction;
onUpdate?: OnModifyForeignAction;
};
export type IndexSpec = {
name: string;
columns: string[];
unique?: boolean;
};
export type DbFunctions = {
jsonObjectFrom: typeof jsonObjectFrom;
jsonArrayFrom: typeof jsonArrayFrom;
jsonBuildObject: typeof jsonBuildObject;
};
export type ConnQuery = CompiledQuery | Compilable;
export type ConnQueryResult<T extends ConnQuery> = T extends CompiledQuery<infer R> ? QueryResult<R> : T extends Compilable<infer R> ? QueryResult<R> : never;
export type ConnQueryResults<T extends ConnQuery[]> = {
[K in keyof T]: ConnQueryResult<T[K]>;
};
export type Features = {
batching: boolean;
softscans: boolean;
};
export declare abstract class Connection<Client = unknown> {
fn: Partial<DbFunctions>;
protected plugins: KyselyPlugin[];
abstract name: string;
protected initialized: boolean;
protected pluginRunner: KyselyPluginRunner;
protected readonly supported: Partial<Features>;
kysely: Kysely<DB>;
client: Client;
constructor(kysely: Kysely<any>, fn?: Partial<DbFunctions>, plugins?: KyselyPlugin[]);
init(): Promise<void>;
/**
* This is a helper function to manage Connection classes
* coming from different places
* @param conn
*/
static isConnection(conn: unknown): conn is Connection;
getIntrospector(): BaseIntrospector;
supports(feature: keyof typeof this.supported): boolean;
ping(): Promise<boolean>;
protected transformResultRows(result: any[]): Promise<any[]>;
/**
* Execute a query and return the result including all metadata
* returned from the dialect.
*/
executeQueries<O extends ConnQuery[]>(...qbs: O): Promise<ConnQueryResults<O>>;
executeQuery<O extends ConnQuery>(qb: O): Promise<ConnQueryResult<O>>;
protected getCompiled(...qbs: ConnQuery[]): CompiledQuery[];
protected withTransformedRows<Key extends string = "rows", O extends {
[K in Key]: any[];
}[] = []>(result: O, _key?: Key): Promise<O>;
protected validateFieldSpecType(type: string): type is FieldSpec["type"];
abstract getFieldSchema(spec: FieldSpec, strict?: boolean): SchemaResponse;
toDriver(value: unknown, field: Field): unknown;
fromDriver(value: any, field: Field): unknown;
close(): Promise<void>;
}
export declare function customIntrospector<T extends Constructor<Dialect>>(dialect: T, introspector: Constructor<DatabaseIntrospector>, options?: BaseIntrospectorConfig): {
create(...args: ConstructorParameters<T>): {
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
createDriver(): import("kysely").Driver;
createQueryCompiler(): import("kysely").QueryCompiler;
createAdapter(): import("kysely").DialectAdapter;
} & Dialect;
};
export declare class DummyConnection extends Connection {
name: string;
constructor();
getFieldSchema(): SchemaResponse;
}