Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Refactor get database type logic.

Mike Cao3327bcf5a33e
diff --git a/lib/db.js b/lib/db.js
index 90e98b063..13139cd58 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -1,39 +1,31 @@
-import { POSTGRESQL, RELATIONAL, MYSQL, KAFKA } from 'lib/constants';
-import { CLICKHOUSE } from 'lib/constants';
+import { POSTGRESQL, RELATIONAL, MYSQL, KAFKA, CLICKHOUSE } from 'lib/constants';
BigInt.prototype.toJSON = function () {
return Number(this);
};
-export function getDatabase(database, databaseType, fallback) {
- const type = databaseType || (database && database.split(':')[0]);
+export function getDatabaseType(url = process.env.DATABASE_URL) {
+ const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
if (type === 'postgres') {
return POSTGRESQL;
}
- if (!type) {
- if (fallback) {
- return getDatabase(fallback);
- }
- return null;
- }
-
return type;
}
export async function runAnalyticsQuery(queries) {
- const db = getDatabase(process.env.ANALYTICS_URL, null, process.env.DATABASE_URL);
+ const db = getDatabaseType(process.env.ANALYTICS_URL || process.env.DATABASE_URL);
if (db === POSTGRESQL || db === MYSQL) {
return queries[RELATIONAL]();
}
if (db === CLICKHOUSE) {
- const kafka = getDatabase(process.env.KAFKA_URL);
- if (kafka === KAFKA && queries[KAFKA]) {
+ if (queries[KAFKA]) {
return queries[KAFKA]();
}
+
return queries[CLICKHOUSE]();
}
}
diff --git a/lib/relational.js b/lib/relational.js
index e5b9f2cea..d86f06075 100644
--- a/lib/relational.js
+++ b/lib/relational.js
@@ -7,8 +7,11 @@ import {
POSTGRESQL,
POSTGRESQL_DATE_FORMATS,
} from 'lib/constants';
-import { getDatabase } from 'lib/db';
+import { getDatabaseType } from 'lib/db';
import moment from 'moment-timezone';
+import debug from 'debug';
+
+const log = debug('prisma');
const options = {
log: [
@@ -20,7 +23,7 @@ const options = {
};
function logQuery(e) {
- console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
+ log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
}
function getClient(options) {
@@ -33,16 +36,14 @@ function getClient(options) {
return prisma;
}
-const prisma = global.prisma || getClient(options);
+let prisma = global.prisma || getClient(options);
if (process.env.NODE_ENV !== 'production') {
global.prisma = prisma;
}
-export { prisma };
-
export function getDateQuery(field, unit, timezone) {
- const db = getDatabase(process.env.DATABASE_URL);
+ const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
if (timezone) {
@@ -63,7 +64,7 @@ export function getDateQuery(field, unit, timezone) {
}
export function getTimestampInterval(field) {
- const db = getDatabase(process.env.DATABASE_URL);
+ const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
return `floor(extract(epoch from max(${field}) - min(${field})))`;
@@ -164,7 +165,7 @@ export async function runQuery(query) {
}
export async function rawQuery(query, params = []) {
- const db = getDatabase(process.env.DATABASE_URL);
+ const db = getDatabaseType(process.env.DATABASE_URL);
if (db !== POSTGRESQL && db !== MYSQL) {
return Promise.reject(new Error('Unknown database.'));
@@ -174,3 +175,15 @@ export async function rawQuery(query, params = []) {
return runQuery(prisma.$queryRawUnsafe.apply(prisma, [sql, ...params]));
}
+
+export { prisma };
+
+export default {
+ prisma,
+ getDateQuery,
+ getTimestampInterval,
+ getFilterQuery,
+ parseFilters,
+ runQuery,
+ rawQuery,
+};
diff --git a/package.json b/package.json
index 04b0f5ebc..12153e841 100644
--- a/package.json
+++ b/package.json
@@ -69,6 +69,7 @@
"date-fns": "^2.23.0",
"date-fns-tz": "^1.1.4",
"dateformat": "^5.0.3",
+ "debug": "^4.3.4",
"del": "^6.0.0",
"detect-browser": "^5.2.0",
"dotenv": "^10.0.0",
diff --git a/yarn.lock b/yarn.lock
index 23754ad6e..25a50baaf 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2862,13 +2862,20 @@ debug@^4.0.1, debug@^4.1.0:
dependencies:
ms "2.1.2"
-debug@^4.1.1, debug@^4.3.2, debug@^4.3.4:
+debug@^4.1.1, debug@^4.3.2:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"
+debug@^4.3.4:
+ version "4.3.4"
+ resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
+ integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
+ dependencies:
+ ms "2.1.2"
+
decamelize-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/decamelize-keys/-/decamelize-keys-1.1.0.tgz"