mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added BaseIntrospector and reorganized connections
This commit is contained in:
50
app/src/data/connection/sqlite/SqliteConnection.ts
Normal file
50
app/src/data/connection/sqlite/SqliteConnection.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { ColumnDataType, ColumnDefinitionBuilder, Kysely, KyselyPlugin } from "kysely";
|
||||
import { jsonArrayFrom, jsonBuildObject, jsonObjectFrom } from "kysely/helpers/sqlite";
|
||||
import { Connection, type DbFunctions, type FieldSpec, type SchemaResponse } from "../Connection";
|
||||
|
||||
export class SqliteConnection extends Connection {
|
||||
constructor(kysely: Kysely<any>, fn: Partial<DbFunctions> = {}, plugins: KyselyPlugin[] = []) {
|
||||
super(
|
||||
kysely,
|
||||
{
|
||||
...fn,
|
||||
jsonArrayFrom,
|
||||
jsonObjectFrom,
|
||||
jsonBuildObject,
|
||||
},
|
||||
plugins,
|
||||
);
|
||||
}
|
||||
|
||||
override supportsIndices(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
override getFieldSchema(spec: FieldSpec): SchemaResponse {
|
||||
this.validateFieldSpecType(spec.type);
|
||||
let type: ColumnDataType = spec.type;
|
||||
|
||||
switch (spec.type) {
|
||||
case "json":
|
||||
type = "text";
|
||||
break;
|
||||
}
|
||||
|
||||
return [
|
||||
spec.name,
|
||||
type,
|
||||
(col: ColumnDefinitionBuilder) => {
|
||||
if (spec.primary) {
|
||||
return col.primaryKey().notNull().autoIncrement();
|
||||
}
|
||||
if (spec.references) {
|
||||
let relCol = col.references(spec.references);
|
||||
if (spec.onDelete) relCol = relCol.onDelete(spec.onDelete);
|
||||
if (spec.onUpdate) relCol = relCol.onUpdate(spec.onUpdate);
|
||||
return relCol;
|
||||
}
|
||||
return spec.nullable ? col : col.notNull();
|
||||
},
|
||||
] as const;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user