feat: support mongodb connection

This commit is contained in:
Joseph Lee 2023-05-15 15:49:21 +09:00
parent 586529a5ca
commit bb30b43a8e
9 changed files with 370 additions and 30 deletions

View file

@ -4,6 +4,7 @@ export const MYSQL = 'mysql';
export const CLICKHOUSE = 'clickhouse';
export const KAFKA = 'kafka';
export const KAFKA_PRODUCER = 'kafka-producer';
export const MONGODB = 'mongodb';
// Fixes issue with converting bigint values
BigInt.prototype.toJSON = function () {
@ -15,6 +16,8 @@ export function getDatabaseType(url = process.env.DATABASE_URL) {
if (type === 'postgres') {
return POSTGRESQL;
} else if (type === 'mongodb+srv') {
return MONGODB;
}
return type;
@ -23,7 +26,7 @@ export function getDatabaseType(url = process.env.DATABASE_URL) {
export async function runQuery(queries) {
const db = getDatabaseType(process.env.CLICKHOUSE_URL || process.env.DATABASE_URL);
if (db === POSTGRESQL || db === MYSQL) {
if (db === POSTGRESQL || db === MYSQL || db === MONGODB) {
return queries[PRISMA]();
}

View file

@ -1,6 +1,6 @@
import prisma from '@umami/prisma-client';
import moment from 'moment-timezone';
import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
import { MYSQL, POSTGRESQL, getDatabaseType, MONGODB } from 'lib/db';
import { getEventDataType } from './eventData';
import { FILTER_COLUMNS } from './constants';
@ -51,6 +51,10 @@ function getDateQuery(field: string, unit: string, timezone?: string): string {
return `date_format(${field}, '${MYSQL_DATE_FORMATS[unit]}')`;
}
if (db === MONGODB) {
return MYSQL_DATE_FORMATS[unit];
}
}
function getTimestampInterval(field: string): string {
@ -152,6 +156,7 @@ async function rawQuery(query: string, params: never[] = []): Promise<any> {
export default {
...prisma,
getDatabaseType: () => getDatabaseType(process.env.DATABASE_URL),
getDateQuery,
getTimestampInterval,
getFilterQuery,