# QuestDB Client Library A QuestDB 10.2.0 client library for the Stock Bot trading platform, optimized for time-series data, parameterized PGWire queries, and intraday bar storage. ## Features - **Time-Series Optimized**: Built for QuestDB 10.2.0 (`LATEST ON`, `SAMPLE BY`, `ASOF JOIN`) - **Parameterized PGWire Queries**: All value filters bind `$1..$n` placeholders; structured identifiers and intervals are strictly validated - **Contract DDL**: Offline DDL generator for M2 intraday tables (`bars_1m`, `fx_1m`, `bar_revisions`) with `SYMBOL CAPACITY ... [NO]CACHE`, `PARTITION BY MONTH WAL`, and `DEDUP UPSERT KEYS` - **Typed `bars_1m` Range Reader**: `readBars1m()` binds `listing_id` as canonical decimal string and returns `INT32` `listing_id` values with `known_at` preserved - **InfluxDB Line Protocol**: Batch ingestion writer - **Health Monitoring**: Connection health monitoring and metrics ## Usage ### Parameterized Queries & `LATEST ON` ```typescript import { QuestDBClient, QuestDBQueryBuilder } from '@stock-bot/questdb'; const questClient = new QuestDBClient({ host: 'localhost', httpPort: 9000, pgPort: 8812, influxPort: 9009, database: 'qdb', }); await questClient.connect(); // Parameterized PGWire query via query builder const { sql, params } = QuestDBQueryBuilder.create(questClient) .select('listing_id', 'ts', 'close', 'volume') .from('bars_1m') .where('listing_id = $1', ['1042']) .latestOn('ts', 'listing_id') .buildParameterized(); // sql: // SELECT listing_id, ts, close, volume FROM bars_1m WHERE listing_id = $1 LATEST ON ts PARTITION BY listing_id // params: ['1042'] // Execute directly (forwards both SQL and bound params) const latestBars = await questClient.latestBy('bars_1m', ['listing_id', 'ts', 'close'], 'listing_id', { where: 'session = $1', params: ['RTH'], }); // Override the designated timestamp column explicitly const latestRevisions = await questClient.latestBy( 'bar_revisions', ['listing_id', 'revised_at', 'new_close'], 'listing_id', { timestampColumn: 'revised_at' } ); // Time-series aggregations with bound filters const hourlyStats = await questClient .aggregate('ohlcv_data') .selectAgg({ avg_close: 'avg(close)' }) .whereSymbol('AAPL') .whereLastHours(24) .groupBy('symbol') .sampleBy('1h') .execute(); ``` ### Reading `bars_1m` Ranges ```typescript import { readBars1m } from '@stock-bot/questdb'; // Queries `[from, to)` ordered by `ts ASC`, binding listingId as decimal string SYMBOL // and converting returned `listing_id` back to signed INT32. const bars = await readBars1m( questClient, 1042, new Date('2024-01-02T14:30:00.000Z'), new Date('2024-01-02T21:00:00.000Z') ); ``` > **Storage-layer read semantics**: `readBars1m` returns all stored bars in `[from, to)` and preserves `known_at` on each row without applying replay cutoffs. Research callers (`MarketDataStore`) remain responsible for as-of availability filtering and Vault fencing. ### M2 Intraday Contract DDL (Offline) ```typescript import { getM2IntradayDDL } from '@stock-bot/questdb'; const ddl = getM2IntradayDDL(); // ddl.bars_1m -> CREATE TABLE IF NOT EXISTS bars_1m (...) TIMESTAMP(ts) PARTITION BY MONTH WAL DEDUP UPSERT KEYS(ts, listing_id) // ddl.fx_1m -> CREATE TABLE IF NOT EXISTS fx_1m (...) TIMESTAMP(ts) PARTITION BY MONTH WAL DEDUP UPSERT KEYS(ts, pair) // ddl.bar_revisions -> CREATE TABLE IF NOT EXISTS bar_revisions (...) TIMESTAMP(revised_at) PARTITION BY MONTH WAL ``` M2 intraday tables are opt-in and are not auto-created during `QuestDBClient.connect()`. ## Configuration Configure using environment variables: ```env QUESTDB_HOST=localhost QUESTDB_HTTP_PORT=9000 QUESTDB_PG_PORT=8812 QUESTDB_INFLUX_PORT=9009 ``` ## Time-Series Features QuestDB 10.2.0 supports: - **High-frequency data**: Millions of data points per second - **Time-based partitioning**: Monthly WAL partitions for intraday tables - **ASOF JOINs**: Time-series specific joins - **SAMPLE BY**: Time-based aggregations - **LATEST ON**: Get latest values per partition key (`LATEST ON PARTITION BY `)