mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
Refactor get database type logic.
This commit is contained in:
parent
b29cece7ef
commit
3327bcf5a3
4 changed files with 36 additions and 23 deletions
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue