umami/lib/db.js

37 lines
867 B
JavaScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import { POSTGRESQL, RELATIONAL, MYSQL, KAFKA } from 'lib/constants';
import { CLICKHOUSE } from 'lib/constants';
2020-07-17 10:03:38 +02:00
2022-07-07 14:55:43 +02:00
BigInt.prototype.toJSON = function () {
2022-07-16 07:21:37 +02:00
return Number(this);
2022-07-07 14:55:43 +02:00
};
2022-08-26 07:04:32 +02:00
export function getDatabase(database, databaseType, fallback) {
const type = databaseType || (database && database.split(':')[0]);
2022-07-16 01:47:38 +02:00
if (type === 'postgres') {
return POSTGRESQL;
}
if (!type) {
2022-08-26 07:04:32 +02:00
return getDatabase(fallback);
2022-07-16 01:47:38 +02:00
}
return type;
}
2022-07-21 06:31:26 +02:00
export async function runAnalyticsQuery(queries) {
2022-08-26 07:04:32 +02:00
const db = getDatabase(process.env.ANALYTICS_URL, null, process.env.DATABASE_URL);
2022-07-16 01:47:38 +02:00
if (db === POSTGRESQL || db === MYSQL) {
2022-07-25 18:47:11 +02:00
return queries[RELATIONAL]();
2022-07-16 01:47:38 +02:00
}
if (db === CLICKHOUSE) {
2022-08-26 07:04:32 +02:00
const kafka = getDatabase(process.env.KAFKA_URL);
2022-08-05 02:43:47 +02:00
if (kafka === KAFKA && queries[KAFKA]) {
return queries[KAFKA]();
}
2022-07-25 18:47:11 +02:00
return queries[CLICKHOUSE]();
2022-07-16 01:47:38 +02:00
}
}