Skip to content

Commit

Permalink
fix create database before ping
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 22, 2024
1 parent b194300 commit 44ed071
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 5 additions & 4 deletions src/clickhouse/createClient.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,5 +15,6 @@ function createClient(readonly = false) {
})
}

export const client = createClient(false);
export const readOnlyClient = createClient(true);
export const client = createClient(false, config.database);
export const defaultClient = createClient(false, "default");
export const readOnlyClient = createClient(true, config.database);
9 changes: 2 additions & 7 deletions src/clickhouse/createDatabase.ts
Original file line number Diff line number Diff line change
@@ -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 })};
}
2 changes: 1 addition & 1 deletion src/fetch/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}

0 comments on commit 44ed071

Please sign in to comment.