Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix create database before ping #116

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
});
}
Loading