summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-04 21:57:24 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-04 21:57:24 +0200
commitb584af656d76f271bc9fc52df7bf78077c7ae585 (patch)
treea4bdbfd2b575515f2bad08dfd58ab789e3f0e124 /util
parentMerge branch 'master' of https://github.com/fosscord/fosscord-server (diff)
downloadserver-b584af656d76f271bc9fc52df7bf78077c7ae585.tar.xz
:construction: different db engines
Diffstat (limited to 'util')
-rw-r--r--util/src/util/Database.ts16
1 files changed, 9 insertions, 7 deletions
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<any>;
 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,