summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-12-27 15:12:32 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-12-27 15:23:56 +1100
commit8aa355dee788a6fb9b694a8e49063a7482f70fcc (patch)
tree71eb00236d37c7eb37b6d8be364e7e51ed8afb3b
parentAdd GH workflow for build (diff)
downloadserver-8aa355dee788a6fb9b694a8e49063a7482f70fcc.tar.xz
Move datasource to own file for typeorm cli
-rw-r--r--package.json3
-rw-r--r--src/util/util/Database.ts36
-rw-r--r--src/util/util/Datasource.ts42
3 files changed, 48 insertions, 33 deletions
diff --git a/package.json b/package.json
index 1c4cd388..6732503c 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,8 @@
 		"generate:rights": "node scripts/rights.js",
 		"generate:schema": "node scripts/schema.js",
 		"generate:client": "node scripts/client.js",
-		"generate:changelog": "node scripts/changelog.js"
+		"generate:changelog": "node scripts/changelog.js",
+		"generate:migration": "node node_modules/typeorm/cli.js migration:generate -d dist/util/util/Datasource.js"
 	},
 	"main": "dist/bundle/index.js",
 	"types": "src/bundle/index.ts",
diff --git a/src/util/util/Database.ts b/src/util/util/Database.ts
index 87ee212f..2f1cd290 100644
--- a/src/util/util/Database.ts
+++ b/src/util/util/Database.ts
@@ -1,14 +1,13 @@
-import path from "path";
 import "reflect-metadata";
 import { DataSource } from "typeorm";
 import { yellow, green, red } from "picocolors";
+import { DataSourceOptions, DatabaseType } from "./Datasource";
 
 // UUID extension option is only supported with postgres
 // We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
 
 var dbConnection: DataSource | undefined;
-let dbConnectionString =
-	process.env.DATABASE || path.join(process.cwd(), "database.db");
+
 
 // Gets the existing database connection
 export function getDatabase(): DataSource | null {
@@ -21,36 +20,9 @@ export function getDatabase(): DataSource | null {
 export async function initDatabase(): Promise<DataSource> {
 	if (dbConnection) return dbConnection;
 
-	const type = dbConnectionString.includes("://")
-		? dbConnectionString.split(":")[0]?.replace("+srv", "")
-		: "sqlite";
-	const isSqlite = type.includes("sqlite");
-
-	console.log(`[Database] ${yellow(`connecting to ${type} db`)}`);
-	if (isSqlite) {
-		console.log(
-			`[Database] ${red(
-				`You are running sqlite! Please keep in mind that we recommend setting up a dedicated database!`,
-			)}`,
-		);
-	}
-
-	const dataSource = new DataSource({
-		//@ts-ignore type 'string' is not 'mysql' | 'sqlite' | 'mariadb' | etc etc
-		type,
-		charset: "utf8mb4",
-		url: isSqlite ? undefined : dbConnectionString,
-		database: isSqlite ? dbConnectionString : undefined,
-		entities: ["dist/util/entities/*.js"],
-		synchronize: false,
-		logging: false,
-		bigNumberStrings: false,
-		supportBigNumbers: true,
-		name: "default",
-		migrations: ["dist/util/migrations/*.js"],
-	});
+	console.log(`[Database] ${yellow(`connecting to ${DatabaseType} db`)}`);
 
-	dbConnection = await dataSource.initialize();
+	dbConnection = await DataSourceOptions.initialize();
 
 	await dbConnection.runMigrations();
 	console.log(`[Database] ${green("connected")}`);
diff --git a/src/util/util/Datasource.ts b/src/util/util/Datasource.ts
new file mode 100644
index 00000000..1495faaa
--- /dev/null
+++ b/src/util/util/Datasource.ts
@@ -0,0 +1,42 @@
+import { config } from "dotenv"
+import path from "path";
+import { DataSource } from "typeorm";
+import { red } from "picocolors";
+
+// For typeorm cli
+if (!process.env) {
+	config();
+}
+
+let dbConnectionString =
+	process.env.DATABASE || path.join(process.cwd(), "database.db");
+
+const type = dbConnectionString.includes("://")
+	? dbConnectionString.split(":")[0]?.replace("+srv", "")
+	: "sqlite";
+const isSqlite = type.includes("sqlite");
+
+if (isSqlite) {
+	console.log(
+		`[Database] ${red(
+			`You are running sqlite! Please keep in mind that we recommend setting up a dedicated database!`,
+		)}`,
+	);
+}
+
+const dataSource = new DataSource({
+	//@ts-ignore type 'string' is not 'mysql' | 'sqlite' | 'mariadb' | etc etc
+	type,
+	charset: "utf8mb4",
+	url: isSqlite ? undefined : dbConnectionString,
+	database: isSqlite ? dbConnectionString : undefined,
+	entities: ["dist/util/entities/*.js"],
+	synchronize: false,
+	logging: false,
+	bigNumberStrings: false,
+	supportBigNumbers: true,
+	name: "default",
+	migrations: ["dist/util/migrations/*.js"],
+});
+
+export { dataSource as DataSourceOptions, type as DatabaseType };
\ No newline at end of file