clickhouse-ts
This module is used by LookFor.sale which produces WB Analytics
⚠️ Module Using this module might lead to some unhandled errors. Please, report Me about issues you faced!
How to contact me
TS client for clickhouse database with using redis/nodejs caching rows for bulk insertion
import { Clickhouse } from 'clickhouse-ts'
import fs from 'fs'
import dayjs from 'dayjs'
import { serializeError } from 'serialize-error'
import Redis from 'ioredis'
const client = new Clickhouse(
{
url: 'url',
port: 8443,
user: 'user',
password: 'password',
database: 'database',
ca: fs.readFileSync('cert.crt')
},
{
cache: {
client: 'redis',
redis: {
instance: new Redis(),
options: {
ttlMs: dayjs().add(2, 'hours').unix()
}
},
chunkSize: 100_000,
chunkCollectorIntervalMs: 100_000
},
defaultResponseFormat: 'JSON',
clickhouseOptions: {
send_progress_in_http_headers: '1'
}
}
)
client
.query('WITH now() as t SELECT t', {
responseFormat: 'TSV',
})
.then(result => result.data.data)
.catch(e => serializeError(e))
client.query(`
CREATE TABLE strings (
date DateTime('UTC'),
string String
) Engine = ReplacingMergeTree()
PARTITION BY toMonday(date)
ORDER BY (date, string)
`)
const response = client
.insert(
'strings',
[{ date: '2021-01-01', string: 'str1' }, { date: '2021-01-02', string: 'str2' }],
{
responseFormat: 'CSVWithNames'
}
)
.then(response => response)
.catch(e => serializeError(e))
response