113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Opt-in CLI to initialize M2 QuestDB 10.2.0 intraday tables (`bars_1m`,
|
|
* `fx_1m`, `bar_revisions`) via PGWire using `applyM2IntradayDDL()`.
|
|
*
|
|
* Connection env vars (checked in order):
|
|
* - `QUESTDB_URL` or `RESEARCH_MCP_QUESTDB_URL`
|
|
* - `QUESTDB_HOST` / `RESEARCH_MCP_QUESTDB_HOST` (default `localhost`)
|
|
* - `QUESTDB_PG_PORT` / `RESEARCH_MCP_QUESTDB_PG_PORT` (default `8812`)
|
|
* - `QUESTDB_DATABASE` / `RESEARCH_MCP_QUESTDB_DATABASE` (default `qdb`)
|
|
* - `QUESTDB_USER` / `RESEARCH_MCP_QUESTDB_USER`
|
|
* - `QUESTDB_PASSWORD` / `RESEARCH_MCP_QUESTDB_PASSWORD`
|
|
* - `QUESTDB_SSL` / `RESEARCH_MCP_QUESTDB_SSL` (`true` | `false`)
|
|
*/
|
|
|
|
import {
|
|
applyM2IntradayDDL,
|
|
assertNoConflictingQuestDbSsl,
|
|
openQuestDbPgExecutor,
|
|
type QuestDbPgExecutorConfig,
|
|
} from '@stock-bot/questdb';
|
|
|
|
function readEnv(
|
|
env: Readonly<Record<string, string | undefined>>,
|
|
primary: string,
|
|
fallback: string
|
|
): string | undefined {
|
|
const value = env[primary] ?? env[fallback];
|
|
if (value === undefined) {
|
|
return undefined;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : undefined;
|
|
}
|
|
|
|
function parseSslFlag(raw: string): boolean {
|
|
const normalized = raw.trim().toLowerCase();
|
|
if (normalized === 'true' || normalized === '1') {
|
|
return true;
|
|
}
|
|
if (normalized === 'false' || normalized === '0') {
|
|
return false;
|
|
}
|
|
throw new RangeError(
|
|
'Invalid QuestDB SSL configuration: expected "true", "false", "1", or "0".'
|
|
);
|
|
}
|
|
|
|
export function resolveQuestDbInitM2Config(
|
|
env: Readonly<Record<string, string | undefined>> = process.env
|
|
): QuestDbPgExecutorConfig {
|
|
const url = readEnv(env, 'QUESTDB_URL', 'RESEARCH_MCP_QUESTDB_URL');
|
|
const sslRaw = readEnv(env, 'QUESTDB_SSL', 'RESEARCH_MCP_QUESTDB_SSL');
|
|
const ssl = sslRaw !== undefined ? parseSslFlag(sslRaw) : undefined;
|
|
|
|
if (url !== undefined) {
|
|
if (ssl !== undefined) {
|
|
assertNoConflictingQuestDbSsl(url, ssl);
|
|
}
|
|
return {
|
|
url,
|
|
...(ssl !== undefined ? { ssl } : {}),
|
|
};
|
|
}
|
|
|
|
const host = readEnv(env, 'QUESTDB_HOST', 'RESEARCH_MCP_QUESTDB_HOST') ?? 'localhost';
|
|
const portRaw = readEnv(env, 'QUESTDB_PG_PORT', 'RESEARCH_MCP_QUESTDB_PG_PORT');
|
|
const port = portRaw !== undefined ? Number(portRaw) : 8812;
|
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
throw new RangeError(
|
|
`Invalid QuestDB PGWire port "${String(portRaw)}". Expected 1..65535.`
|
|
);
|
|
}
|
|
const database = readEnv(env, 'QUESTDB_DATABASE', 'RESEARCH_MCP_QUESTDB_DATABASE') ?? 'qdb';
|
|
const user = readEnv(env, 'QUESTDB_USER', 'RESEARCH_MCP_QUESTDB_USER');
|
|
const password = readEnv(env, 'QUESTDB_PASSWORD', 'RESEARCH_MCP_QUESTDB_PASSWORD');
|
|
|
|
return {
|
|
host,
|
|
port,
|
|
database,
|
|
...(user !== undefined ? { user } : {}),
|
|
...(password !== undefined ? { password } : {}),
|
|
...(ssl !== undefined ? { ssl } : {}),
|
|
};
|
|
}
|
|
|
|
export async function runQuestDbInitM2(
|
|
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
openExecutor: typeof openQuestDbPgExecutor = openQuestDbPgExecutor
|
|
): Promise<{ readonly appliedTables: readonly string[] }> {
|
|
const config = resolveQuestDbInitM2Config(env);
|
|
const handle = await openExecutor(config);
|
|
try {
|
|
return await applyM2IntradayDDL(handle.executor);
|
|
} finally {
|
|
await handle.close();
|
|
}
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
runQuestDbInitM2()
|
|
.then(result => {
|
|
process.stdout.write(
|
|
`Initialized QuestDB M2 intraday tables: ${result.appliedTables.join(', ')}\n`
|
|
);
|
|
})
|
|
.catch((err: unknown) => {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
process.stderr.write(`questdb-init-m2 failed: ${message}\n`);
|
|
process.exit(1);
|
|
});
|
|
}
|