Open sourceApache 2.0

ClickHouse Node.js

Use ClickHouse from Node.js without hand-maintained query types

Scripts, workers, and Express servers all end up querying ClickHouse. hypequery gives them generated types and one place for each query, so a helper buried in a service file can become a typed API when it needs to.

Runtime

Node.js and Bun

Frameworks

Express, Next.js, custom Node servers

Type source

Generated from schema

Node services accumulate query helpers fast

A server codebase rarely has just one query caller. Jobs, workers, route handlers, and admin tasks all want the same data, and each one tends to grow its own helper if there is no shared path.

The HTTP layer keeps getting rebuilt

Once a query needs an endpoint, teams start writing validation, auth handling, error shaping, and docs around it. That is a lot of repeated backend work for what should still be one analytics query.

Type mismatches hide inside server code

Node developers often spot the runtime mismatch later because the wrong shape is hidden inside service code and only surfaces after it has already passed through several layers.

Setup

Start with a typed backend query layer, not another helper module

Generate the schema once, initialize the builder in your backend, and make that the standard way Node code reaches ClickHouse. That gives scripts, workers, and services the same typed starting point.

  • Works well in Node.js services, scripts, and Bun runtimes
  • Schema types generated from your live ClickHouse instance
  • Correct ClickHouse-to-Node.js type mappings — no guessing
  • Composable query builder with support for typed filters, joins, CTEs, and raw SQL when needed
  • Optional HTTP serving layer via @hypequery/serve handlers

Setup

A backend entry point you can reuse across services and jobs

setup.ts
import { createQueryBuilder } from '@hypequery/clickhouse';
import type { DB } from './schema'; // generated from your ClickHouse schema

const db = createQueryBuilder<DB>({
  url: process.env.CLICKHOUSE_URL!, // e.g. http://localhost:8123
  username: process.env.CLICKHOUSE_USER!,
  password: process.env.CLICKHOUSE_PASSWORD!,
  database: 'analytics',
});

// db is fully typed — autocomplete on table names and column names

The point of this setup is consistency. Once the builder exists in one standard place, the rest of the Node codebase stops reinventing how it talks to ClickHouse.

With Express

Promote a query into an endpoint without burying logic in the route

When Node code needs to expose analytics over HTTP, keep the query definition separate from the web framework and mount the handler around it. That keeps the route thin and the analytics logic reusable.

That separation is what makes the Node page distinct from the framework pages. The query can still run directly inside a job or script, and only becomes an HTTP concern when you actually need a server surface.

If your app is specifically App Router-first, use the Next.js page. This page is for the general backend case where Express-style or standalone Node runtimes are still the center of gravity.

Express integration

A thin Express mount around a shared analytics definition

with-express.ts
import express from 'express';
import { createNodeHandler, initServe } from '@hypequery/serve';
import { z } from 'zod';

const app = express();

const { query, serve } = initServe({
  context: () => ({ db }),
});

const pageViews = query({
  input: z.object({ from: z.string(), to: z.string() }),
  query: async ({ ctx, input }) =>
    ctx.db
      .table('events')
      .where('event_name', 'eq', 'page_view')
      .where('created_at', 'gte', input.from)
      .where('created_at', 'lte', input.to)
      .groupBy(['page'])
      .count('id', 'views')
      .orderBy('views', 'DESC')
      .execute(),
});

const api = serve({ queries: { pageViews } });
app.all('/api/analytics/{*path}', createNodeHandler(api.handler));

The useful part is not the middleware line by itself. It is that the route is no longer where the analytics logic lives.

Where teams usually get stuck

Questions teams ask

What this page is really for

It is for backend teams whose main concern is service code, jobs, and server routes, not client hooks or App Router rendering.

Where Node teams usually feel pain

Usually when the same query appears in a worker, a route, and a report generator and every copy evolves separately.

Why the server layer matters

Backend teams end up paying for every decision twice if query logic and route logic are fused together. This page is about keeping them separate.

Where to branch next

Use the Next.js page for App Router specifics and the REST page if the endpoint layer itself is your main concern.

Examples

Runnable examples

Every example runs against the same seeded ClickHouse dataset. Scaffold one with npx @hypequery/cli create <slug> -y, or browse the full list.

Recipe · TypeScript

ClickHouse async inserts from Node.js: per-row vs batch vs async_insert

Insert the same 1,000 rows from Node per row, in client batches and with async_insert, and count the parts each creates: 1,000, 4, 10 and 1.

Node.jsClickHouseIngestionReal time analytics

Open recipe

Guide · ClickHouse 25.8

ClickHouse with Docker Compose: a pinned, persistent local server with a million-row sample dataset

Run ClickHouse 25.8 in Docker Compose with a healthcheck, a password, volumes and a 1M-row sample dataset, then connect from Node, Python and hypequery.

Quantile

Open guide

Example · TypeScript

A ClickHouse REST API with OpenAPI docs, API keys and rate limits

Put a typed REST API in front of ClickHouse: named GET endpoints with validated params, API-key auth, rate limits, and an OpenAPI spec with live docs.

Node.jsHonoClickHouseAPIAnalytics API

Open example

Example · TypeScript

ClickHouse materialized view rollups: incremental vs refreshable

Pre-aggregate a million events two ways, incremental and refreshable, and query both through one typed hypequery dataset.

Node.jsClickHousePre-aggregationDashboardsTime series

Open example

Example · TypeScript

A governed ClickHouse MCP server: agents query the semantic layer, not the database

Give Claude Code, Cursor or any MCP client ClickHouse analytics through four governed tools: only published datasets, no SQL, one tenant per server.

Node.jsMCPClickHouseAI agentsMulti-tenantSemantic layer

Open example

Example · TypeScript

Multi-tenant embedded analytics on ClickHouse with signed tokens

Embed tenant-scoped ClickHouse analytics in your product: your server mints a short-lived token, hypequery enforces the tenant on every query.

Node.jsHonoClickHouseEmbedded analyticsMulti-tenantSaaS analytics

Open example

Example · TypeScript

Add ClickHouse analytics to a Postgres + Drizzle app

Keep accounts in Postgres with Drizzle, serve usage from ClickHouse with hypequery, and join the two by account id in one API route.

Node.jsHonoDrizzlePostgresClickHouseSaaS analyticsUsage analyticsPostgres to ClickHouse

Open example

Next step

Replace one backend query path end to end

Pick one script, worker, or route that currently owns its own ClickHouse query code, move it onto the shared builder, and then decide whether it also deserves a served API surface.

Open source, and it runs in your own backend. Want it hosted?

Get early access to hypequery cloud.

Cloud hosts the same model you run in your backend, with a managed API and MCP server, versions, and rollback. We're letting teams in a few at a time. Join the waitlist and we'll reach out when your project can go live.

How do you want to build your analytics layer? (Optional)
What features do you most need? (Optional)

Pick as many as you like.

Only your email is required. All other questions are optional.