Browse Source

Enable custom entity registration in database package. Add environment variable for database selection

dev
David Ludwig 4 years ago
parent
commit
ab329544ab
1 changed files with 20 additions and 13 deletions
  1. +20
    -13
      packages/database/src/DatabaseService.ts

+ 20
- 13
packages/database/src/DatabaseService.ts View File

@ -1,42 +1,48 @@
import { InternalService, Microservice } from "@autoplex/microservice";
import { secret } from "@autoplex/utils";
import { createConnection, Connection } from "typeorm";
import { env, secret } from "@autoplex/utils";
import { createConnection, Connection, EntitySchema } from "typeorm";
import * as DB from "./constants";
import * as entities from "./entities";
/**
* The type for entity schemas
*/
export type EntitySchemaTypes = string|Function|EntitySchema<any>;
/**
* A convenience database service
*/
export class DatabaseService<M extends Microservice = Microservice> extends InternalService<M>
{
/**
* The active database connection
* The name of the service
*/
protected connection!: Connection;
public readonly NAME = "Database";
/**
* The name of the service
* The active database connection
*/
public readonly NAME = "Database";
protected connection!: Connection;
/**
* @TODO This database should be renamed
* The database to use
* The database entities
*/
protected readonly DATABASE = "autoplex_request";
protected entities: EntitySchemaTypes[] = Object.values(entities);
/**
* Boot the database service
*/
public async boot() {
let password = await secret(DB.DATABASE_PASSWORD_FILE)
let password = await secret(DB.DATABASE_PASSWORD_FILE);
let database = env("DATABASE");
this.connection = await this.connectToDatabase(
DB.DATABASE_TYPE,
DB.DATABASE_HOST,
DB.DATABASE_PORT,
DB.DATABASE_USER,
password,
this.DATABASE
database,
this.entities
);
}
@ -51,7 +57,8 @@ export class DatabaseService<M extends Microservice = Microservice> extends Inte
* A convenience function to connect to the database
*/
protected async connectToDatabase(type: "mysql"|"mariadb", host: string, port: number,
user: string, password: string, database: string)
user: string, password: string, database: string,
entities: EntitySchemaTypes[])
{
// Create the database connection
return await createConnection({
@ -63,7 +70,7 @@ export class DatabaseService<M extends Microservice = Microservice> extends Inte
database : database,
// synchronize: process.env["NODE_ENV"] != "production",
synchronize: true, // Seems stable enough for my liking
entities : Object.values(entities)
entities
});
}
}

Loading…
Cancel
Save