mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-16 08:25:14 +02:00
refactor(cli): route local query execution through driver registry
This commit is contained in:
parent
d0fa77970c
commit
ad2471da74
1 changed files with 32 additions and 7 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { driverRegistrations, getDriverRegistration } from './drivers.js';
|
||||
import { createPostgresQueryExecutor } from './postgres-query-executor.js';
|
||||
import type {
|
||||
KtxSqlQueryExecutionInput,
|
||||
|
|
@ -5,6 +6,7 @@ import type {
|
|||
KtxSqlQueryExecutorPort,
|
||||
} from './query-executor.js';
|
||||
import { createSqliteQueryExecutor } from './sqlite-query-executor.js';
|
||||
import type { KtxConnectionDriver } from '../scan/types.js';
|
||||
|
||||
export interface DefaultLocalQueryExecutorOptions {
|
||||
postgres?: KtxSqlQueryExecutorPort;
|
||||
|
|
@ -15,20 +17,43 @@ function driverFor(input: KtxSqlQueryExecutionInput): string {
|
|||
return String(input.connection?.driver ?? '').toLowerCase();
|
||||
}
|
||||
|
||||
function localExecutorMap(
|
||||
options: DefaultLocalQueryExecutorOptions,
|
||||
): Partial<Record<KtxConnectionDriver, KtxSqlQueryExecutorPort>> {
|
||||
const wiredExecutors: Partial<Record<KtxConnectionDriver, KtxSqlQueryExecutorPort>> = {
|
||||
postgres: options.postgres ?? createPostgresQueryExecutor(),
|
||||
sqlite: options.sqlite ?? createSqliteQueryExecutor(),
|
||||
};
|
||||
|
||||
const executors: Partial<Record<KtxConnectionDriver, KtxSqlQueryExecutorPort>> = {};
|
||||
for (const registration of Object.values(driverRegistrations)) {
|
||||
if (!registration.hasLocalQueryExecutor) continue;
|
||||
const executor = wiredExecutors[registration.driver];
|
||||
if (executor) {
|
||||
executors[registration.driver] = executor;
|
||||
}
|
||||
}
|
||||
return executors;
|
||||
}
|
||||
|
||||
export function createDefaultLocalQueryExecutor(options: DefaultLocalQueryExecutorOptions = {}): KtxSqlQueryExecutorPort {
|
||||
const postgres = options.postgres ?? createPostgresQueryExecutor();
|
||||
const sqlite = options.sqlite ?? createSqliteQueryExecutor();
|
||||
const executors = localExecutorMap(options);
|
||||
|
||||
return {
|
||||
async execute(input: KtxSqlQueryExecutionInput): Promise<KtxSqlQueryExecutionResult> {
|
||||
const driver = driverFor(input);
|
||||
if (driver === 'postgres') {
|
||||
return postgres.execute(input);
|
||||
const registration = getDriverRegistration(driver);
|
||||
if (!registration?.hasLocalQueryExecutor) {
|
||||
throw new Error(`No local query executor is configured for driver "${input.connection?.driver ?? 'unknown'}".`);
|
||||
}
|
||||
if (driver === 'sqlite') {
|
||||
return sqlite.execute(input);
|
||||
|
||||
const executor = executors[registration.driver];
|
||||
if (!executor) {
|
||||
throw new Error(
|
||||
`Local query executor flag is enabled for driver "${registration.driver}", but no executor factory is wired.`,
|
||||
);
|
||||
}
|
||||
throw new Error(`No local query executor is configured for driver "${input.connection?.driver ?? 'unknown'}".`);
|
||||
return executor.execute(input);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue