diff --git a/lib/db.js b/lib/db.js index e58a18c8..a50dc254 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,6 +1,7 @@ export const PRISMA = 'prisma'; export const POSTGRESQL = 'postgresql'; export const MYSQL = 'mysql'; +export const COCKROACHDB = 'cockroachdb'; export const CLICKHOUSE = 'clickhouse'; export const KAFKA = 'kafka'; export const KAFKA_PRODUCER = 'kafka-producer'; @@ -14,6 +15,10 @@ BigInt.prototype.toJSON = function () { export function getDatabaseType(url = process.env.DATABASE_URL) { const type = url && url.split(':')[0]; + if (process.env.COCKROACH_DB) { + return COCKROACHDB; + } + if (type === 'postgres') { return POSTGRESQL; } @@ -24,7 +29,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 === COCKROACHDB) { return queries[PRISMA](); } diff --git a/lib/prisma.js b/lib/prisma.js index ab1e6ebf..eb3ea50a 100644 --- a/lib/prisma.js +++ b/lib/prisma.js @@ -2,7 +2,7 @@ import { PrismaClient } from '@prisma/client'; import chalk from 'chalk'; import moment from 'moment-timezone'; import debug from 'debug'; -import { PRISMA, MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db'; +import { PRISMA, MYSQL, POSTGRESQL, COCKROACHDB, getDatabaseType } from 'lib/db'; import { FILTER_IGNORED } from 'lib/constants'; const MYSQL_DATE_FORMATS = { @@ -55,6 +55,13 @@ function getClient(options) { function getDateQuery(field, unit, timezone) { const db = getDatabaseType(process.env.DATABASE_URL); + if (db == COCKROACHDB) { + if (timezone) { + return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}')::TIMESTAMP)`; + } + return `to_char(date_trunc('${unit}', ${field})::TIMESTAMP)`; + } + if (db === POSTGRESQL) { if (timezone) { return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`; @@ -76,7 +83,7 @@ function getDateQuery(field, unit, timezone) { function getTimestampInterval(field) { const db = getDatabaseType(process.env.DATABASE_URL); - if (db === POSTGRESQL) { + if (db === POSTGRESQL || db === COCKROACHDB) { return `floor(extract(epoch from max(${field}) - min(${field})))`; } @@ -88,7 +95,7 @@ function getTimestampInterval(field) { function getJsonField(column, property, isNumber) { const db = getDatabaseType(process.env.DATABASE_URL); - if (db === POSTGRESQL) { + if (db === POSTGRESQL || db === COCKROACHDB) { let accessor = `${column} ->> '${property}'`; if (isNumber) { @@ -229,7 +236,7 @@ function parseFilters(table, column, filters = {}, params = [], sessionKey = 'se async function rawQuery(query, params = []) { const db = getDatabaseType(process.env.DATABASE_URL); - if (db !== POSTGRESQL && db !== MYSQL) { + if (db !== POSTGRESQL && db !== MYSQL && db !== COCKROACHDB) { return Promise.reject(new Error('Unknown database.')); } diff --git a/scripts/copy-db-files.js b/scripts/copy-db-files.js index 3e902d45..29693e4e 100644 --- a/scripts/copy-db-files.js +++ b/scripts/copy-db-files.js @@ -6,6 +6,10 @@ const del = require('del'); function getDatabaseType(url = process.env.DATABASE_URL) { const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]); + if (process.env.COCKROACH_DB) { + return 'cockroachdb'; + } + if (type === 'postgres') { return 'postgresql'; } @@ -15,7 +19,7 @@ function getDatabaseType(url = process.env.DATABASE_URL) { const databaseType = getDatabaseType(); -if (!databaseType || !['mysql', 'postgresql'].includes(databaseType)) { +if (!databaseType || !['mysql', 'postgresql', 'cockroachdb'].includes(databaseType)) { throw new Error('Missing or invalid database'); }