---
name: clickhouse-sdk-patterns
description: |-
  Production-ready patterns for @clickhouse/client — streaming inserts, typed
  queries, error handling, and connection management.
  Use when building robust ClickHouse integrations, implementing streaming
  inserts or low-memory streaming reads, or establishing team coding standards.
  Trigger with "clickhouse SDK patterns", "clickhouse client patterns",
  "clickhouse best practices", "clickhouse streaming insert".
allowed-tools: Read
version: 1.7.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
  - saas
  - database
  - analytics
  - clickhouse
  - olap
compatibility: Designed for Claude Code
title: clickhouse-sdk-patterns
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/examples.md), [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/implementation.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/clickhouse-sdk-patterns`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/clickhouse-sdk-patterns`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/clickhouse-sdk-patterns --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/clickhouse-sdk-patterns --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# ClickHouse SDK Patterns

## Overview

Production patterns for `@clickhouse/client` — typed queries, streaming inserts,
error handling, and connection lifecycle management. Start from the typed query
helper below, then drill into `references/implementation.md` for the streaming,
batching, and lifecycle patterns.

## Prerequisites

- `@clickhouse/client` installed and authenticated (see `clickhouse-install-auth`)
- Node.js 18+ with a `CLICKHOUSE_HOST` / `CLICKHOUSE_USER` / `CLICKHOUSE_PASSWORD` env set
- Familiarity with async/await and Node.js streams (backpressure, `drain`, `Readable`)

## Instructions

Apply the pattern that fits your workload. Steps 2–7 live in
[references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/implementation.md) with full,
copy-pasteable code; the core typed-query skeleton stays here.

1. **Typed query helper** — the foundation every other pattern builds on. Define
   a generic `query<T>` wrapper that returns parsed rows (skeleton below).
2. **Streaming insert (backpressure-safe)** — stream large inserts through a
   `Readable` instead of buffering in memory; honor `drain`.
3. **Batch insert with retry** — chunk rows (default 10k) with exponential-backoff
   retries, returning `{ inserted, errors }`.
4. **Streaming SELECT (low memory)** — consume large result sets as an
   `AsyncGenerator` so you never load the full set into RAM.
5. **Error handling** — distinguish server-side `ClickHouseError` (code + message)
   from network/client errors and normalize into a structured result.
6. **Connection lifecycle** — flush pending inserts on `SIGTERM` via
   `client.close()`; expose a `ping()`-based health check.
7. **Per-query settings** — override `max_threads`, `max_memory_usage`,
   `max_execution_time`, and `max_result_rows` for heavy queries.

### Skeleton: Typed Query Helper

```typescript
import { createClient } from '@clickhouse/client';

const client = createClient({
  url: process.env.CLICKHOUSE_HOST!,
  username: process.env.CLICKHOUSE_USER ?? 'default',
  password: process.env.CLICKHOUSE_PASSWORD ?? '',
});

// Generic typed query — returns parsed JSON rows
async function query<T>(sql: string, params?: Record<string, unknown>): Promise<T[]> {
  const rs = await client.query({
    query: sql,
    query_params: params,
    format: 'JSONEachRow',
  });
  return rs.json<T>();
}
```

**Note on parameterized queries:** ClickHouse uses `{name:Type}` syntax for
parameters, not `$1` or `?`. Always use typed parameters to prevent SQL injection.

## Output

Applying these patterns produces:

- A single reusable `client` instance plus a generic `query<T>` helper that
  returns typed, parsed rows.
- Streaming insert/read paths that keep memory flat regardless of dataset size.
- A batch-insert result object `{ inserted: number; errors: Error[] }` you can act
  on programmatically.
- Normalized error results (`CH-<code>: <message>` for server-side failures) rather
  than raw thrown exceptions.
- Graceful shutdown that flushes pending inserts before the process exits.

## Error Handling

Map common ClickHouse server error codes to a corrective action:

| Error Code | Meaning | Action |
|------------|---------|--------|
| `SYNTAX_ERROR (62)` | Bad SQL | Fix query syntax |
| `UNKNOWN_TABLE (60)` | Table doesn't exist | Check table name, database |
| `TOO_MANY_SIMULTANEOUS_QUERIES (202)` | Connection overload | Reduce concurrency or pool |
| `MEMORY_LIMIT_EXCEEDED (241)` | Query uses too much RAM | Add filters, use streaming |
| `TIMEOUT_EXCEEDED (159)` | Query too slow | Optimize ORDER BY, add indexes |

Full `safeQuery` wrapper (server-vs-client error discrimination) is in
[references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/implementation.md) under Pattern 5.

## Examples

Worked, runnable usage of each helper is in
[references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/examples.md). Quick look — a typed aggregation
query with named parameters:

```typescript
interface EventCount {
  event_type: string;
  cnt: string;  // ClickHouse JSON returns numbers as strings
}

const rows = await query<EventCount>(
  'SELECT event_type, count() AS cnt FROM events WHERE user_id = {user_id:UInt64} GROUP BY event_type',
  { user_id: 42 }
);
```

See [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/examples.md) for streaming reads and
structured error-result usage.

## Resources

- [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/implementation.md) — full code for patterns 2–7 + format table
- [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-sdk-patterns/references/examples.md) — worked, runnable usage examples
- [Node.js Client Docs](https://clickhouse.com/docs/integrations/javascript)
- [Client Examples (GitHub)](https://github.com/ClickHouse/clickhouse-js/tree/main/examples)
- [Query Settings Reference](https://clickhouse.com/docs/operations/settings/settings)

## Next Steps

Apply these patterns in `clickhouse-core-workflow-a` for real data modeling, then
tune query cost and concurrency with `clickhouse-cost-tuning` and
`clickhouse-performance-tuning`.
