From b584af656d76f271bc9fc52df7bf78077c7ae585 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 4 Oct 2021 21:57:24 +0200 Subject: :construction: different db engines --- util/src/util/Database.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'util') diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts index ab7c70c5..369a403c 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts @@ -1,6 +1,6 @@ import path from "path"; import "reflect-metadata"; -import { Connection, createConnection, ValueTransformer } from "typeorm"; +import { Connection, createConnection } from "typeorm"; import * as Models from "../entities"; // UUID extension option is only supported with postgres @@ -8,18 +8,20 @@ import * as Models from "../entities"; var promise: Promise; var dbConnection: Connection | undefined; +let dbConnectionString = process.env.DATABASE || path.join(process.cwd(), "database.db"); export function initDatabase() { if (promise) return promise; // prevent initalizing multiple times - console.log("[Database] connecting ..."); + const type = dbConnectionString.includes(":") ? dbConnectionString.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + + console.log(`[Database] connecting to ${type} db`); // @ts-ignore promise = createConnection({ - type: "sqlite", - database: path.join(process.cwd(), "database.db"), - // type: "postgres", - // url: "postgres://fosscord:wb94SmuURM2Syv&@localhost/fosscord", - // + type, + url: isSqlite ? undefined : dbConnectionString, + database: isSqlite ? dbConnectionString : undefined, entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"), synchronize: true, logging: false, -- cgit 1.4.1