umami/lib/db.js

38 lines
854 B
JavaScript
Raw Normal View History

2022-08-28 06:38:35 +02:00
export const PRISMA = 'prisma';
export const POSTGRESQL = 'postgresql';
export const MYSQL = 'mysql';
export const CLICKHOUSE = 'clickhouse';
export const KAFKA = 'kafka';
export const KAFKA_PRODUCER = 'kafka-producer';
// Fixes issue with converting bigint values
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-27 07:43:31 +02:00
export function getDatabaseType(url = process.env.DATABASE_URL) {
const type = url && url.split(':')[0];
2022-07-16 01:47:38 +02:00
if (type === 'postgres') {
return POSTGRESQL;
}
return type;
}
2022-08-28 06:38:35 +02:00
export async function runQuery(queries) {
const db = getDatabaseType(process.env.CLICKHOUSE_URL || process.env.DATABASE_URL);
2022-07-16 01:47:38 +02:00
if (db === POSTGRESQL || db === MYSQL) {
2022-08-28 06:38:35 +02:00
return queries[PRISMA]();
2022-07-16 01:47:38 +02:00
}
if (db === CLICKHOUSE) {
2022-08-27 07:43:31 +02:00
if (queries[KAFKA]) {
2022-08-05 02:43:47 +02:00
return queries[KAFKA]();
}
2022-08-27 07:43:31 +02:00
2022-07-25 18:47:11 +02:00
return queries[CLICKHOUSE]();
2022-07-16 01:47:38 +02:00
}
}