update clickhouse

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-17 12:47:26 +02:00
committed by Carl-Gerhard Lindesvärd
parent 16b391797d
commit 1b8e3d31db
3 changed files with 23 additions and 15 deletions

View File

@@ -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<T> {
data: T[];
rows: number;
statistics: { elapsed: number; rows_read: number; bytes_read: number };
meta: { name: string; type: string }[];
}
export async function chQueryWithMeta<T extends Record<string, any>>(
query: string
): Promise<ClickhouseJsonResponse<T>> {
): Promise<ResponseJSON<T>> {
const res = await ch.query({
query,
});
const json = await res.json<ClickhouseJsonResponse<T>>();
const json = await res.json<T>();
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]: