feedback
This commit is contained in:
parent
4e760c50a7
commit
3aea58a015
5 changed files with 50 additions and 24 deletions
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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,
|
||||||
|
|
|
@ -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;
|
||||||
|
|
14
server/src/infra/migrations/1691490366889-AddAuditTable1.ts
Normal file
14
server/src/infra/migrations/1691490366889-AddAuditTable1.ts
Normal 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"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
server/src/infra/subscribers/asset.subscriber.ts
Normal file
33
server/src/infra/subscribers/asset.subscriber.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue