Refactor get database type logic.

This commit is contained in:
Mike Cao 2022-08-26 22:43:31 -07:00
parent b29cece7ef
commit 3327bcf5a3
4 changed files with 36 additions and 23 deletions

View File

@ -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]();
}
}

View File

@ -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,
};

View File

@ -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",

View File

@ -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"