import { onServerMainBootstrap , onServerMainShutdown } from '../application-server' ; import { eventDispatcher } from '@deepkit/event' ; import { injectable } from '@deepkit/injector' ; import { Logger } from '@deepkit/logger' ; import { DatabaseRegistry } from '@deepkit/orm' ; import { getClassName } from '@deepkit/core' ; @ injectable export class DatabaseListener { constructor ( protected databases , protected logger ) { } @ eventDispatcher . listen ( onServerMainBootstrap ) async onMainBootstrap ( ) { for ( const databaseType of this . databases . getDatabaseTypes ( ) ) { if ( this . databases . isMigrateOnStartup ( databaseType . classType ) ) { const database = this . databases . getDatabase ( databaseType . classType ) ; if ( ! database ) throw new Error ( 'Database not created' ) ; this . logger . log ( `Migrate database <yellow>${ getClassName ( database ) } ${ database . name }</yellow> (${ getClassName ( database . adapter ) })` ) ; await database . migrate ( ) ; } } } @ eventDispatcher . listen ( onServerMainShutdown ) onShutdown ( ) { this . databases . onShutDown ( ) ; } }