-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.subscriber.ts
34 lines (31 loc) · 1017 Bytes
/
model.subscriber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { EventSubscriber, FlushEventArgs, Subscriber } from "@mikro-orm/core";
import { Center } from "./center.model";
import context from "./context";
@Subscriber()
export class ModelSubscriber implements EventSubscriber {
getSubscribedEntities() {
return [Center];
}
afterFlush(args: FlushEventArgs): void | Promise<void> {
const username = context.get("username") as string;
const changes = args.uow.getChangeSets();
const marker = args.uow as any;
if (!marker.marked) {
marker.marked = true;
const changeLogs = [
...changes.map(
(change) =>
`Operation: ${change.type}, model: ${
change.name
}, by: ${username}, from: ${JSON.stringify(
change.originalEntity,
null,
4
)}, to: ${JSON.stringify(change.entity, null, 4)}`
),
];
// log changes. We could also send to paper trail, record in database, ...
console.log(changeLogs);
}
}
}