Import other modules into monorepo
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-06-01 20:59:40 -05:00
parent 26d54033af
commit 9be9c44a32
138 changed files with 11544 additions and 139 deletions

View File

@@ -0,0 +1,62 @@
import {Inject, Singleton} from "../../di";
import {DatabaseService} from "../DatabaseService";
import {PostgresConnection} from "../connection/PostgresConnection";
import {ErrorWithContext} from "../../util";
import {Unit} from "../../lifecycle/Unit";
import {Config} from "../../service/Config";
import {Logging} from "../../service/Logging";
/**
* Application unit responsible for loading and creating database connections from config.
*/
@Singleton()
export class Database extends Unit {
@Inject()
protected readonly config!: Config;
@Inject()
protected readonly dbService!: DatabaseService;
@Inject()
protected readonly logging!: Logging;
/**
* Load the `database.connections` config and register Connection instances for each config.
* Automatically initializes the connections.
*/
public async up() {
const connections = this.config.get('database.connections')
const promises = []
for ( const key in connections ) {
if ( !connections.hasOwnProperty(key) ) continue
const config = connections[key]
this.logging.info(`Initializing database connection: ${key}`)
this.logging.verbose(config)
let conn
if ( config?.dialect === 'postgres' ) {
conn = <PostgresConnection> this.app().make(PostgresConnection, key, config)
} else {
const e = new ErrorWithContext(`Invalid or missing database dialect: ${config.dialect}. Should be one of: postgres`)
e.context = { connectionName: key }
throw e
}
this.dbService.register(key, conn)
promises.push(conn.init())
}
await Promise.all(promises)
this.logging.info('Database connections opened.')
}
/**
* Close the configured connections cleanly before exit.
*/
public async down() {
await Promise.all(this.dbService.names()
.map(name => this.dbService.get(name).close()))
}
}

View File

@@ -0,0 +1,33 @@
import {Model} from "../model/Model";
import {Instantiable, Singleton, Inject} from "../../di";
import {CommandLine} from "../../cli";
import {model_template} from "../template/model";
import {CanonicalStatic} from "../../service/CanonicalStatic";
import {CanonicalDefinition} from "../../service/Canonical";
/**
* Canonical unit responsible for loading the model classes defined by the application.
*/
@Singleton()
export class Models extends CanonicalStatic<Model<any>, Instantiable<Model<any>>> {
@Inject()
protected readonly cli!: CommandLine
protected appPath = ['models']
protected canonicalItem = 'model'
protected suffix = '.model.js'
public async up() {
await super.up()
this.cli.registerTemplate(model_template)
}
public async initCanonicalItem(definition: CanonicalDefinition): Promise<Instantiable<Model<any>>> {
const item = await super.initCanonicalItem(definition)
if ( !(item.prototype instanceof Model) ) {
throw new TypeError(`Invalid controller definition: ${definition.originalName}. Models must extend from @extollo/orm.Model.`)
}
return item
}
}