T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing

This commit is contained in:
Studio 2026-12-04 15:06:51 -05:00
parent a3a31f7562
commit 0c33d8ae19
23 changed files with 2440 additions and 82 deletions

View file

@ -0,0 +1,80 @@
import { Pool, type PoolConfig } from 'pg';
export interface QuestDbPgExecutorConfig {
readonly url?: string;
readonly host?: string;
readonly port?: number;
readonly database?: string;
readonly user?: string;
readonly password?: string;
readonly ssl?: boolean;
readonly max?: number;
readonly connectionTimeoutMillis?: number;
readonly idleTimeoutMillis?: number;
}
export interface QuestDbPgQueryExecutor {
query(
sql: string,
params?: readonly unknown[]
): Promise<{ readonly rows: readonly Record<string, unknown>[] }>;
}
export interface QuestDbPgExecutorHandle {
readonly executor: QuestDbPgQueryExecutor;
close(): Promise<void>;
}
/**
* Opens a lightweight PGWire `pg.Pool` executor for QuestDB read queries or
* opt-in DDL without creating legacy tables or starting background health timers.
*/
export async function openQuestDbPgExecutor(
config: QuestDbPgExecutorConfig
): Promise<QuestDbPgExecutorHandle> {
const poolConfig: PoolConfig = {
max: config.max ?? 4,
connectionTimeoutMillis: config.connectionTimeoutMillis ?? 5_000,
idleTimeoutMillis: config.idleTimeoutMillis ?? 30_000,
...(config.url !== undefined
? { connectionString: config.url }
: {
host: config.host ?? 'localhost',
port: config.port ?? 8812,
database: config.database ?? 'qdb',
...(config.user !== undefined ? { user: config.user } : {}),
...(config.password !== undefined ? { password: config.password } : {}),
}),
...(config.ssl !== undefined ? { ssl: config.ssl } : {}),
};
const pool = new Pool(poolConfig);
try {
await pool.query('SELECT 1');
} catch (err: unknown) {
await pool.end().catch(() => undefined);
throw err;
}
let closePromise: Promise<void> | null = null;
const close = (): Promise<void> => {
if (closePromise === null) {
closePromise = pool.end();
}
return closePromise;
};
const executor: QuestDbPgQueryExecutor = {
async query(sql: string, params?: readonly unknown[]) {
const result =
params !== undefined && params.length > 0
? await pool.query(sql, [...params])
: await pool.query(sql);
return {
rows: result.rows as readonly Record<string, unknown>[],
};
},
};
return { executor, close };
}