From bcbcaaa936722d7285e889ee475cd93e68e022e3 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Fri, 7 May 2021 12:59:11 +0000 Subject: [PATCH] Provide default database info to the database package to remove environment requirements --- packages/database/src/DatabaseService.ts | 32 ++++++++++++++++-------- packages/database/src/constants.ts | 24 ++++++++++++++++++ packages/database/src/index.ts | 1 + 3 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 packages/database/src/constants.ts diff --git a/packages/database/src/DatabaseService.ts b/packages/database/src/DatabaseService.ts index be127b0..050a816 100644 --- a/packages/database/src/DatabaseService.ts +++ b/packages/database/src/DatabaseService.ts @@ -1,6 +1,7 @@ import { InternalService, Microservice } from "@autoplex/microservice"; -import { env, secret } from "@autoplex/utils"; +import { secret } from "@autoplex/utils"; import { createConnection, Connection } from "typeorm"; +import * as DB from "./constants"; import * as entities from "./entities"; /** @@ -18,16 +19,25 @@ export class DatabaseService extends Inte */ public readonly NAME = "Database"; + /** + * @TODO This database should be renamed + * The database to use + */ + protected readonly DATABASE = "autoplex_request"; + /** * Boot the database service */ public async boot() { - let host = env("DB_HOST"); - let port = parseInt(env("DB_PORT")); - let username = env("DB_USER"); - let password = await secret(env("DB_PASSWORD_FILE")); - let database = env("DB_DATABASE"); - this.connection = await this.connectToDatabase(host, port, username, password, database); + let password = await secret(DB.DATABASE_PASSWORD_FILE) + this.connection = await this.connectToDatabase( + DB.DATABASE_TYPE, + DB.DATABASE_HOST, + DB.DATABASE_PORT, + DB.DATABASE_USER, + password, + this.DATABASE + ); } /** @@ -40,15 +50,15 @@ export class DatabaseService extends Inte /** * A convenience function to connect to the database */ - private async connectToDatabase(host: string, port: number, username: string, password: string, - database: string) + protected async connectToDatabase(type: "mysql"|"mariadb", host: string, port: number, + user: string, password: string, database: string) { // Create the database connection return await createConnection({ - type : "mysql", + type : type, host : host, port : port, - username : username, + username : user, password : password, database : database, // synchronize: process.env["NODE_ENV"] != "production", diff --git a/packages/database/src/constants.ts b/packages/database/src/constants.ts new file mode 100644 index 0000000..1e5c6d9 --- /dev/null +++ b/packages/database/src/constants.ts @@ -0,0 +1,24 @@ +/** + * The type of the database being used + */ +export const DATABASE_TYPE: "mysql"|"mariadb" = "mysql"; + +/** + * The database host (Docker service name) + */ +export const DATABASE_HOST = "database"; + +/** + * The databse port + */ +export const DATABASE_PORT = 3306; + +/** + * The database username + */ +export const DATABASE_USER = "root"; + +/** + * The password file for the database + */ +export const DATABASE_PASSWORD_FILE = "/run/secrets/mysql_root_password"; diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts index 45942a1..141c015 100644 --- a/packages/database/src/index.ts +++ b/packages/database/src/index.ts @@ -1,2 +1,3 @@ +export * from "./constants"; export * from "./entities"; export * from "./DatabaseService";