This commit is contained in:
Alex Tran 2023-08-08 05:27:50 -05:00
parent 4e760c50a7
commit 3aea58a015
5 changed files with 50 additions and 24 deletions

View file

@ -1,21 +0,0 @@
import { AssetEntity } from '@app/infra/entities';
import { EntitySubscriberInterface, EventSubscriber, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm';
@EventSubscriber()
export class AssetAudit implements EntitySubscriberInterface<AssetEntity> {
listenTo() {
return AssetEntity;
}
afterInsert(event: InsertEvent<AssetEntity>): void | Promise<any> {
console.log('AFTER ENTITY INSERTED: ', event.entity);
}
afterRemove(event: RemoveEvent<AssetEntity>): void | Promise<any> {
console.log('AFTER ENTITY WITH ID ' + event.entityId + ' REMOVED: ', event.entity);
}
afterUpdate(event: UpdateEvent<AssetEntity>): void | Promise<any> {
console.log('AFTER ENTITY UPDATED: ', event.entity);
}
}

View file

@ -17,7 +17,7 @@ export const databaseConfig: PostgresConnectionOptions = {
entities: [__dirname + '/entities/*.entity.{js,ts}'], entities: [__dirname + '/entities/*.entity.{js,ts}'],
synchronize: false, synchronize: false,
migrations: [__dirname + '/migrations/*.{js,ts}'], migrations: [__dirname + '/migrations/*.{js,ts}'],
subscribers: [__dirname + '/../domain/database-subscriber/*.{js,ts}'], subscribers: [__dirname + '/subscribers/*.{js,ts}'],
migrationsRun: true, migrationsRun: true,
connectTimeoutMS: 10000, // 10 seconds connectTimeoutMS: 10000, // 10 seconds
...urlOrParts, ...urlOrParts,

View file

@ -13,7 +13,7 @@ export enum EntityType {
@Entity('audit') @Entity('audit')
export class AuditEntity { export class AuditEntity {
@PrimaryGeneratedColumn('increment') @PrimaryGeneratedColumn('increment')
id!: string; id!: number;
@Column() @Column()
entityType!: EntityType; entityType!: EntityType;
@ -25,7 +25,7 @@ export class AuditEntity {
action!: DatabaseAction; action!: DatabaseAction;
@Column() @Column()
onwerId!: string; ownerId!: string;
@Column() @Column()
userId!: string; userId!: string;

View file

@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddAuditTable11691490366889 implements MigrationInterface {
name = 'AddAuditTable11691490366889'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "audit" RENAME COLUMN "onwerId" TO "ownerId"`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "audit" RENAME COLUMN "ownerId" TO "onwerId"`);
}
}

View file

@ -0,0 +1,33 @@
import { EntitySubscriberInterface, EventSubscriber, InsertEvent, LoadEvent, RemoveEvent, UpdateEvent } from 'typeorm';
import { AssetEntity, AuditEntity, DatabaseAction, EntityType } from '../entities';
@EventSubscriber()
export class AssetAudit implements EntitySubscriberInterface<AssetEntity> {
private assetEntity: AssetEntity = new AssetEntity();
listenTo() {
return AssetEntity;
}
afterLoad(entity: AssetEntity, event?: LoadEvent<AssetEntity> | undefined): void | Promise<any> {
this.assetEntity = entity;
}
afterInsert(event: InsertEvent<AssetEntity>): void | Promise<any> {
console.log('AFTER ENTITY INSERTED: ', event.entity);
}
afterRemove(event: RemoveEvent<AssetEntity>): void | Promise<any> {
console.log('AFTER ENTITY WITH ID ' + event.entityId + ' REMOVED: ', event.entity);
}
async afterUpdate(event: UpdateEvent<AssetEntity>): Promise<any> {
const audit = event.manager.getRepository(AuditEntity);
const auditEntity = new AuditEntity();
auditEntity.action = DatabaseAction.UPDATE;
auditEntity.entityType = EntityType.ASSET;
console.log('AFTER ENTITY UPDATED: ', this.assetEntity.isFavorite);
}
}