You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

import LifecycleUnit from '../../lib/src/lifecycle/Unit.ts'
import Config from '../../lib/src/unit/Config.ts'
import {Unit} from '../../lib/src/lifecycle/decorators.ts'
import Database from './service/Database.ts'
/**
* Lifecycle unit which loads and creates database connections from the database config files.
* @extends LifecycleUnit
*/
@Unit()
export class DatabaseUnit extends LifecycleUnit {
constructor(
protected config: Config,
protected db: Database,
) {
super()
}
public async up() {
const connections: { [key: string]: any } = this.config.get('db.connections')
for ( const conn_name in connections ) {
if ( !connections.hasOwnProperty(conn_name) ) continue
const config = connections[conn_name]
switch(config.type) {
case 'postgres':
await this.db.postgres(conn_name, config)
break
default:
throw new TypeError(`Invalid database driver type: ${config.type}`)
}
}
}
}