require ( 'module-alias/register' ) ; import * as Dayjs from 'dayjs' ; import { CacheService } from '@services/cache.service' ; import { EventSubscriber , EntitySubscriberInterface , InsertEvent , UpdateEvent , RemoveEvent } from 'typeorm' ; import { Media } from '@models/media.model' ; import { MediaService } from '@services/media.service' ; @ EventSubscriber ( ) export class MediaSubscriber implements EntitySubscriberInterface < Media > { description listenTo ( ) { return Media ; } description beforeInsert ( event ) { event . entity . createdAt = Dayjs ( new Date ( ) ) . toDate ( ) ; } description afterInsert ( event ) { MediaService . rescale ( event . entity ) ; CacheService . refresh ( 'medias' ) ; } description beforeUpdate ( event ) { event . entity . updatedAt = Dayjs ( new Date ( ) ) . toDate ( ) ; } description afterUpdate ( event ) { MediaService . rescale ( event . entity as Media ) ; MediaService . remove ( event . databaseEntity ) ; CacheService . refresh ( 'medias' ) ; } description afterRemove ( event ) { MediaService . remove ( event . entity ) ; CacheService . refresh ( 'medias' ) ; } }