feat(server): audit log
This commit is contained in:
parent
1d37d8cac0
commit
4e760c50a7
5 changed files with 74 additions and 0 deletions
21
server/src/domain/database-subscriber/asset.subscriber.ts
Normal file
21
server/src/domain/database-subscriber/asset.subscriber.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
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,6 +17,7 @@ export const databaseConfig: PostgresConnectionOptions = {
|
|||
entities: [__dirname + '/entities/*.entity.{js,ts}'],
|
||||
synchronize: false,
|
||||
migrations: [__dirname + '/migrations/*.{js,ts}'],
|
||||
subscribers: [__dirname + '/../domain/database-subscriber/*.{js,ts}'],
|
||||
migrationsRun: true,
|
||||
connectTimeoutMS: 10000, // 10 seconds
|
||||
...urlOrParts,
|
||||
|
|
35
server/src/infra/entities/audit.entity.ts
Normal file
35
server/src/infra/entities/audit.entity.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
export enum DatabaseAction {
|
||||
CREATE = 'CREATE',
|
||||
UPDATE = 'UPDATE',
|
||||
DELETE = 'DELETE',
|
||||
}
|
||||
|
||||
export enum EntityType {
|
||||
ASSET = 'ASSET',
|
||||
}
|
||||
|
||||
@Entity('audit')
|
||||
export class AuditEntity {
|
||||
@PrimaryGeneratedColumn('increment')
|
||||
id!: string;
|
||||
|
||||
@Column()
|
||||
entityType!: EntityType;
|
||||
|
||||
@Column()
|
||||
entityId!: string;
|
||||
|
||||
@Column()
|
||||
action!: DatabaseAction;
|
||||
|
||||
@Column()
|
||||
onwerId!: string;
|
||||
|
||||
@Column()
|
||||
userId!: string;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamptz' })
|
||||
createdAt!: Date;
|
||||
}
|
|
@ -2,6 +2,7 @@ import { AlbumEntity } from './album.entity';
|
|||
import { APIKeyEntity } from './api-key.entity';
|
||||
import { AssetFaceEntity } from './asset-face.entity';
|
||||
import { AssetEntity } from './asset.entity';
|
||||
import { AuditEntity } from './audit.entity';
|
||||
import { PartnerEntity } from './partner.entity';
|
||||
import { PersonEntity } from './person.entity';
|
||||
import { SharedLinkEntity } from './shared-link.entity';
|
||||
|
@ -15,6 +16,7 @@ export * from './album.entity';
|
|||
export * from './api-key.entity';
|
||||
export * from './asset-face.entity';
|
||||
export * from './asset.entity';
|
||||
export * from './audit.entity';
|
||||
export * from './exif.entity';
|
||||
export * from './partner.entity';
|
||||
export * from './person.entity';
|
||||
|
@ -30,6 +32,7 @@ export const databaseEntities = [
|
|||
APIKeyEntity,
|
||||
AssetEntity,
|
||||
AssetFaceEntity,
|
||||
AuditEntity,
|
||||
PartnerEntity,
|
||||
PersonEntity,
|
||||
SharedLinkEntity,
|
||||
|
|
14
server/src/infra/migrations/1691462205943-AddAuditTable.ts
Normal file
14
server/src/infra/migrations/1691462205943-AddAuditTable.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class AddAuditTable1691462205943 implements MigrationInterface {
|
||||
name = 'AddAuditTable1691462205943'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE "audit" ("id" SERIAL NOT NULL, "entityType" character varying NOT NULL, "entityId" character varying NOT NULL, "action" character varying NOT NULL, "onwerId" character varying NOT NULL, "userId" character varying NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_1d3d120ddaf7bc9b1ed68ed463a" PRIMARY KEY ("id"))`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE "audit"`);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue