ClickHouse client with named parameter support (NOT exported - use factory function).
import { warehouseClientFromCredentials } from '@lightdash/warehouses';
import { WarehouseTypes } from '@lightdash/common';
const client = warehouseClientFromCredentials({
type: WarehouseTypes.CLICKHOUSE,
host: 'localhost',
port: 8123, // HTTP port
user: 'default',
password: 'mypassword',
schema: 'analytics', // Database name in ClickHouse
secure: true,
timeoutSeconds: 30,
});
await client.test();JSONCompactEachRowWithNamesAndTypes formatawait client.streamQuery(
`SELECT * FROM events
WHERE timestamp > {startDate: DateTime}
AND timestamp < {endDate: DateTime}
AND user_id = {userId: UInt64}
AND event_type = {eventType: String}`,
(data) => { /* process rows */ },
{
queryParams: {
startDate: '2024-01-01 00:00:00',
endDate: '2024-01-31 23:59:59',
userId: 12345,
eventType: 'page_view',
},
timezone: 'Europe/London',
tags: { source: 'app' },
}
);await client.streamQuery(
'SELECT * FROM events WHERE user_id IN {userIds: Array(UInt64)}',
(data) => { /* process rows */ },
{ queryParams: { userIds: [100, 200, 300] }, tags: {} }
);Note: ClickhouseSqlBuilder is NOT exported. Use factory:
import { warehouseSqlBuilderFromType } from '@lightdash/warehouses';
import { SupportedDbtAdapter } from '@lightdash/common';
const builder = warehouseSqlBuilderFromType(SupportedDbtAdapter.CLICKHOUSE);
builder.getFieldQuoteChar(); // '"'
builder.escapeString("O'Reilly"); // "O''Reilly"Percentile SQL:
// Median: median(col)
// 95th: quantile(0.95)(col)Important: In ClickHouse, "schema" = "database"
schema field = ClickHouse database namedatabase parameter in methods = always empty stringTags are added as SQL comments:
{ tags: { dashboard: 'analytics', user_id: '123' } }
// Adds: -- {"dashboard":"analytics","user_id":"123"}ClickhouseTypes enum is NOT exported. Types mapped automatically:
Int*, UInt*, Float*, Decimal* → NUMBERDate, Date32 → DATEDateTime, DateTime64 → TIMESTAMPBool → BOOLEANHandles:
Nullable() wrapper (stripped before mapping)LowCardinality() wrapper (stripped)Decimal(18, 2))secure: true for HTTPS (port 8443)JSONCompactEachRowWithNamesAndTypeswarehouseClientFromCredentials()