Add connect methods to libraries.

This commit is contained in:
Mike Cao 2022-10-06 15:00:16 -07:00
parent 186f484ff1
commit e442617421
5 changed files with 59 additions and 28 deletions

View file

@ -14,6 +14,9 @@ export const CLICKHOUSE_DATE_FORMATS = {
const log = debug('umami:clickhouse');
let clickhouse;
const enabled = Boolean(process.env.CLICKHOUSE_URL);
function getClient() {
const {
hostname,
@ -144,6 +147,8 @@ async function rawQuery(query, params = []) {
log(formattedQuery);
}
await connect();
return clickhouse.query(formattedQuery).toPromise();
}
@ -159,12 +164,19 @@ async function findFirst(data) {
return data[0] ?? null;
}
// Initialization
const clickhouse = process.env.CLICKHOUSE_URL && (global[CLICKHOUSE] || getClient());
async function connect() {
if (!clickhouse) {
clickhouse = process.env.CLICKHOUSE_URL && (global[CLICKHOUSE] || getClient());
}
return clickhouse;
}
export default {
enabled,
client: clickhouse,
log,
connect,
getDateStringQuery,
getDateQuery,
getDateFormat,