@fjall/clickhouse

中文文档
39.2.2发布于 2026/11/15SEE LICENSE IN LICENSETypeScriptES Module近 30 天下载 5559

Runtime helpers for ClickHouse migration containers — user provisioning, SQL file application, connection retry. Consumes the manifest contract published by @fjall/components-infrastructure ClickHouseDatabase.

原始使用文档
文档目录5 节
  1. Installation
  2. Env-var contract
  3. Usage
  4. Rolling your own consumer
  5. Licence

@fjall/clickhouse

Runtime helpers for ClickHouse migration containers. Consumes the user manifest published by @fjall/components-infrastructure ClickHouseDatabase and provisions workload users in writable users_local storage. Also ships a generic SQL-file runner and a connection-retry helper.

Installation

npm install @fjall/clickhouse @clickhouse/client

@clickhouse/client is a peer dependency — the consumer supplies the runtime client.

Env-var contract

The framework's commitment is the env-var shape, not this package's API. A customer rolling their own consumer reads the same env vars.

Env varShapeProducer
CLICKHOUSE_MANAGED_USERSJSON Array<string> of user namesClickHouseDatabase.getMigrationContributions()
USER_<NAME>_PASSWORDplaintext (one per managed-users entry)ECS executionRole + Secrets Manager (no runtime SDK call)

The manifest carries names only — the schema admin (XML-defined) is NOT included, and profile binding is the customer SQL's job (ALTER USER <name> SETTINGS PROFILE '<profile>').

The constant name + env-name helper + schema live in @fjall/util/migration:

import {
  CLICKHOUSE_MANAGED_USERS_ENV,
  ManagedUserNamesSchema,
  userPasswordEnvName,
} from "@fjall/util/migration";

This package re-exports them for convenience so consumers don't need two imports.

Usage

import {
  connectWithRetry,
  createConsoleMigrationLogger,
  createFjallClickHouseClient,
  provisionUsersFromEnv,
  runSqlMigrations,
} from "@fjall/clickhouse";

const logger = createConsoleMigrationLogger("migration-runner");
const client = createFjallClickHouseClient({
  url,
  username,
  password,
  database,
});

await connectWithRetry(client, { label: "user-provision", logger });
await provisionUsersFromEnv({ client, logger });
await runSqlMigrations({ client, dir: "./clickhouse-init", logger });

Build the client with createFjallClickHouseClient. It installs FjallClickHouseLogger at WARN, which writes each of the client's log records through @fjall/util's logger as one masked line under the component ClickHouseClient. A failed request's record names the server's error code, its class and, for a code this package knows, its type, never the server's message: that message can quote the failing statement and its literal values. An argument that carries a request's or a response's content prints as [withheld: content], whatever it holds: the arguments @clickhouse/client 1.18.0 to 1.24.1 use for the statement, the request's URL parameters with their bound values, a request's or a response's headers, and an error's message (read 2026-10-13). A message or argument value that carries a terminal control sequence prints as [withheld: control characters], and an argument whose key carries one, or a character JSON writes as an escape, is left out. @clickhouse/client's own createClient uses its DefaultLogger, which prints the raw error and the request's arguments to the console; its default level is OFF at @clickhouse/client-common 1.18.0 and WARN from 1.18.1 (read 2026-10-13). If you build the client yourself, pass log: { LoggerClass: FjallClickHouseLogger, level: ClickHouseLogLevel.WARN }, stating the level so that no version in the peer range drops the warnings.

One path is outside any logger's reach. When the values stream of a streamed insert fails, @clickhouse/client (every release from 1.18.0 to 1.24.1, pipelineCb in dist/utils/encoder.js, read 2026-10-13) prints the stream's error to console.error itself, raw, whichever logger is installed. That error is the one the caller's own stream raised; or the one encoding a row threw, in the client's JSON.stringify or in a json.stringify you supplied (a BigInt, a circular row, whose message names a property, or a row whose own toJSON throws); or a fixed Node error (an AbortError, or socket hang up) when the upload is cut short or times out. Destroy a values stream with an error that carries no row data, and write it only rows that encode.

provisionUsersFromEnv is idempotent — it issues CREATE USER IF NOT EXISTS then ALTER USER for every manifest entry, so repeated invocations are safe.

Rolling your own consumer

The env-var contract above is stable. A customer who prefers their own provisioner reads the same env vars; this package's helpers are convenience wrappers. The framework commits to the env shape, not to this package.

If rolling your own, you'll want to replicate:

  • single-quote-escape the password literal (' → '') before string-interpolating into IDENTIFIED WITH … BY '…'
  • mask passwords in any log output (use maskSensitiveOutput from @fjall/util)
  • order CREATE USER IF NOT EXISTS before ALTER USER so re-provisioning realigns drifted credentials
  • place provisioning BEFORE any migration-hash idempotency gate
  • regex-validate user names before string-interpolating into SQL (CH doesn't bind identifier parameters)

Licence

Proprietary — see LICENSE.

版本与发布记录

@fjall/clickhouse 共发布 150 个版本,首次发布于 2026/05/24;过去 12 个月发布了 150 个版本。

版本发布日期距今
39.2.2latest2026/11/15今天
39.2.12026/11/15今天
39.2.02026/11/124 天前
39.1.02026/11/114 天前
39.0.142026/11/114 天前
39.0.132026/11/087 天前

关于 @fjall/clickhouse 的常见问题

@fjall/clickhouse 是什么?

Runtime helpers for ClickHouse migration containers — user provisioning, SQL file application, connection retry. Consumes the manifest contract published by @fjall/components-infrastructure ClickHouseDatabase.

如何安装 @fjall/clickhouse?

在项目目录运行 npm install @fjall/clickhouse,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@clickhouse/client(^1.18.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fjall/clickhouse 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fjall/clickhouse 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fjall/clickhouse 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.3.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fjall/clickhouse 可以商用吗?

@fjall/clickhouse 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@fjall/clickhouse 的下载量有多少?

根据 npm 官方下载统计,@fjall/clickhouse 在 2026/10/15 – 2026/11/13 这 30 天内被下载 5,559 次,日均约 185 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fjall/clickhouse 还在维护吗?

最新版本 39.2.2 发布于 2026/11/15(今天);至今共发布 150 个版本,其中过去 12 个月发布了 150 个;首次发布于 2026/05/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。