diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-27 15:12:32 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-27 15:23:56 +1100 |
commit | c25a2e5c5b64b2f4353b79745a8bfb7d57628628 (patch) | |
tree | a1a6fc24b6703cc59cd31cc868fe0e825421cf52 | |
parent | Add GH workflow for build (diff) | |
download | server-c25a2e5c5b64b2f4353b79745a8bfb7d57628628.tar.xz |
Move datasource to own file for typeorm cli
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | src/util/util/Database.ts | 36 | ||||
-rw-r--r-- | src/util/util/Datasource.ts | 42 |
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 |