clickhouse-toolkit
v3.0.0
Published
A customizable SQL query builder for ClickHouse databases.
Readme
ClickHouse Toolkit
A safety-first, composable TypeScript toolkit for building safe, ergonomic ClickHouse queries and migrations.
Table of Contents
- Features
- Installation
- Upgrading to 3.0.0
- Quick Start
- Query Builder
- Streaming
- Migrations
- Configuration
- API Reference
- Examples
- License
Features
- Fluent Query Builder - Chain methods for building complex SQL queries with full type safety
- ClickHouse-Aware - Built specifically for ClickHouse with support for FINAL, PREWHERE, arrays, maps, and more
- Advanced Migrator - Version-controlled migrations with drift detection, ON CLUSTER support, and dry-run capabilities
- Multiple Insert Strategies - Traditional SQL inserts, object-based inserts, and stream-based inserts for maximum flexibility
- Streaming Support - Built-in support for streaming large result sets with format validation and multiple formats
- CLI Tool - Command-line interface for running migrations and schema management
- Type Safety - Full TypeScript support with type inference and compile-time safety
Installation
npm install clickhouse-toolkitUpgrading to 3.0.0
3.0.0 closes two SQL-injection primitives. Both were verified against a live ClickHouse, and one of them was also silently corrupting data.
The examples below show injected SQL as a <placeholder> rather than a pasteable
payload — npm's publishing pipeline rejects a package whose README carries the literal
strings. The shape of each attack, and the field that carried it, is unchanged.
Identifiers must now be identifier-shaped
What changed. Until 3.0.0, a column/alias/table string containing ( was treated
as a function call and, outside predicate context, emitted completely unquoted and
unvalidated. Any caller-controlled "column name" was therefore arbitrary SQL. A
measure key of
v, (SELECT groupArray(concat(node_id,'=',toString(value))) FROM events) AS leakproduced a SELECT whose injected subquery ignored the outer query's row-level
scoping and returned every tenant's rows to the caller.
Identifiers are now validated. A string that is not identifier-shaped — letters,
digits, underscores and hyphens, not starting with a digit — throws a
ValidationError instead of being emitted. *, numeric literals (SELECT 1 FROM …)
and table.column keep working exactly as before.
Why a hard break rather than more escaping. The library already had an explicit
route for author-supplied SQL: Raw(sql). The ( passthrough was a second,
implicit, unvalidated route to the same capability, which left the renderer unable to
tell "an identifier the caller named" from "SQL the caller authored". Removing it
leaves one honest route, and the renderer can tell the difference again.
Migration. Wrap the expression in Raw() — or use the typed helpers, which were
always the better answer:
// Before (2.x) → After (3.0.0)
select(['sum(value)']) → select([Raw('sum(value)')]) // or select([Sum('value')])
select(['status', 'count()']) → select(['status', Raw('count()')])
select({ total: 'sum(amount)' }) → select({ total: Raw('sum(amount)') })
.having({ 'count()': Gt(5) }) → .having(Raw('count() > 5'))
.groupBy(['toDate(created_at)']) → .groupBy([Raw('toDate(created_at)')])
.orderBy([{ column: 'sum(v)', … }]) → .orderBy([{ column: Raw('sum(v)'), … }])The one-line rule: 'sum(value)' → Raw('sum(value)').
groupBy() and orderBy()'s column accept a RawExpr alongside plain identifier
strings — a non-breaking addition in 3.0.0, so expression-based grouping and
ordering keeps working through an explicit, greppable API. Keys of both kinds mix
freely:
select(['id'])
.from('events')
.groupBy(['node_id', Raw('toDate(created_at)')])
.orderBy([{ column: Raw('sum(value)'), direction: 'DESC' }])
// SELECT `id` FROM `events` GROUP BY `node_id`, toDate(created_at) ORDER BY sum(value) DESCAliasing the expression in the SELECT list and grouping by the alias remains a good
alternative, especially when the same expression is both selected and grouped:
select({ day: Raw('toDate(created_at)'), total: Sum('value') })
.from('events')
.groupBy(['day'])
.orderBy([{ column: 'total', direction: 'DESC' }])Note that Raw() bypasses all escaping by design. If the string is built from user
input, that input is now your responsibility — which is precisely the distinction 2.x
could not express.
Function arguments are quoted
What changed. Column names passed as function arguments were emitted unquoted,
which made the typed helpers themselves an injection vector — the opposite of what
callers reasonably assume of Sum() / Count() / Avg():
select({ leak: Sum("x) , (SELECT groupArray(node_id) FROM events") })
// 2.x: SELECT sum(x) , (SELECT groupArray(node_id) FROM events) AS `leak` FROM `events`
// 3.0.0: throws ValidationErrorArguments are now validated and quoted like any other identifier. The exemption existed only to make generated SQL prettier; nothing depended on it semantically.
Migration. Output changes cosmetically — sum(amount) is now sum(`amount`),
which is equivalent SQL. Only snapshot tests that compare SQL text are affected. An
argument that is deliberately an expression goes through Raw(), as everywhere else:
Sum(Raw('if(ok, value, 0)')).
LIMIT / OFFSET are validated, and LIMIT 0 now renders
What changed. Both values were interpolated straight into the SQL string with no runtime check. A type annotation is not a runtime boundary, so any caller arriving from outside TypeScript — plain JavaScript, a value parsed from JSON or a query string, a hand-built AST — could inject:
// `<appended SELECT>` stands for a second, attacker-chosen query smuggled in through a
// set operator; `<appended statement>` for a statement after the `;`, DDL included.
select(['id']).from('users').limit('1 <appended SELECT>')
// 2.x: SELECT `id` FROM `users` LIMIT 1 <appended SELECT>
// 3.0.0: throws ValidationError
select(['id']).from('users').limit(10, '5; <appended statement>')
// 2.x: ... LIMIT 10 OFFSET 5; <appended statement>
// 3.0.0: throws ValidationErrorBoth must now be non-negative safe integers. Negative and fractional values are rejected too: ClickHouse would reject the resulting SQL anyway, and failing here names the offending field instead of surfacing a server-side syntax error.
Separately, a correctness fix. The clause sat behind a truthiness test, so limit(0)
— a legitimate query meaning no rows — dropped LIMIT entirely and returned the
whole table. It now renders LIMIT 0. A query with no limit set is unchanged.
Migration. Pass numbers, not numeric strings: limit(Number(req.query.limit)), and
validate that value before it reaches the builder if it came from a request. If anything
relied on limit(0) meaning "unlimited", it was relying on a bug — omit the call instead.
Keyword and name fields are validated at render time
What changed. Several fields whose types declare a closed set — 'ASC' | 'DESC',
'ROWS' | 'RANGE', window names, SETTINGS keys, frame offsets — were interpolated raw,
because the type annotation was mistaken for a runtime guarantee. Types are erased at
runtime, so values from JSON, a query string, plain JavaScript or a hand-built AST walked
straight through. All are now validated where they are emitted.
The ORDER BY direction was the most dangerous, because it sits mid-query and so
swallowed every clause that followed:
select(['a'])
.from('e')
.orderBy([{ column: 'a', direction: 'ASC LIMIT 1 <appended SELECT> --' }])
.limit(5)
// 2.x: SELECT `a` FROM `e` ORDER BY `a` ASC LIMIT 1 <appended SELECT> -- LIMIT 5
// 3.0.0: throws ValidationErrorThe trailing -- deleted the query's own LIMIT 5. The same field on a window ORDER BY
had the identical hole. Also closed: window declaration names and OVER names
(.window('w) AS (), evil AS (', …)), SETTINGS keys on SELECT/UPDATE/DELETE
(a key of max_threads=1 FORMAT JSON followed by a comment marker injected a FORMAT
change and truncated the statement), and window frame type, bound literals and
offsets.
formatNumber is now type-checked too. isNaN() coercion rejected most non-numeric
strings by accident, but anything Number() could parse was emitted verbatim —
formatNumber('0x10') rendered the raw token 0x10.
Migration. None for valid input: directions, frame types and bound literals render
byte-identically, and lowercase spellings ('asc') are accepted and normalised to the
canonical form. You only need to act if you were passing something outside the declared
set. Window names and SETTINGS keys must be plain identifiers — letters, digits and
underscores, not starting with a digit — since both are emitted without backticks.
Join types, function names, and migrator names are validated
What changed. join() takes the join type as a caller parameter, and it was
uppercased and interpolated without being checked against the set its type declares:
select(['a']).from('e').join('inner JOIN Z ON <always true> --', 'p', Raw('e.id = p.id'))
// 2.x: SELECT `a` FROM `e` INNER JOIN Z ON <always true> -- JOIN `p` ON e.id = p.id
// 3.0.0: throws ValidationErrorThe trailing -- truncated the real join, so the query silently joined a table of the
caller's choosing. (strictness, on the same method, was already validated.) Function
names in the expression tree are now validated as identifiers too — the last raw path in
the renderer.
The migrator gets the same treatment, where the stakes are higher: these names reach
CREATE / ALTER / DROP and run with the migration runner's privileges, and both
arrive from configuration rather than from code.
migrationsTableName(CLICKHOUSE_MIGRATIONS_TABLE_NAME) is interpolated intoCREATE TABLE,SELECT,INSERT INTOandDELETE FROM. A value ofmigrations WHERE <always true> --turned the record-removal statement intoDELETE FROM migrations WHERE <always true> -- WHERE id = ?, dropping every migration record. It is now validated when theMigratoris constructed, so a bad value fails immediately rather than mid-migration. A qualifieddb.tableis still accepted.cluster(CLICKHOUSE_CLUSTER) is interpolated intoON CLUSTERfor everyCREATE/DROP/ALTER TABLE.
Migration. None for valid input: join types render byte-identically and are accepted case-insensitively, and ordinary table and cluster names are unaffected. Only values that were never valid identifiers now fail.
ON CLUSTER now works at all
What changed. ON CLUSTER was inserted before the table name, so setting
CLICKHOUSE_CLUSTER produced SQL ClickHouse cannot parse:
-- 2.x emitted this, which fails with SYNTAX_ERROR (code 62) at the CLUSTER token
CREATE TABLE ON CLUSTER prod_cluster t (a String) ENGINE = Memory
-- 3.0.0 emits the correct grammar
CREATE TABLE t ON CLUSTER prod_cluster (a String) ENGINE = MemoryIf you tried ON CLUSTER before and gave up because every DDL migration failed: that
was this bug, not your configuration. The feature has never worked, which is also why
changing the emitted SQL carries no compatibility risk — nothing could have depended on
output that always errored.
The clause now goes after the (optionally qualified) table name, for
CREATE TABLE [IF NOT EXISTS], DROP TABLE [IF EXISTS] and ALTER TABLE. Three
further problems in the old three-replace() approach are fixed with it:
- String literals and comments are no longer rewritten. A migration containing
-- CREATE TABLE fooin a comment, or seed data likeINSERT INTO log VALUES ('a; CREATE TABLE evil (x String)'), used to be mangled. - It no longer cascades. Text inserted by one pass was rewritten again by the next.
IF NOT EXISTS/IF EXISTSanddb.tableare handled, rather than pushing the clause in front of them.
A statement that already carries its own ON CLUSTER is left untouched, and rewriting
happens only at a statement head, so a DDL keyword appearing mid-statement is ignored.
Not covered: only TABLE statements are rewritten, matching the previous behaviour.
CREATE MATERIALIZED VIEW and CREATE DICTIONARY also accept ON CLUSTER but are left
alone deliberately — add the clause yourself in those migrations.
Migration. None, beyond DDL migrations with a cluster configured now succeeding where they previously failed.
Placeholders inside literals and comments are no longer substituted
What changed. injectValues replaced every ? in the SQL, with no idea which ones
were real parameter slots. A ? inside a string literal consumed a value:
formatter.injectValues("SELECT * FROM t WHERE label = 'why?' AND id = ?", [42, 'bob'])
// 2.x: SELECT * FROM t WHERE label = 'why42' AND id = 'bob'
// 3.0.0: SELECT * FROM t WHERE label = 'why?' AND id = 42Note what 2.x did there: the literal was corrupted and every later value bound one
position to the left, writing the wrong data to the wrong column — with nothing thrown.
The variant that failed with Not enough values provided was the lucky case, because at
least it was loud. validatePlaceholders counted with the same naive pattern, so it
agreed with the bug rather than catching it; both now share one set of rules.
Literals ('…', "…", backticks, including '' doubling and \' escapes) and --,
# and block comments are all skipped.
Migration. None, unless you were compensating for the bug by padding your value array to match the miscount — remove that padding.
QueryRunner.insert() validates its table and columns
What changed. insert() handed table and columns to @clickhouse/client, which
builds INSERT INTO <table> (<columns>) FORMAT <format> by interpolation, quoting and
validating neither. The builder's INSERT path had run both through quoteIdentifier all
along, so the same operation was guarded through one route and wide open through the
other — the same asymmetry this release removed from quoteIdentifier itself.
runner.insert({ table: 't (x) SELECT * FROM secrets --', values: [] })
// 3.0.0: throws ValidationErrorBoth are now validated at the toolkit's own boundary. Because neither is backtick-quoted
downstream, hyphens are not accepted here — a table named user-profiles works through
the query builder, which quotes it, but must be renamed to be used with insert(). A
qualified db.table is accepted.
String values escape backslashes
What changed. formatString doubled ' but left \ untouched. This was a
correctness bug before it was a security one: ClickHouse honours C-style escapes
inside single-quoted literals, so the value a\b was emitted as 'a\b' and read back
as a followed by a backspace.
With two caller-supplied values it was a full breakout, because the first value's trailing backslash consumed its own closing quote and the second supplied the SQL:
SELECT count() FROM events WHERE node_status = 'x\' AND device_status = ' OR <always true> --'That returned the entire table, and the trailing -- commented out every WHERE
clause after it.
Migration. None — values now round-trip correctly. If you had a workaround that pre-escaped backslashes before handing values to the builder, remove it, or those values will now be double-escaped.
Quick Start
import { select, createQueryRunner, Eq, Gt } from 'clickhouse-toolkit'
// Create a query runner
const runner = createQueryRunner({
url: 'http://localhost:8123',
username: 'default',
password: '',
database: 'my_database'
})
// Build and execute a query
const users = await select(['id', 'name', 'email', 'age'])
.from('users')
.where({
age: Gt(18),
status: Eq('active')
})
.orderBy([{ column: 'created_at', direction: 'DESC' }])
.limit(10)
.run(runner)
console.log(users)Query Builder
SELECT Queries
Build SELECT queries with a fluent, type-safe API:
import { select, Eq, In, Between, Like, IsNotNull } from 'clickhouse-toolkit'
// Simple SELECT
const users = await select(['id', 'name', 'email'])
.from('users')
.run(runner)
// SELECT with aliases
const results = await select(['u.id', 'u.name'])
.from('users', 'u')
.run(runner)
// SELECT with WHERE
const activeUsers = await select(['*'])
.from('users')
.where({
status: Eq('active'),
age: Between(18, 65),
country: In(['US', 'CA', 'UK']),
name: Like('%John%'),
email: IsNotNull()
})
.run(runner)
// SELECT with ORDER BY and LIMIT
const recentUsers = await select(['id', 'name', 'created_at'])
.from('users')
.orderBy([
{ column: 'created_at', direction: 'DESC' },
{ column: 'id', direction: 'ASC' }
])
.limit(20)
.run(runner)
// Object notation with aliases
const results = await select({
userId: 'id',
userName: 'name',
userEmail: 'email'
})
.from('users')
.run(runner)INSERT Queries
Insert data safely with multiple strategies and automatic value formatting:
Traditional Value-Based Inserts
import { insertInto } from 'clickhouse-toolkit'
// Insert single row
await insertInto('users')
.columns(['id', 'name', 'email', 'age'])
.values([[1, 'John Doe', '[email protected]', 30]])
.run(runner)
// Insert multiple rows
await insertInto('users')
.columns(['id', 'name', 'email', 'age'])
.values([
[1, 'John Doe', '[email protected]', 30],
[2, 'Jane Smith', '[email protected]', 25],
[3, 'Bob Johnson', '[email protected]', 35]
])
.run(runner)
// Insert with complex data types
await insertInto('users')
.columns(['id', 'name', 'tags', 'metadata', 'created_at'])
.values([[
1,
'John Doe',
['admin', 'developer'], // Array
{ department: 'Engineering', level: 'Senior' }, // Map
new Date('2024-01-15T10:30:00Z') // DateTime
]])
.run(runner)Object-Based Inserts
Insert JavaScript objects directly using ClickHouse client's native insert method:
import { insertInto } from 'clickhouse-toolkit'
// Insert single object
await insertInto('users')
.objects([{
id: 1,
name: 'John Doe',
email: '[email protected]',
age: 30
}])
.format('JSONEachRow')
.run(runner)
// Insert multiple objects
await insertInto('users')
.objects([
{ id: 1, name: 'John Doe', email: '[email protected]', age: 30 },
{ id: 2, name: 'Jane Smith', email: '[email protected]', age: 25 },
{ id: 3, name: 'Bob Johnson', email: '[email protected]', age: 35 }
])
.format('JSONEachRow')
.run(runner)Stream-Based Inserts
Insert data from Node.js streams for large datasets:
import { insertInto } from 'clickhouse-toolkit'
import { Readable } from 'stream'
// Insert from CSV stream
const csvData = '1,John Doe,[email protected],30\n2,Jane Smith,[email protected],25'
const csvStream = Readable.from([csvData], { objectMode: false })
await insertInto('users')
.fromStream(csvStream)
.format('CSV')
.run(runner)
// Insert from JSONEachRow stream
const jsonData = [
{ id: 1, name: 'John', email: '[email protected]' },
{ id: 2, name: 'Jane', email: '[email protected]' }
]
const jsonStream = Readable.from(jsonData, { objectMode: true })
await insertInto('users')
.fromStream(jsonStream)
.format('JSONEachRow')
.run(runner)
// Insert large dataset efficiently
const largeDataset = generateLargeDataset() // Your data generator
const dataStream = Readable.from(largeDataset, { objectMode: false })
await insertInto('events')
.fromStream(dataStream)
.format('JSONEachRow')
.run(runner)UPDATE Queries
Note: ClickHouse UPDATE operations are asynchronous mutations that process in the background.
import { update, Eq, Gt, In } from 'clickhouse-toolkit'
// Update single column
await update('users')
.set({ status: 'inactive' })
.where({ id: Eq(1) })
.run(runner)
// Update multiple columns
await update('users')
.set({
status: 'promoted',
salary: 75000.00
})
.where({ id: Eq(2) })
.run(runner)
// Update with complex conditions
await update('users')
.set({ status: 'verified' })
.where({
age: Gt(18),
country: In(['US', 'CA'])
})
.run(runner)
// Update with NULL
await update('users')
.set({ middle_name: null })
.where({ id: Eq(3) })
.run(runner)DELETE Queries
Note: ClickHouse DELETE operations are asynchronous mutations that process in the background.
import { deleteFrom, Eq, Gt, Between } from 'clickhouse-toolkit'
// Delete single row
await deleteFrom('users')
.where({ id: Eq(1) })
.run(runner)
// Delete with complex conditions
await deleteFrom('users')
.where({
status: Eq('inactive'),
last_login: Lt(new Date('2023-01-01'))
})
.run(runner)
// Delete with range
await deleteFrom('users')
.where({ age: Between(60, 100) })
.run(runner)Operators and Predicates
Available operators for building WHERE clauses:
import {
Eq, Neq, Gt, Gte, Lt, Lte, // Comparison
In, NotIn, // Membership
Between, // Range
Like, ILike, // Pattern matching
IsNull, IsNotNull, // NULL checks
And, Or, Not, // Logical combinators
EqCol // Column comparison
} from 'clickhouse-toolkit'
// Comparison operators
where({ age: Eq(25) })
where({ age: Gt(18) })
where({ age: Gte(21) })
where({ age: Lt(65) })
where({ age: Lte(60) })
where({ status: Neq('deleted') })
// Membership
where({ country: In(['US', 'CA', 'UK']) })
where({ role: NotIn(['guest', 'anonymous']) })
// Range
where({ age: Between(18, 65) })
// Pattern matching
where({ name: Like('%John%') })
where({ email: ILike('%@gmail.com') }) // Case-insensitive
// NULL checks
where({ deleted_at: IsNull() })
where({ email: IsNotNull() })
// Logical combinators
where({
age: And(Gte(18), Lt(65)),
status: Or(Eq('active'), Eq('pending'))
})
where({
status: Not(Eq('deleted'))
})
// Column comparison (for JOINs)
.innerJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')Joins
Build JOIN queries with type-safe conditions:
import { select, EqCol } from 'clickhouse-toolkit'
// INNER JOIN
const results = await select(['u.id', 'u.name', 'o.product_name', 'o.amount'])
.from('users', 'u')
.innerJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.run(runner)
// LEFT JOIN
const results = await select(['u.id', 'u.name', 'o.product_name'])
.from('users', 'u')
.leftJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.run(runner)
// Multiple JOINs
const results = await select(['u.name', 'o.product_name', 'p.price'])
.from('users', 'u')
.innerJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.innerJoin('products', { 'p.name': EqCol('o.product_name') }, 'p')
.run(runner)
// RIGHT JOIN
const results = await select(['*'])
.from('users', 'u')
.rightJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.run(runner)
// FULL JOIN
const results = await select(['*'])
.from('users', 'u')
.fullJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.run(runner)Aggregations
Perform aggregations with GROUP BY and HAVING:
import { select, Count, Sum, Avg, Min, Max, Gt } from 'clickhouse-toolkit'
// Simple aggregation
const results = await select({
country: 'country',
userCount: Count()
})
.from('users')
.groupBy(['country'])
.run(runner)
// Multiple aggregations
const results = await select({
userId: 'user_id',
totalAmount: Sum('amount'),
avgAmount: Avg('amount'),
minAmount: Min('amount'),
maxAmount: Max('amount'),
orderCount: Count()
})
.from('orders')
.groupBy(['user_id'])
.run(runner)
// With HAVING clause
const results = await select(['user_id', 'sum(amount) as total'])
.from('orders')
.groupBy(['user_id'])
.having({ total: Gt(1000) })
.run(runner)Subqueries
Use subqueries in WHERE clauses and SELECT lists:
import { select, In, Gt, Eq } from 'clickhouse-toolkit'
// Subquery in WHERE with IN
const userIdsWithOrders = select(['user_id']).from('orders')
const users = await select(['id', 'name'])
.from('users')
.where({ id: In(userIdsWithOrders) })
.run(runner)
// Subquery with comparison operators
const avgAge = select(['avg(age)']).from('users')
const olderUsers = await select(['id', 'name', 'age'])
.from('users')
.where({ age: Gt(avgAge) })
.run(runner)
// Scalar subquery in SELECT (non-correlated)
const results = await select({
id: 'id',
name: 'name',
avgAge: select(['avg(age)']).from('users')
})
.from('users')
.limit(10)
.run(runner)Note: Correlated subqueries are not supported due to ClickHouse limitations with the experimental feature.
ClickHouse-Specific Features
Take advantage of ClickHouse-specific optimizations:
import { select } from 'clickhouse-toolkit'
// Use FINAL modifier for ReplacingMergeTree
const results = await select(['id', 'name'])
.from('users')
.final()
.run(runner)
// Use PREWHERE for optimization
const results = await select(['*'])
.from('events')
.prewhere({ event_type: Eq('click') })
.where({ user_id: Eq(123) })
.run(runner)
// Apply query settings
const results = await select(['id', 'name'])
.from('users')
.settings({
max_execution_time: 30,
max_threads: 4,
max_memory_usage: 10000000000
})
.run(runner)Streaming
Stream large datasets efficiently with format options and validation:
import { select } from 'clickhouse-toolkit'
// Stream with default JSONEachRow format
const stream = await select(['id', 'name', 'email'])
.from('users')
.stream(runner)
const results: any[] = []
stream.on('data', (rows: any[]) => {
results.push(...rows)
})
stream.on('end', () => {
console.log(`Processed ${results.length} rows`)
})
stream.on('error', (error) => {
console.error('Stream error:', error)
})
// Stream with explicit format using .format() method
const stream = await select(['id', 'name', 'email'])
.from('users')
.format('JSONEachRow')
.stream(runner)
// Stream with CSV format
const csvStream = await select(['id', 'name', 'email'])
.from('users')
.format('CSV')
.stream(runner)
// Stream with TabSeparated format
const tsvStream = await select(['id', 'name', 'email'])
.from('users')
.format('TabSeparated')
.stream(runner)
// Stream with filters and settings
const stream = await select(['id', 'name', 'created_at'])
.from('users')
.where({ status: Eq('active') })
.settings({ max_execution_time: 60 })
.format('JSONEachRow')
.stream(runner)Supported Streaming Formats
The following formats are supported for streaming operations:
- JSONEachRow - One JSON object per line (default)
- JSONStringsEachRow - JSON with string values
- JSONCompactEachRow - Compact JSON format
- JSONCompactStringsEachRow - Compact JSON with string values
- JSONCompactEachRowWithNames - Compact JSON with column names
- JSONCompactEachRowWithNamesAndTypes - Compact JSON with names and types
- JSONCompactStringsEachRowWithNames - Compact JSON strings with names
- JSONCompactStringsEachRowWithNamesAndTypes - Compact JSON strings with names and types
- CSV - Comma-separated values
- CSVWithNames - CSV with header row
- CSVWithNamesAndTypes - CSV with header and types
- TabSeparated - Tab-separated values
- TabSeparatedRaw - Raw tab-separated values
- TabSeparatedWithNames - Tab-separated with header
- TabSeparatedWithNamesAndTypes - Tab-separated with header and types
Format Validation
The toolkit automatically validates that only streamable formats are used with the .stream() method:
// This works - JSONEachRow is streamable
const stream = await select(['id', 'name'])
.from('users')
.format('JSONEachRow')
.stream(runner)
// This throws an error - JSON is not streamable
const stream = await select(['id', 'name'])
.from('users')
.format('JSON')
.stream(runner) // Throws: Format 'JSON' is not streamableMigrations
CLI Usage
The toolkit includes a CLI for managing migrations:
Installation
# Install globally
npm install -g clickhouse-toolkit
# Or use with npx
npx clickhouse-toolkitConfiguration
Set environment variables:
export CLICKHOUSE_URL="http://localhost:8123"
export CLICKHOUSE_USERNAME="default"
export CLICKHOUSE_PASSWORD=""
export CLICKHOUSE_DATABASE="my_database"
export CLICKHOUSE_MIGRATIONS_TABLE_NAME="migrations" # optionalCommands
# Show migration status
clickhouse-toolkit migrate:status
# Show migration plan (dry-run)
clickhouse-toolkit migrate:plan
# Apply pending migrations
clickhouse-toolkit migrate:up
# Rollback last migration
clickhouse-toolkit migrate:down
# Rollback 3 migrations
clickhouse-toolkit migrate:down 3Programmatic Usage
Use migrations in your code:
import { Migrator, SimpleMigration, createQueryRunner } from 'clickhouse-toolkit'
const runner = createQueryRunner({
url: 'http://localhost:8123',
username: 'default',
password: '',
database: 'my_database'
})
const migrator = new Migrator(runner, {
migrationsTableName: 'migrations',
migrationsPath: './migrations',
migrationsPattern: '*.sql'
})
// Initialize
await migrator.init()
// Check status
const status = await migrator.status()
console.log(status)
// Apply migrations
await migrator.up()
// Rollback migrations
await migrator.down(1)
// Get migration plan
const plan = await migrator.plan()
plan.print()Migration Files
SQL Migrations
Create SQL migration files in the migrations directory:
migrations/001_create_users.sql:
-- up
CREATE TABLE users (
id UInt32,
name String,
email String,
age UInt8,
created_at DateTime DEFAULT now()
) ENGINE = MergeTree()
ORDER BY id;
-- down
DROP TABLE users;Class-based Migrations
Create programmatic migrations:
import { SimpleMigration } from 'clickhouse-toolkit'
const createUsersTable = new SimpleMigration(
'001_create_users',
'Create users table',
`CREATE TABLE users (
id UInt32,
name String,
email String
) ENGINE = MergeTree() ORDER BY id`,
`DROP TABLE users`
)
const migrator = new Migrator(runner, {
migrationClasses: [createUsersTable]
})Configuration
QueryRunner Options
interface QueryRunnerConfig {
url: string // ClickHouse HTTP URL
username: string // Username
password: string // Password
database: string // Database name
timeout?: number // Request timeout in ms (default: 30000)
retries?: number // Number of retries (default: 3)
settings?: Record<string, any> // Default ClickHouse settings
}Migrator Options
interface MigratorOptions {
migrationsTableName?: string // Default: 'clickhouse_toolkit_migrations'
migrationsPath?: string // Default: './migrations'
migrationsPattern?: string // Default: '*.sql'
tsMigrationsPath?: string // Default: './migrations'
tsMigrationsPattern?: string // Default: '*.ts'
migrationClasses?: Migration[] // Programmatic migrations
cluster?: string | null // Cluster name for ON CLUSTER
allowMutations?: boolean // Allow mutations (default: false)
dryRun?: boolean // Dry run mode (default: false)
}API Reference
Query Builders
select(columns?: string[] | Record<string, string | SelectBuilder>)- Create SELECT queryinsertInto(table: string)- Create INSERT queryupdate(table: string)- Create UPDATE querydeleteFrom(table: string)- Create DELETE query
InsertBuilder Methods
.columns(cols: string[])- Specify column names.values(vals: any[][])- Insert using value arrays (traditional SQL).value(row: any[])- Add single row to values.objects(data: Array<Record<string, any>>)- Insert JavaScript objects.fromStream(stream: NodeJS.ReadableStream)- Insert from Node.js stream.format(fmt: DataFormat)- Specify data format.run(runner?: QueryRunner)- Execute the insert
SelectBuilder Methods
.from(table: string, alias?: string)- Specify source table.where(conditions: Record<string, any>)- Add WHERE conditions.format(fmt: DataFormat)- Specify output format.stream(runner?: QueryRunner)- Create streaming query.run(runner?: QueryRunner)- Execute query and return results
Operators
Eq(value)- Equal toNeq(value)- Not equal toGt(value)- Greater thanGte(value)- Greater than or equalLt(value)- Less thanLte(value)- Less than or equalIn(values)- IN clauseNotIn(values)- NOT IN clauseBetween(start, end)- BETWEEN clauseLike(pattern)- LIKE patternILike(pattern)- Case-insensitive LIKEIsNull()- IS NULLIsNotNull()- IS NOT NULLAnd(...conditions)- AND combinatorOr(...conditions)- OR combinatorNot(condition)- NOT operatorEqCol(column)- Column equality (for JOINs)
Aggregation Functions
Count(column?)- COUNT aggregateSum(column)- SUM aggregateAvg(column)- AVG aggregateMin(column)- MIN aggregateMax(column)- MAX aggregate
ClickHouse Functions
arrayElement(arr, index)- Access array elementarrayLength(arr)- Get array lengtharrayConcat(arr1, arr2)- Concatenate arraysnow()- Current timestamptoday()- Current datetoDate(value)- Convert to dateformatDateTime(date, format)- Format datetime
And many more! See the source code for complete function list.
Examples
Complex Query with Everything
import { select, Eq, Gt, In, Between, And, Count, Sum } from 'clickhouse-toolkit'
const report = await select({
country: 'u.country',
userCount: Count(),
totalRevenue: Sum('o.amount'),
avgOrderValue: select(['avg(amount)']).from('orders')
})
.from('users', 'u')
.innerJoin('orders', { 'o.user_id': EqCol('u.id') }, 'o')
.where({
'o.status': Eq('completed'),
'u.age': Between(18, 65),
'u.country': In(['US', 'CA', 'UK'])
})
.groupBy(['u.country'])
.having({ totalRevenue: Gt(10000) })
.orderBy([{ column: 'totalRevenue', direction: 'DESC' }])
.limit(10)
.settings({
max_execution_time: 30,
max_threads: 4
})
.run(runner)Complete Migration Workflow
import { Migrator, SimpleMigration, createQueryRunner } from 'clickhouse-toolkit'
const runner = createQueryRunner({
url: process.env.CLICKHOUSE_URL,
username: process.env.CLICKHOUSE_USERNAME,
password: process.env.CLICKHOUSE_PASSWORD,
database: process.env.CLICKHOUSE_DATABASE
})
// Create migrations
const migrations = [
new SimpleMigration(
'001_create_users',
'Create users table',
`CREATE TABLE users (
id UInt32,
name String,
email String
) ENGINE = MergeTree() ORDER BY id`,
`DROP TABLE users`
),
new SimpleMigration(
'002_create_orders',
'Create orders table',
`CREATE TABLE orders (
id UInt32,
user_id UInt32,
amount Decimal(10, 2)
) ENGINE = MergeTree() ORDER BY id`,
`DROP TABLE orders`
)
]
const migrator = new Migrator(runner, {
migrationClasses: migrations,
migrationsPath: './migrations'
})
// Initialize and run
await migrator.init()
await migrator.up()
// Check status
const status = await migrator.status()
console.log(status)Streaming Large Datasets
import { select } from 'clickhouse-toolkit'
// Stream millions of rows efficiently
const stream = await select(['id', 'event_type', 'timestamp', 'data'])
.from('events')
.where({ event_type: Eq('click') })
.orderBy([{ column: 'timestamp', direction: 'DESC' }])
.settings({ max_execution_time: 300 })
.format('JSONEachRow')
.stream(runner)
let processedCount = 0
stream.on('data', (rows: any[]) => {
rows.forEach(row => {
// Process each row
console.log(`Processing event ${row.id}`)
processedCount++
})
})
stream.on('end', () => {
console.log(`Processed ${processedCount} events`)
})
stream.on('error', (error) => {
console.error('Stream error:', error)
})Object-Based Data Insertion
import { insertInto } from 'clickhouse-toolkit'
// Insert user data as objects
const users = [
{ id: 1, name: 'John Doe', email: '[email protected]', age: 30, active: true },
{ id: 2, name: 'Jane Smith', email: '[email protected]', age: 25, active: true },
{ id: 3, name: 'Bob Johnson', email: '[email protected]', age: 35, active: false }
]
await insertInto('users')
.objects(users)
.format('JSONEachRow')
.run(runner)
// Insert complex nested data
const events = [
{
id: 1,
user_id: 123,
event_type: 'click',
properties: { page: '/home', element: 'button' },
tags: ['web', 'mobile'],
timestamp: new Date('2024-01-15T10:30:00Z')
}
]
await insertInto('events')
.objects(events)
.format('JSONEachRow')
.run(runner)Stream-Based Data Insertion
import { insertInto } from 'clickhouse-toolkit'
import { Readable } from 'stream'
// Insert from CSV file stream
const csvData = '1,John Doe,[email protected],30\n2,Jane Smith,[email protected],25'
const csvStream = Readable.from([csvData], { objectMode: false })
await insertInto('users')
.fromStream(csvStream)
.format('CSV')
.run(runner)
// Insert large JSON dataset from stream
const jsonData = [
{ id: 1, name: 'User1', value: 100 },
{ id: 2, name: 'User2', value: 200 },
{ id: 3, name: 'User3', value: 300 }
]
const jsonStream = Readable.from(jsonData, { objectMode: true })
await insertInto('large_dataset')
.fromStream(jsonStream)
.format('JSONEachRow')
.run(runner)Data Type Support
ClickHouse Toolkit automatically handles formatting for various data types:
- Primitives: String, Number, Boolean, NULL
- Dates: Date objects → ClickHouse DateTime format
- Arrays: JavaScript arrays → ClickHouse Array syntax
[...] - Maps: Plain objects → ClickHouse Map syntax
{'key': 'value'} - Special Numbers: Infinity, -Infinity, NaN
Limitations
Known Limitations
- Correlated Subqueries - Not supported due to ClickHouse experimental feature limitations
- SQL Expressions in UPDATE - Only literal values supported in SET clause (no
salary * 1.1) - Async Mutations - UPDATE and DELETE are async in ClickHouse; immediate verification may not show changes
- Date Objects in UPDATE - Use string literals or raw SQL for complex datetime expressions
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License
