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.
32 lines
965 B
32 lines
965 B
4 years ago
|
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'
|
||
|
|
||
|
@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}`)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|