fix(core): update
This commit is contained in:
@@ -9,6 +9,7 @@ export interface IClickhouseConstructorOptions {
|
||||
|
||||
export class SmartClickHouseDb {
|
||||
public options: IClickhouseConstructorOptions;
|
||||
public defaultClient: plugins.clickhouse.ClickHouseClient;
|
||||
public clickhouseClient: plugins.clickhouse.ClickHouseClient;
|
||||
|
||||
constructor(optionsArg: IClickhouseConstructorOptions) {
|
||||
@@ -32,23 +33,38 @@ export class SmartClickHouseDb {
|
||||
// the password
|
||||
this.options.password ? defaultOptions.password = this.options.password : null;
|
||||
// lets connect
|
||||
const defaultClient = new plugins.clickhouse.ClickHouseClient({
|
||||
this.defaultClient = new plugins.clickhouse.ClickHouseClient({
|
||||
...defaultOptions,
|
||||
database: 'default',
|
||||
});
|
||||
await this.pingDatabaseUntilAvailable();
|
||||
console.log(`Create database ${this.options.database}, if it does not exist...`);
|
||||
if (dropOld) {
|
||||
await defaultClient.queryPromise(`DROP DATABASE IF EXISTS ${this.options.database}`);
|
||||
}
|
||||
await defaultClient.queryPromise(`CREATE DATABASE IF NOT EXISTS ${this.options.database}`);
|
||||
await this.createDatabase(dropOld);
|
||||
|
||||
console.log(`Ensured database. Now connecting to wanted database: ${this.options.database}`);
|
||||
this.clickhouseClient = new plugins.clickhouse.ClickHouseClient({
|
||||
...defaultOptions
|
||||
});
|
||||
console.log(`trying to ping database...`);
|
||||
const result = await this.clickhouseClient.ping();
|
||||
console.log(`Ping successfull?: ${result}`);
|
||||
}
|
||||
|
||||
public async createDatabase(dropOld: boolean = false) {
|
||||
if (dropOld) {
|
||||
await this.defaultClient.queryPromise(`DROP DATABASE IF EXISTS ${this.options.database}`);
|
||||
}
|
||||
await this.defaultClient.queryPromise(`CREATE DATABASE IF NOT EXISTS ${this.options.database}`);
|
||||
}
|
||||
|
||||
public async pingDatabaseUntilAvailable() {
|
||||
let available = false;
|
||||
while(!available) {
|
||||
available = await this.defaultClient.ping().catch(err => {
|
||||
return false;
|
||||
});
|
||||
if (!available) {
|
||||
console.log(`NOT OK: tried pinging ${this.options.url}... Trying again in 5 seconds.`);
|
||||
await plugins.smartdelay.delayFor(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user