clickhouse-js-node-coding
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseClickHouse Node.js Client — Coding
ClickHouse Node.js 客户端编码开发
⚠️ Node.js runtime only. This skill covers thepackage running in a Node.js runtime exclusively — including Next.js Node runtime API routes, React Server Components, Server Actions, and standard Node.js processes. Do not apply this skill to browser client components, Web Workers, Next.js Edge runtime, Cloudflare Workers, or any usage of@clickhouse/client. For browser/edge environments, the correct package is@clickhouse/client-web.@clickhouse/client-web
⚠️ 仅适用于Node.js运行时。本技能仅涵盖在Node.js运行时中运行的包——包括Next.js Node运行时的API路由、React Server Components、Server Actions以及标准Node.js进程。请勿将此技能应用于浏览器客户端组件、Web Workers、Next.js Edge运行时、Cloudflare Workers或任何@clickhouse/client的使用场景。对于浏览器/边缘环境,应使用@clickhouse/client-web包。@clickhouse/client-web
How to Use This Skill
如何使用本技能
- Match the user's intent to a row in the Task Index below and read the corresponding reference file before writing code. After reading it, scan any Answer checklist in that reference and make sure the final answer covers each relevant item; those checklists capture details users usually need but are easy to omit in short answers.
- Always import from (never
@clickhouse/client) and create a client with@clickhouse/client-webor rely on supported defaults when appropriate. Close it withcreateClient({ url })preferably when it's no longer needed or during graceful shutdown for global resources.await client.close() - Prefer for typical row inserts/selects unless the user has already chosen another format or is streaming raw bytes (CSV / TSV / Parquet — see
JSONEachRow). Note onexamples/node/performance/: settings passed toclickhouse_settingsare defaults for every request; they can be overridden per-call by passingcreateClientdirectly toclickhouse_settings,insert(), orquery(). Always mention this when the user configures settings at the client level.command() - Always use for user-supplied values — never template- literal-interpolate them into SQL. See
query_params. When answering a parameter-binding question, your response must explicitly name template-literal interpolation as a "SQL injection risk" — even when the user only asked about syntax and did not raise security. The literal phrase "SQL injection" needs to appear; this is the most common mistake from PostgreSQL/MySQL users and the security framing is part of the correct answer, not an optional aside.reference/query-parameters.md - Pick the right method for the job:
- — write rows.
client.insert() - +
client.query()/resultSet.json()/.text()— read rows that return data..stream() - — DDL and other statements that don't return rows (
client.command(),CREATE,DROP,TRUNCATE,ALTERin a session, etc.).SET - — when you need the raw response stream of an arbitrary statement (rare in coding scenarios).
client.exec() - — health check; returns
client.ping(), never throws on connection failure.{ success, error? }
- Note version constraints when relevant. Examples:
- config option: client
pathname.>= 1.0.0 - values in
BigInt: clientquery_params.>= 1.15.0 - and JS
TupleParaminMap: clientquery_params.>= 1.9.0 - Configurable /
json.parse: clientjson.stringify.>= 1.14.0 - /
Timedata types: ClickHouse serverTime64.>= 25.6 - /
Dynamic/ newVarianttypes: ClickHouse serverJSON/>= 24.1/24.5(no longer experimental since24.8).25.3
- 匹配用户意图与下方任务索引中的对应条目,编写代码前先阅读相关参考文档。阅读后,查看参考文档中的答案检查清单,确保最终回答涵盖所有相关项;这些清单包含了用户通常需要但在简短回答中容易遗漏的细节。
- 始终从导入(切勿使用
@clickhouse/client),使用@clickhouse/client-web创建客户端,或在合适时依赖支持的默认配置。当客户端不再需要时,或在全局资源优雅关闭期间,最好使用createClient({ url })关闭客户端。await client.close() - 对于常规的行插入/查询,优先使用格式,除非用户已选择其他格式或正在流式传输原始字节(CSV/TSV/Parquet——详见
JSONEachRow)。 关于examples/node/performance/的说明:传递给clickhouse_settings的设置是所有请求的默认配置;可以通过在createClient、insert()或query()调用中直接传入command()来覆盖这些默认配置。当用户在客户端级别配置设置时,务必提及这一点。clickhouse_settings - 用户提供的值务必使用——切勿通过模板字面量将其插入SQL中。详见
query_params。 回答参数绑定相关问题时,必须明确指出模板字面量插值存在"SQL injection"风险——即使用户仅询问语法而未提及安全问题。必须出现"SQL injection"这个短语;这是PostgreSQL/MySQL用户最常犯的错误,安全提示是正确回答的一部分,而非可选补充。reference/query-parameters.md - 为任务选择合适的方法:
- — 写入行数据。
client.insert() - +
client.query()/resultSet.json()/.text()— 读取返回数据的行。.stream() - — 执行DDL和其他不返回行的语句(如会话中的
client.command()、CREATE、DROP、TRUNCATE、ALTER等)。SET - — 当你需要获取任意语句的原始响应流时使用(在编码场景中很少用到)。
client.exec() - — 健康检查;返回
client.ping(),连接失败时不会抛出异常。{ success, error? }
- 相关时注意版本限制。示例:
- 配置选项:客户端版本
pathname。>= 1.0.0 - 中的
query_params值:客户端版本BigInt。>= 1.15.0 - 中的
query_params和JSTupleParam:客户端版本Map。>= 1.9.0 - 可配置的/
json.parse:客户端版本json.stringify。>= 1.14.0 - /
Time数据类型:ClickHouse服务器版本Time64。>= 25.6 - /
Dynamic/ 新Variant类型:ClickHouse服务器版本JSON/>= 24.1/24.5(自24.8版本起不再是实验性功能)。25.3
Task Index
任务索引
Identify the user's task and read the matching reference file.
| Task | Triggers / symptoms | Reference file |
|---|---|---|
| Configure / connect the client | Building a | |
| Ping the server | Health checks, readiness probes, "is ClickHouse up?" | |
| Choose an insert format | "Which format should I use to insert?", JSON vs raw, | |
| Insert into a subset of columns / different database | | |
| Insert values, expressions, dates, decimals | | |
| Async inserts (server-side batching) | | |
| Select and parse results | | |
| Parameterize queries | Binding values, special characters / escaping, "SQL injection?", | |
| Sessions & temporary tables | | |
| Modern data types | | |
| Custom JSON parse/stringify | Plug in | |
识别用户的任务并阅读对应的参考文档。
| 任务 | 触发场景/症状 | 参考文档 |
|---|---|---|
| 配置/连接客户端 | 编写 | |
| Ping服务器 | 健康检查、就绪探针、"ClickHouse是否正常运行?" | |
| 选择插入格式 | "我应该使用哪种格式插入数据?"、JSON与原始格式对比、 | |
| 插入到部分列/不同数据库 | 使用 | |
| 插入值、表达式、日期、小数 | 带SQL函数的 | |
| 异步插入(服务器端批量处理) | 设置 | |
| 查询并解析结果 | 读取 | |
| 参数化查询 | 值绑定、特殊字符/转义、"SQL injection风险?"、 | |
| 会话与临时表 | | |
| 现代数据类型 | | |
| 自定义JSON解析/序列化 | 接入 | |
Conventions used in answers
回答中的约定
- Always show (Node, never Web).
import { createClient } from '@clickhouse/client' - Always at the end of self-contained snippets; in long-running services, close on graceful shutdown.
await client.close() - For inserts, prefer and
format: 'JSONEachRow'unless the user's scenario requires otherwise.values: [...] - For selects, prefer for small / medium result sets; for bigger results suggest streaming.
await (await client.query({...})).json<RowType>() - When showing parameter binding, use ClickHouse's native syntax — never
{name: Type},$1, or?.:name - For DDL inside a cluster or behind a load balancer, set
on the
clickhouse_settings: { wait_end_of_query: 1 }call so the server only acknowledges after the change is applied. See https://clickhouse.com/docs/en/interfaces/http/#response-buffering.command()
- 始终展示(Node环境,切勿使用Web版本)。
import { createClient } from '@clickhouse/client' - 在独立代码片段末尾始终添加;在长期运行的服务中,在优雅关闭时关闭客户端。
await client.close() - 插入数据时,优先使用和
format: 'JSONEachRow',除非用户场景有特殊要求。values: [...] - 查询数据时,对于中小型结果集,优先使用;对于大型结果集,建议使用流式处理。
await (await client.query({...})).json<RowType>() - 展示参数绑定时,使用ClickHouse原生的语法——切勿使用
{name: Type}、$1或?。:name - 在集群内或负载均衡器后的环境中执行DDL时,在调用中设置
command(),以便服务器在变更生效后再返回确认。详见https://clickhouse.com/docs/en/interfaces/http/#response-buffering。clickhouse_settings: { wait_end_of_query: 1 }
Out of scope
超出范围的内容
This skill covers day-to-day coding against (Node).
The following topics are intentionally not covered here:
@clickhouse/client- Errors, hangs, type mismatches, proxy pathname surprises, log silence,
socket hang-ups, → use the
ECONNRESETskill.clickhouse-js-node-troubleshooting - Streaming, Parquet, file streams, server-side bulk moves, progress
streaming, async-insert throughput tuning — see
.
examples/node/performance/ - TLS, RBAC / read-only users, deeper SQL-injection guidance — see
.
examples/node/security/ - patterns, deployment-shaped connection strings, replication / sharding choices — see
CREATE TABLE.examples/node/schema-and-deployments/ - Browser, Web Worker, Next.js Edge, Cloudflare Workers — use
and see
@clickhouse/client-web.examples/web/
本技能涵盖基于(Node环境)的日常编码内容。以下主题不在本技能的覆盖范围内:
@clickhouse/client- 错误、挂起、类型不匹配、代理路径异常、日志无输出、套接字挂起、→ 使用
ECONNRESET技能。clickhouse-js-node-troubleshooting - 流式处理、Parquet、文件流、服务器端批量迁移、进度流式传输、异步插入吞吐量调优——详见。
examples/node/performance/ - TLS、RBAC/只读用户、深入的SQL注入指导——详见。
examples/node/security/ - 模式、部署相关的连接字符串、复制/分片选择——详见
CREATE TABLE。examples/node/schema-and-deployments/ - 浏览器、Web Worker、Next.js Edge、Cloudflare Workers——使用并详见
@clickhouse/client-web。examples/web/
Still Stuck?
仍有疑问?
- — the runnable corpus this skill is built on.
examples/node/coding/ - ClickHouse JS client docs
- ClickHouse supported formats
- ClickHouse data types
- — 本技能基于的可运行代码示例集合。
examples/node/coding/ - ClickHouse JS客户端文档
- ClickHouse支持的格式
- ClickHouse数据类型