feat: implement real-time database synchronization with SSE and update backend modules
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Prisma, PrismaClient } from '@prisma/client';
|
||||
import { RealtimeEventsService } from '../realtime/realtime-events.service';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService
|
||||
@@ -7,9 +8,30 @@ export class PrismaService
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
private readonly logger = new Logger(PrismaService.name);
|
||||
private readonly writeActions = new Set<string>([
|
||||
'create',
|
||||
'update',
|
||||
'upsert',
|
||||
'delete',
|
||||
'createMany',
|
||||
'updateMany',
|
||||
'deleteMany',
|
||||
]);
|
||||
|
||||
constructor() {
|
||||
constructor(private readonly realtimeEvents: RealtimeEventsService) {
|
||||
super();
|
||||
|
||||
const realtimeMiddleware: Prisma.Middleware = async (params, next) => {
|
||||
const result = await next(params);
|
||||
|
||||
if (params.model && this.writeActions.has(params.action)) {
|
||||
this.realtimeEvents.notifyDatabaseWrite();
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
this.$use(realtimeMiddleware);
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
|
||||
Reference in New Issue
Block a user