From 44ed0715eecc99bb92d9afcc5a19ee0e63fa4d4d Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Thu, 22 Feb 2024 16:05:56 -0500 Subject: [PATCH] fix create database before ping --- index.ts | 2 +- src/clickhouse/createClient.ts | 9 +++++---- src/clickhouse/createDatabase.ts | 9 ++------- src/fetch/init.ts | 2 +- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/index.ts b/index.ts index 7cd6e2c..b55fa53 100644 --- a/index.ts +++ b/index.ts @@ -17,9 +17,9 @@ import * as buffer from "./src/buffer.js" if (config.verbose) logger.enable(); // initizalize before starting the server +await init(); await show_tables(); await show_databases(); -await init(); await buffer.flush(true); const app = Bun.serve({ diff --git a/src/clickhouse/createClient.ts b/src/clickhouse/createClient.ts index 45fae55..9b0a621 100644 --- a/src/clickhouse/createClient.ts +++ b/src/clickhouse/createClient.ts @@ -1,11 +1,11 @@ import * as clickhouse from "@clickhouse/client-web"; import { APP_NAME, config } from "../config.js"; -function createClient(readonly = false) { +function createClient(readonly = false, database = "default") { return clickhouse.createClient({ host: config.host, password: config.password, - database: config.database, + database, clickhouse_settings: { // wait_for_async_insert: 0, // 0 async_insert: 0, // 1 @@ -15,5 +15,6 @@ function createClient(readonly = false) { }) } -export const client = createClient(false); -export const readOnlyClient = createClient(true); \ No newline at end of file +export const client = createClient(false, config.database); +export const defaultClient = createClient(false, "default"); +export const readOnlyClient = createClient(true, config.database); diff --git a/src/clickhouse/createDatabase.ts b/src/clickhouse/createDatabase.ts index ddfe472..515d902 100644 --- a/src/clickhouse/createDatabase.ts +++ b/src/clickhouse/createDatabase.ts @@ -1,13 +1,8 @@ import { logger } from "../logger.js"; -import { client } from "./createClient.js"; -import { databases } from "./stores.js"; +import { defaultClient } from "./createClient.js"; export async function createDatabase(database: string) { - // if (!database) { - // throw new Error("[database] is required") - // } - // if ( databases?.has(database) ) return {}; const query = `CREATE DATABASE IF NOT EXISTS "${database}"`; logger.info('[clickhouse::createDatabase]\t', `CREATE DATABASE [${database}]`); - return {query, ...await client.exec({ query })}; + return {query, ...await defaultClient.exec({ query })}; } diff --git a/src/fetch/init.ts b/src/fetch/init.ts index 78df3d6..2b6803f 100644 --- a/src/fetch/init.ts +++ b/src/fetch/init.ts @@ -6,8 +6,8 @@ import { toJSON } from "./cors.js"; export default async function init() { return toJSON({ - ping: await ping(), createDatabase: await createDatabase(config.database), + ping: await ping(), initializeDefaultTables: await initializeDefaultTables(), }); }