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 | 8aa355dee788a6fb9b694a8e49063a7482f70fcc (patch) | |
tree | 71eb00236d37c7eb37b6d8be364e7e51ed8afb3b /src | |
parent | Add GH workflow for build (diff) | |
download | server-8aa355dee788a6fb9b694a8e49063a7482f70fcc.tar.xz |
Move datasource to own file for typeorm cli
Diffstat (limited to 'src')
-rw-r--r-- | src/util/util/Database.ts | 36 | ||||
-rw-r--r-- | src/util/util/Datasource.ts | 42 |
2 files changed, 46 insertions, 32 deletions
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 |