fix relational

This commit is contained in:
Brian Cao 2022-08-26 21:29:26 -07:00
parent 1ba7b76072
commit b29cece7ef
3 changed files with 26 additions and 6 deletions

View File

@ -13,7 +13,10 @@ export function getDatabase(database, databaseType, fallback) {
}
if (!type) {
return getDatabase(fallback);
if (fallback) {
return getDatabase(fallback);
}
return null;
}
return type;

View File

@ -34,11 +34,15 @@ const kafka = global.kafka || getClient();
let kafkaProducer = null;
(async () => {
kafkaProducer = global.kakfaProducer || (await getProducer());
if (kafka) {
kafkaProducer = global.kakfaProducer || (await getProducer());
}
if (process.env.NODE_ENV !== 'production') {
global.kafka = kafka;
global.kakfaProducer = kafkaProducer;
if (kafka) {
global.kakfaProducer = kafkaProducer;
}
}
})();

View File

@ -7,7 +7,7 @@ import {
POSTGRESQL,
POSTGRESQL_DATE_FORMATS,
} from 'lib/constants';
import { getDatabase } from './db';
import { getDatabase } from 'lib/db';
import moment from 'moment-timezone';
const options = {
@ -32,6 +32,7 @@ function getClient(options) {
return prisma;
}
const prisma = global.prisma || getClient(options);
if (process.env.NODE_ENV !== 'production') {
@ -41,7 +42,7 @@ if (process.env.NODE_ENV !== 'production') {
export { prisma };
export function getDateQuery(field, unit, timezone) {
const db = getDatabase();
const db = getDatabase(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
if (timezone) {
@ -61,6 +62,18 @@ export function getDateQuery(field, unit, timezone) {
}
}
export function getTimestampInterval(field) {
const db = getDatabase(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
return `floor(extract(epoch from max(${field}) - min(${field})))`;
}
if (db === MYSQL) {
return `floor(unix_timestamp(max(${field})) - unix_timestamp(min(${field})))`;
}
}
export function getFilterQuery(table, column, filters = {}, params = []) {
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
@ -151,7 +164,7 @@ export async function runQuery(query) {
}
export async function rawQuery(query, params = []) {
const db = getDatabase(process.env.DATABASE_URL, process.env.DATABASE_TYPE);
const db = getDatabase(process.env.DATABASE_URL);
if (db !== POSTGRESQL && db !== MYSQL) {
return Promise.reject(new Error('Unknown database.'));