@depyronick/clickhouse-client

中文文档
2.1.0发布于 2026/01/21MITTypeScript近 30 天下载 5101

ClickHouse Client for NodeJS.

原始使用文档
文档目录8 节
  1. clickhouse-client for NodeJS
  2. Installation
  3. Quick Start
  4. Importing the module
  5. Methods
  6. Notes
  7. Stay in touch
  8. License

clickhouse-client for NodeJS

ClickHouse® is an open-source, high performance columnar OLAP database management system for real-time analytics using SQL. ClickHouse combined with TypeScript helps you develop better type safety with your ClickHouse queries, giving you end-to-end typing.

Installation

Install the following package:

$ npm i --save @depyronick/clickhouse-client

Quick Start

Importing the module

Once the installation process is complete, you can import the ClickHouseClient

const { ClickHouseClient } = require('@depyronick/clickhouse-client');

// or:
// import { ClickHouseClient } from '@depyronick/clickhouse-client';

const analyticsServer = new ClickHouseClient({
  host: '127.0.0.1',
  password: '7h3ul71m473p4555w0rd'
});

// you can create multiple clients
const chatServer = new ClickHouseClient({
  host: '127.0.0.2',
  password: '7h3ul71m473p4555w0rd'
});

new ClickHouseClient(options: ClickHouseOptions) will create a ClickHouse client with the specified connection options.

See ClickHouseOptions object for more information.

Methods

Query

ClickHouseClient.query<T>(query: string): Observable<string | T>
this.analyticsServer.query('SELECT * FROM visits LIMIT 10').subscribe({
  error: (err) => {
    // called when an error occurred during query
  },
  next: (row) => {
    // if specified format is any of JSON formats, `row` here is the json representation of the row
    // if format is not any of JSON, then `row` represents string chunk from http stream
  },
  complete: () => {
    // called when stream is completed
  }
});
ClickHouseClient.queryPromise<T>(query: string): Promise<string | T[]>
this.analyticsServer
  .queryPromise('SELECT * FROM visits LIMIT 10')
  .then((rows) => {
    // if specified format is any of JSON formats, rows is an array of all retrieved rows
    // if not, then rows is the raw string result from clickhouse-server
  })
  .catch((err) => {
    // called when an error occurred during query
  });

// or

const rows = await this.analyticsServer.queryPromise(
  'SELECT * FROM visits LIMIT 10'
);

Query with params

Clickhouse-server supports performing queries with paramaters. Both query and queryPromise accept a second argument which respresents the query paramaters value as a Record<string, string | number>.

The query can contain parameters placeholders that have the following syntax:

{<name>:<data type>}
-- a parameter called "limit" that will be interpreted as an 8-bit unsigned integer
{limit:UInt8}

So, you can pass parameters as the following:

SELECT * FROM visits LIMIT {limit:UInt8}

Official documentation (HTTP Interface - Query with paramters)

ClickHouseClient.query<T>(query: string, params: Record<string, string | number>): Observable<string | T>
const yersterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

const params = {
  yesterday: yesterday.getTime(),
  osName: 'OSX'
};

const query =
  'SELECT * FROM visits WHERE timestamp >= {yesterday:DateTime} AND os = {osName:String} LIMIT 10';

this.analyticsServer.query(query, params).subscribe({
  error: (err) => {
    // called when an error occurred during query
  },
  next: (row) => {
    // if specified format is any of JSON formats, `row` here is the json representation of the row
    // if format is not any of JSON, then `row` represents string chunk from http stream
  },
  complete: () => {
    // called when stream is completed
  }
});
ClickHouseClient.queryPromise<T>(query: string, params: Record<string, string | number>): Promise<string | T[]>
const yersterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

const params = {
  yesterday: yesterday.getTime(),
  osName: 'OSX'
};

const query =
  'SELECT * FROM visits WHERE timestamp >= {yesterday:DateTime} AND os = {osName:String} LIMIT 10';

