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