From 1b8e3d31dba7b8724b937f2e8acbcd5408d59b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Wed, 17 Apr 2024 12:47:26 +0200 Subject: [PATCH] update clickhouse --- packages/db/package.json | 2 +- packages/db/src/clickhouse-client.ts | 21 +++++++++------------ pnpm-lock.yaml | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/db/package.json b/packages/db/package.json index a6ab07d0..4e96ad0c 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -13,7 +13,7 @@ }, "dependencies": { "@clerk/nextjs": "^4.29.7", - "@clickhouse/client": "^0.2.9", + "@clickhouse/client": "^1.0.1", "@openpanel/common": "workspace:*", "@openpanel/constants": "workspace:*", "@openpanel/redis": "workspace:*", diff --git a/packages/db/src/clickhouse-client.ts b/packages/db/src/clickhouse-client.ts index 0bb69c2b..9a08bccf 100644 --- a/packages/db/src/clickhouse-client.ts +++ b/packages/db/src/clickhouse-client.ts @@ -1,33 +1,30 @@ +import type { ResponseJSON } from '@clickhouse/client'; import { createClient } from '@clickhouse/client'; export const ch = createClient({ - host: process.env.CLICKHOUSE_URL, + url: process.env.CLICKHOUSE_URL, username: process.env.CLICKHOUSE_USER, password: process.env.CLICKHOUSE_PASSWORD, database: process.env.CLICKHOUSE_DB, - max_open_connections: 100, + max_open_connections: 10, + keep_alive: { + enabled: true, + }, }); -interface ClickhouseJsonResponse { - data: T[]; - rows: number; - statistics: { elapsed: number; rows_read: number; bytes_read: number }; - meta: { name: string; type: string }[]; -} - export async function chQueryWithMeta>( query: string -): Promise> { +): Promise> { const res = await ch.query({ query, }); - const json = await res.json>(); + const json = await res.json(); return { ...json, data: json.data.map((item) => { const keys = Object.keys(item); return keys.reduce((acc, key) => { - const meta = json.meta.find((m) => m.name === key); + const meta = json.meta?.find((m) => m.name === key); return { ...acc, [key]: diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb0a4b12..eefa7ebb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -813,8 +813,8 @@ importers: specifier: ^4.29.7 version: 4.29.7(next@14.2.1)(react-dom@18.2.0)(react@18.2.0) '@clickhouse/client': - specifier: ^0.2.9 - version: 0.2.9 + specifier: ^1.0.1 + version: 1.0.1 '@openpanel/common': specifier: workspace:* version: link:../common @@ -2902,6 +2902,10 @@ packages: resolution: {integrity: sha512-ecXcegMbT4HYNWtGcfyidW6lNVRqPogbFMY5kfjJmz4IXJ4WZbQMwj2IQgemwFwE7jyia2OEwPIVfw1sNfDHRA==} dev: false + /@clickhouse/client-common@1.0.1: + resolution: {integrity: sha512-3L6e0foP6VOktScoi6XWMjJyOpKCWgLUYgPVxP2c7gm6Kotq+iRmmmXtXTSg7B7uozcLZycTtPfIw2d80SYsYw==} + dev: false + /@clickhouse/client@0.2.9: resolution: {integrity: sha512-KqQlO9vZNSLyhMWG9+0/VXqcUZrNk1Hybr9icgI/nLCoX8RD19BJsakZJj38IQvQxNUTxvcItm/kyu/gD/9LXA==} engines: {node: '>=16'} @@ -2909,6 +2913,13 @@ packages: '@clickhouse/client-common': 0.2.9 dev: false + /@clickhouse/client@1.0.1: + resolution: {integrity: sha512-fluUNnE2R7COJ6rn6DorYfi4D+AQn3t2qeBtEq37bQV3pD4EbKrBfKAwJ13e1lmMWdQ2B9bJUTMqGsRIDdWhJw==} + engines: {node: '>=16'} + dependencies: + '@clickhouse/client-common': 1.0.1 + dev: false + /@emnapi/runtime@0.45.0: resolution: {integrity: sha512-Txumi3td7J4A/xTTwlssKieHKTGl3j4A1tglBx72auZ49YK7ePY6XZricgIg9mnZT4xPfA+UPCUdnhRuEFDL+w==} requiresBuild: true