Implement Cockroachdb interaction
parent
38c9da4e55
commit
33422d8a6c
|
@ -1,6 +1,7 @@
|
||||||
export const PRISMA = 'prisma';
|
export const PRISMA = 'prisma';
|
||||||
export const POSTGRESQL = 'postgresql';
|
export const POSTGRESQL = 'postgresql';
|
||||||
export const MYSQL = 'mysql';
|
export const MYSQL = 'mysql';
|
||||||
|
export const COCKROACHDB = 'cockroachdb';
|
||||||
export const CLICKHOUSE = 'clickhouse';
|
export const CLICKHOUSE = 'clickhouse';
|
||||||
export const KAFKA = 'kafka';
|
export const KAFKA = 'kafka';
|
||||||
export const KAFKA_PRODUCER = 'kafka-producer';
|
export const KAFKA_PRODUCER = 'kafka-producer';
|
||||||
|
@ -14,6 +15,10 @@ BigInt.prototype.toJSON = function () {
|
||||||
export function getDatabaseType(url = process.env.DATABASE_URL) {
|
export function getDatabaseType(url = process.env.DATABASE_URL) {
|
||||||
const type = url && url.split(':')[0];
|
const type = url && url.split(':')[0];
|
||||||
|
|
||||||
|
if (process.env.COCKROACH_DB) {
|
||||||
|
return COCKROACHDB;
|
||||||
|
}
|
||||||
|
|
||||||
if (type === 'postgres') {
|
if (type === 'postgres') {
|
||||||
return POSTGRESQL;
|
return POSTGRESQL;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +29,7 @@ export function getDatabaseType(url = process.env.DATABASE_URL) {
|
||||||
export async function runQuery(queries) {
|
export async function runQuery(queries) {
|
||||||
const db = getDatabaseType(process.env.CLICKHOUSE_URL || process.env.DATABASE_URL);
|
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]();
|
return queries[PRISMA]();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { PrismaClient } from '@prisma/client';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import debug from 'debug';
|
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';
|
import { FILTER_IGNORED } from 'lib/constants';
|
||||||
|
|
||||||
const MYSQL_DATE_FORMATS = {
|
const MYSQL_DATE_FORMATS = {
|
||||||
|
@ -55,6 +55,13 @@ function getClient(options) {
|
||||||
function getDateQuery(field, unit, timezone) {
|
function getDateQuery(field, unit, timezone) {
|
||||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
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 (db === POSTGRESQL) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
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) {
|
function getTimestampInterval(field) {
|
||||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||||
|
|
||||||
if (db === POSTGRESQL) {
|
if (db === POSTGRESQL || db === COCKROACHDB) {
|
||||||
return `floor(extract(epoch from max(${field}) - min(${field})))`;
|
return `floor(extract(epoch from max(${field}) - min(${field})))`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +95,7 @@ function getTimestampInterval(field) {
|
||||||
function getJsonField(column, property, isNumber) {
|
function getJsonField(column, property, isNumber) {
|
||||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||||
|
|
||||||
if (db === POSTGRESQL) {
|
if (db === POSTGRESQL || db === COCKROACHDB) {
|
||||||
let accessor = `${column} ->> '${property}'`;
|
let accessor = `${column} ->> '${property}'`;
|
||||||
|
|
||||||
if (isNumber) {
|
if (isNumber) {
|
||||||
|
@ -229,7 +236,7 @@ function parseFilters(table, column, filters = {}, params = [], sessionKey = 'se
|
||||||
async function rawQuery(query, params = []) {
|
async function rawQuery(query, params = []) {
|
||||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
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.'));
|
return Promise.reject(new Error('Unknown database.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,10 @@ const del = require('del');
|
||||||
function getDatabaseType(url = process.env.DATABASE_URL) {
|
function getDatabaseType(url = process.env.DATABASE_URL) {
|
||||||
const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
|
const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
|
||||||
|
|
||||||
|
if (process.env.COCKROACH_DB) {
|
||||||
|
return 'cockroachdb';
|
||||||
|
}
|
||||||
|
|
||||||
if (type === 'postgres') {
|
if (type === 'postgres') {
|
||||||
return 'postgresql';
|
return 'postgresql';
|
||||||
}
|
}
|
||||||
|
@ -15,7 +19,7 @@ function getDatabaseType(url = process.env.DATABASE_URL) {
|
||||||
|
|
||||||
const databaseType = getDatabaseType();
|
const databaseType = getDatabaseType();
|
||||||
|
|
||||||
if (!databaseType || !['mysql', 'postgresql'].includes(databaseType)) {
|
if (!databaseType || !['mysql', 'postgresql', 'cockroachdb'].includes(databaseType)) {
|
||||||
throw new Error('Missing or invalid database');
|
throw new Error('Missing or invalid database');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue