import { EventSubscriberModel } from '@midwayjs/typeorm';
import { EntitySubscriberInterface, InsertEvent, UpdateEvent, RemoveEvent } from 'typeorm';

@EventSubscriberModel()
export class DbSubscriber implements EntitySubscriberInterface {
  /**
   * 添加前触发
   */
  beforeInsert(event: InsertEvent<any>) {
    console.log('BEFORE ENTITY INSERTED: ', event.entity);
    // console.log(this.ctx.request);
    // console.log(event.metadata.givenTableName);
  }

  /**
   * 修改前触发
   */
  beforeUpdate(event: UpdateEvent<any>) {
    console.log('BEFORE ENTITY UPDATED: ', event.entity);
  }

  /**
   * 删除前触发
   */
  beforeRemove(event: RemoveEvent<any>) {
    console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
  }

  /**
   * 添加后触发
   */
  afterInsert(event: InsertEvent<any>) {
    console.log('AFTER ENTITY INSERTED: ', event.entity);
  }

  /**
   * 修改后触发
   */
  afterUpdate(event: UpdateEvent<any>) {
    console.log('AFTER ENTITY UPDATED: ', event.entity);
  }

  /**
   * 删除后触发
   */
  afterRemove(event: RemoveEvent<any>) {
    console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
  }

  /**
   * 查询后触发
   */
  async afterLoad(entity: any) {
    // console.log('AFTER ENTITY LOADED: ', entity);
  }
}
