stock-bot/scripts/import-research-dataset.ts

42 lines
1.9 KiB
TypeScript

/** Explicit offline custody of an existing v2 dataset. No provider/calendar inference. */
import { readFile } from 'node:fs/promises';
import { Pool } from 'pg';
import { validateDatasetV2 } from '../apps/stock/orchestrator/src/data/historical-dataset-v2';
import { retainResearchDataset } from '../apps/stock/orchestrator/src/data/retained-research-dataset';
export { retainResearchDataset } from '../apps/stock/orchestrator/src/data/retained-research-dataset';
if (import.meta.main) {
let database: Pool | undefined;
try {
const args = process.argv.slice(2);
if (args.length !== 2 || args[0] !== '--file' || !args[1]) {
throw new Error(
'Usage: RESEARCH_DATASET_DATABASE_URL=<database> bun run scripts/import-research-dataset.ts --file <v2-dataset.json>'
);
}
const connectionString = process.env.RESEARCH_DATASET_DATABASE_URL;
if (
!connectionString ||
!['postgres:', 'postgresql:'].includes(new URL(connectionString).protocol)
) {
throw new Error(
'Set RESEARCH_DATASET_DATABASE_URL explicitly; this command retains research data'
);
}
const input: unknown = JSON.parse(await readFile(args[1], 'utf8'));
// Validate before opening a database connection. Existing startup migrations
// own the schema; import never updates/deletes retained data or migrates tables.
validateDatasetV2(input);
database = new Pool({ connectionString, connectionTimeoutMillis: 5000 });
const manifest = await retainResearchDataset(database, input);
process.stdout.write(
`${JSON.stringify({ hash: manifest.hash, barCount: manifest.barCount, eligibility: manifest.eligibility, limitations: manifest.limitations }, null, 2)}\n`
);
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exitCode = 1;
} finally {
await database?.end();
}
}