mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-19 08:28:06 +02:00
34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { createPostgresQueryExecutor } from './postgres-query-executor.js';
|
|
import type {
|
|
KtxSqlQueryExecutionInput,
|
|
KtxSqlQueryExecutionResult,
|
|
KtxSqlQueryExecutorPort,
|
|
} from './query-executor.js';
|
|
import { createSqliteQueryExecutor } from './sqlite-query-executor.js';
|
|
|
|
export interface DefaultLocalQueryExecutorOptions {
|
|
postgres?: KtxSqlQueryExecutorPort;
|
|
sqlite?: KtxSqlQueryExecutorPort;
|
|
}
|
|
|
|
function driverFor(input: KtxSqlQueryExecutionInput): string {
|
|
return String(input.connection?.driver ?? '').toLowerCase();
|
|
}
|
|
|
|
export function createDefaultLocalQueryExecutor(options: DefaultLocalQueryExecutorOptions = {}): KtxSqlQueryExecutorPort {
|
|
const postgres = options.postgres ?? createPostgresQueryExecutor();
|
|
const sqlite = options.sqlite ?? createSqliteQueryExecutor();
|
|
|
|
return {
|
|
async execute(input: KtxSqlQueryExecutionInput): Promise<KtxSqlQueryExecutionResult> {
|
|
const driver = driverFor(input);
|
|
if (driver === 'postgres' || driver === 'postgresql') {
|
|
return postgres.execute(input);
|
|
}
|
|
if (driver === 'sqlite' || driver === 'sqlite3') {
|
|
return sqlite.execute(input);
|
|
}
|
|
throw new Error(`No local query executor is configured for driver "${input.connection?.driver ?? 'unknown'}".`);
|
|
},
|
|
};
|
|
}
|