# chDB (Embedded ClickHouse) (/docs/chdb)

## Getting started [#getting-started]

<CodeBlock>
```typescript
import { createQueryBuilder } from '@hypequery/clickhouse';
import { Session } from 'chdb';
import { chdbAdapter } from 'chdb/hypequery';

const session = new Session('./analytics.chdb');   // or new Session() for in-memory
const db = createQueryBuilder<Schema>({ adapter: chdbAdapter({ session }) });

await db.table('trips')
  .where('passenger_count', 'gte', 2)
  .select(['passenger_count'])
  .count('trip_id', 'trip_count')
  .sum('total_amount', 'revenue')
  .groupBy(['passenger_count'])
  .orderBy('passenger_count', 'ASC')
  .execute();
```
</CodeBlock>

Nothing else changes: `toSQL()`, `rawQuery()`, and streaming all work, and the adapter renders SQL with hypequery's own exported helpers, so the queries are byte-identical to what the built-in HTTP adapter sends to a ClickHouse server.

## Why Run Embedded? [#why-run-embedded]

- **Testing and CI** — run your hypequery analytics against a real ClickHouse engine with no container and no shared staging environment. The builder code your tests exercise is the code you ship.
- **Local development** — build a dashboard against a local Parquet file or an in-memory database, then point the same code at remote ClickHouse for production by swapping the adapter.
- **Serverless** — chDB runs in any Node.js environment, including AWS Lambda (note: chDB's native binary is large; verify your deployment package stays within Lambda's 250 MB unzipped limit). Edge runtimes that run in V8 isolates (Cloudflare Workers, Vercel Edge Runtime) cannot load native Node.js addons and are not supported.
- **Local files and beyond** — chDB reads Parquet, CSV, S3, and more through ClickHouse table functions, queryable via `rawQuery()`.

Working in Python instead? See the [chDB Python tutorial](/blog/chdb-python-tutorial) and [Python datasets on chDB](/docs/python/datasets/chdb).

## Zero-Server Scaffolding [#zero-server-scaffolding]

`hypequery init` can scaffold a project straight onto the embedded engine — no server, no credentials, and no `.env` created or updated:

<CodeBlock>
```bash
npx hypequery init --database chdb
```
</CodeBlock>

You can also run `npx hypequery init` and choose **chDB (embedded, no server)**
from the interactive database prompt.

The scaffolded `client.ts` uses the adapter instead of an HTTP connection, `chdb` is installed as a dependency automatically, and the credential prompts are skipped. Choose an on-disk session directory (e.g. `./analytics.chdb`) to persist data between runs, or stay in-memory for a throwaway runtime sandbox. After creating tables in an on-disk session, pass that same directory when refreshing types:

<CodeBlock>
```bash
npx hypequery generate --database chdb --chdb-path ./analytics.chdb
```
</CodeBlock>

An in-memory session is discarded when its process exits. Use an on-disk path when tables created by your application must also be visible to a later `hypequery generate` process.

Installing `chdb` does not prevent the CLI from connecting to ClickHouse Cloud or another remote server. `CLICKHOUSE_*` environment variables and `.env` configuration take precedence over dependency-based chDB detection. If neither remote configuration nor a `chdb` dependency is present, generation asks you to select a database explicitly. You can also select the remote driver explicitly:

<CodeBlock>
```bash
npx hypequery generate --database clickhouse
```
</CodeBlock>

See the [CLI reference](/docs/reference/api/cli) for all `init` and `generate` flags.

## Setup [#setup]

<CodeBlock>
```bash
npm install chdb @hypequery/clickhouse
```
</CodeBlock>

chDB ships prebuilt binaries for Linux x64/arm64 (glibc) and macOS x64/arm64 — no node-gyp step. Windows isn't supported natively; use WSL2.

Swapping between embedded and remote is a one-line change:

<CodeBlock>
```typescript
// Local / test
const db = createQueryBuilder<Schema>({ adapter: chdbAdapter({ session }) });

// Production
const db = createQueryBuilder<Schema>({ url: process.env.CLICKHOUSE_URL, password: '...' });
```
</CodeBlock>

## How It Works [#how-it-works]

The adapter implements hypequery's `DatabaseAdapter` contract: hypequery compiles your query to SQL client-side, and the adapter executes it in-process via a chDB `Session`, returning `JSONEachRow` rows. The adapter is maintained by the chDB team — issues with the adapter itself belong on [chdb-io/chdb-node](https://github.com/chdb-io/chdb-node/issues).

## Differences from the HTTP adapter [#differences-from-the-http-adapter]

Checked with chdb 3.5.0:

- **64-bit integers arrive as numbers.** The HTTP adapter asks ClickHouse to quote `Int64`, `UInt64` and `count()` results, which is why hypequery types them as `string`. The chDB adapter sends no settings, so they come back as JSON numbers, which lose precision above 2^53. Set the same option once on the session to get strings, as the types expect:

<CodeBlock>
```typescript
const session = new Session('./analytics.chdb');
session.query('SET output_format_json_quote_64bit_integers = 1');
const db = createQueryBuilder<Schema>({ adapter: chdbAdapter({ session }) });
```
</CodeBlock>

- **No `insert()`.** The adapter implements `query()` and `stream()` only, so the builder's typed insert is not available. Load data with SQL on the session instead, for example `session.query("INSERT INTO trips SELECT * FROM file('trips.parquet')")`.