Skip to content

Latest commit

 

History

History

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ClickHouse JS client examples

Examples located directly in examples directory should work with both Node.js and Web versions of the client. The only difference will be in the import statement - @clickhouse/client vs @clickhouse/client-web.

Node.JS-specific examples are located in the examples/node directory.

Overview

We aim to cover various scenarios of client usage with these examples. You should be able to run any of these examples, see How to run section below.

If something is missing, or you found a mistake in one of these examples, please open an issue or a pull request, or contact us.

General usage

  • url_configuration.ts - client configuration using the URL parameters.
  • clickhouse_settings.ts - ClickHouse settings on the client side, both global and per operation.
  • ping.ts - sample checks if the server can be reached.
  • abort_request.ts - cancelling an outgoing request or a read-only query.
  • cancel_query.ts - cancelling a potentially long-running query on the server side.
  • long_running_queries_timeouts.ts - avoiding timeout errors for long-running queries (two different approaches).
  • read_only_user.ts - an example of using the client with a read-only user, with possible read-only user limitations highlights.
  • basic_tls.ts - (Node.js only) using certificates for basic TLS authentication.
  • mutual_tls.ts - (Node.js only) using certificates for mutual TLS authentication.

Creating tables

Inserting data

Selecting data

Data types

Special cases

How to run

Prerequisites

Environment requirements for all examples:

  • Node.js 18+
  • NPM
  • Docker Compose

Run ClickHouse in Docker from the root folder of this repository:

docker-compose up -d

This will create two local ClickHouse instances: one with plain authentication and one that requires TLS.

Any example except create_table_*

Change the working directory to examples and install the dependencies:

cd examples
npm i

Then, you should be able to run the sample programs, for example:

ts-node --transpile-only array_json_each_row.ts

TLS examples

You will need to add server.clickhouseconnect.test to your /etc/hosts to make it work, as self-signed certificates are used in these examples.

Execute the following command to add the required /etc/hosts entry:

sudo -- sh -c "echo 127.0.0.1 server.clickhouseconnect.test >> /etc/hosts"

After that, you should be able to run the examples:

ts-node --transpile-only node/basic_tls.ts
ts-node --transpile-only node/mutual_tls.ts

On-premise cluster examples

For create_table_on_premise_cluster.ts, you will need to start a local cluster first.

Run this command from the root folder of this repository:

docker-compose -f docker-compose.cluster.yml up -d

Now, you should be able to run the example:

ts-node --transpile-only create_table_on_premise_cluster.ts

ClickHouse Cloud examples

  • for *_cloud.ts examples, Docker containers are not required, but you need to set some environment variables first:
export CLICKHOUSE_URL=https://<your-clickhouse-cloud-hostname>:8443
export CLICKHOUSE_PASSWORD=<your-clickhouse-cloud-password>

You can obtain these credentials in the ClickHouse Cloud console (check the docs for more information).

Cloud examples assume that you are using the default user and database.

Run one of the Cloud examples:

ts-node --transpile-only create_table_cloud.ts