Browse Source

Provide default database info to the database package to remove environment requirements

dev
David Ludwig 4 years ago
parent
commit
bcbcaaa936
3 changed files with 46 additions and 11 deletions
  1. +21
    -11
      packages/database/src/DatabaseService.ts
  2. +24
    -0
      packages/database/src/constants.ts
  3. +1
    -0
      packages/database/src/index.ts

+ 21
- 11
packages/database/src/DatabaseService.ts View File

@ -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<M extends Microservice = Microservice> 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<M extends Microservice = Microservice> 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",


+ 24
- 0
packages/database/src/constants.ts View File

@ -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";

+ 1
- 0
packages/database/src/index.ts View File

@ -1,2 +1,3 @@
export * from "./constants";
export * from "./entities";
export * from "./DatabaseService";

Loading…
Cancel
Save