this.analyticsServer
  .queryPromise(query, params)
  .then((rows) => {
    // if specified format is any of JSON formats, rows is an array of all retrieved rows
    // if not, then rows is the raw string result from clickhouse-server
  })
  .catch((err) => {
    // called when an error occurred during query
  });

// or

const rows = await this.analyticsServer.queryPromise(query, params);

Insert

ClickHouseClient.insert<T>(table: string, data: T[]): Observable<void>

The insert method accepts two inputs.

  • table is the name of the table that you'll be inserting data to.
    • Table value could be prefixed with database like analytics_db.visits.
  • data: T[] array of JSON objects to insert.
analyticsServer
  .insert('visits', [
    {
      timestamp: new Date().getTime(),
      ip: '127.0.0.1',
      os: 'OSX',
      userAgent:
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/95.0.4638.69 Safari/537.36',
      version: '1.0.0'
    }
  ])
  .subscribe({
    error: (err) => {
      // called when an error occurred during insert
    },
    next: () => {
      // currently next does not emits anything for inserts
    },
    complete: () => {
      // called when insert is completed
    }
  });

ClickHouseClient.insertPromise<T>(table: string, data: T[]): Promise<void>

The insertPromise method accepts the same inputs as insert but returns a Promise, instead of Observable.

analyticsServer
  .insertPromise('visits', [
    {
      timestamp: new Date().getTime(),
      ip: '127.0.0.1',
      os: 'OSX',
      userAgent:
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/95.0.4638.69 Safari/537.36',
      version: '1.0.0'
    }
  ])
  .then(() => {
    // insert was success
  })
  .catch((err) => {
    // called when an error occurred during insert
  });
ClickHouseClient.insertRaw(table: string, data: string | Buffer | Readable, format: ClickHouseDataFormat): Observable<void>

Use insertRaw to send pre-formatted data such as CSV or JSONAsString.

// insert a raw JSON object into a single String column
await analyticsServer.insertRawPromise(
  'sensor_raw',
  '{"source":"test","uid":"test"}',
  ClickHouseDataFormat.JSONAsString
);

// insert CSV payload
await analyticsServer.insertRawPromise(
  'visits',
  'timestamp,ip,os\n1700000000,127.0.0.1,OSX\n',
  ClickHouseDataFormat.CSV
);

Other

ClickHouseClient.ping(timeout: number = 3000): Promise<boolean>

The ping method accepts one input.

  • timeout is the time in milliseconds to wait for the server to send ping response Ok.\n.
// if you're using async/await
const ping = await analyticsServer.ping();

// or

analyticsServer
  .then((pingResult) => {
    // ping result is a boolean
    // it will return `true` if we were able to receive `Ok.\n`
    // and `false` if anything but `Ok.\n`
  })
  .catch((reason) => {
    // reason is the full response of the error
    // see more details at https://axios-http.com/docs/handling_errors
  });

Notes

  • This repository will be actively maintained and improved.
  • Planning to implement TCP protocol, if ClickHouse decides to documentate it.
  • Planning to implement inserts with streams.
  • This library supports http response compressions such as brotli, gzip and deflate.

Stay in touch

License

MIT licensed.

版本与发布记录

@depyronick/clickhouse-client 共发布 21 个版本,首次发布于 2021/11/19;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.1.0latest2026/01/219 个月前
2.0.42024/01/292 年前
2.0.32023/12/072 年前
2.0.22023/04/193 年前
2.0.12023/02/203 年前
2.0.02022/10/074 年前

关于 @depyronick/clickhouse-client 的常见问题

@depyronick/clickhouse-client 是什么?

ClickHouse Client for NodeJS.

如何安装 @depyronick/clickhouse-client?

在项目目录运行 npm install @depyronick/clickhouse-client,或使用 pnpm add / yarn add / bun add。

@depyronick/clickhouse-client 支持 TypeScript 吗?

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

@depyronick/clickhouse-client 可以商用吗?

@depyronick/clickhouse-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@depyronick/clickhouse-client 的下载量有多少?

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

@depyronick/clickhouse-client 还在维护吗?

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