mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 12:37:20 +00:00
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
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 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;
|
|
}
|
|
}
|