various fixes: refactored imports, introduced fromDriver/toDriver to improve compat

This commit is contained in:
dswbx
2025-06-13 21:15:29 +02:00
parent cc038a0a9a
commit 2ada4e9f20
15 changed files with 100 additions and 35 deletions

View File

@@ -20,6 +20,7 @@ import {
import type { BaseIntrospector, BaseIntrospectorConfig } from "./BaseIntrospector";
import type { Constructor, DB } from "core";
import { KyselyPluginRunner } from "data/plugins/KyselyPluginRunner";
import type { Field } from "data/fields/Field";
export type QB = SelectQueryBuilder<any, any, any>;
@@ -200,6 +201,14 @@ export abstract class Connection<Client = unknown> {
abstract getFieldSchema(spec: FieldSpec, strict?: boolean): SchemaResponse;
toDriver(value: unknown, field: Field): unknown {
return value;
}
fromDriver(value: any, field: Field): unknown {
return value;
}
async close(): Promise<void> {
// no-op by default
}

View File

@@ -1,6 +1,8 @@
import type { TestRunner } from "core/test";
import { Connection, type FieldSpec } from "./Connection";
// @todo: add various datatypes: string, number, boolean, object, array, null, undefined, date, etc.
export function connectionTestSuite(
testRunner: TestRunner,
{

View File

@@ -57,6 +57,6 @@ export class LibsqlConnection extends SqliteConnection<Client> {
}
}
export function libsql(credentials: LibSqlCredentials): LibsqlConnection {
export function libsql(credentials: Client | LibSqlCredentials): LibsqlConnection {
return new LibsqlConnection(credentials);
}

View File

@@ -11,7 +11,9 @@ import { Connection, type DbFunctions, type FieldSpec, type SchemaResponse } fro
import type { Constructor } from "core";
import { customIntrospector } from "../Connection";
import { SqliteIntrospector } from "./SqliteIntrospector";
import type { Field } from "data/fields/Field";
// @todo: add pragmas
export type SqliteConnectionConfig<
CustomDialect extends Constructor<Dialect> = Constructor<Dialect>,
> = {
@@ -80,4 +82,24 @@ export abstract class SqliteConnection<Client = unknown> extends Connection<Clie
},
] as const;
}
override toDriver(value: unknown, field: Field): unknown {
if (field.type === "boolean") {
return value ? 1 : 0;
}
if (typeof value === "undefined") {
return null;
}
return value;
}
override fromDriver(value: any, field: Field): unknown {
if (field.type === "boolean" && typeof value === "number") {
return value === 1;
}
if (value === null) {
return undefined;
}
return value;
}
}