mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-21 12:56:21 +02:00
♻️ Improvements to house-keeping webhook (#1063)
This commit is contained in:
parent
0d82c158ce
commit
90f0d90fab
9 changed files with 173 additions and 104 deletions
|
@ -1,7 +1,5 @@
|
|||
import { PrismaClient } from "@rallly/database";
|
||||
|
||||
import { softDeleteMiddleware } from "./middleware/soft-delete-middleware";
|
||||
|
||||
export * from "@prisma/client";
|
||||
|
||||
declare global {
|
||||
|
@ -12,6 +10,4 @@ declare global {
|
|||
|
||||
export const prisma = global.prisma || new PrismaClient();
|
||||
|
||||
softDeleteMiddleware(prisma, "Poll");
|
||||
|
||||
if (process.env.NODE_ENV !== "production") global.prisma = prisma;
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
import { Prisma, PrismaClient } from "@rallly/database";
|
||||
|
||||
export const softDeleteMiddleware = (
|
||||
prisma: PrismaClient,
|
||||
model: Prisma.ModelName,
|
||||
) => {
|
||||
prisma.$use(async (params, next) => {
|
||||
// We use middleware to handle soft deletes
|
||||
// See: https://www.prisma.io/docs/concepts/components/prisma-client/middleware/soft-delete-middleware
|
||||
if (params.model === model) {
|
||||
if (params.action === "delete") {
|
||||
// Delete queries
|
||||
// Change action to an update
|
||||
params.action = "update";
|
||||
params.args["data"] = { deleted: true, deletedAt: new Date() };
|
||||
}
|
||||
if (params.action == "deleteMany") {
|
||||
// Delete many queries
|
||||
params.action = "updateMany";
|
||||
if (params.args.data != undefined) {
|
||||
params.args.data["deleted"] = true;
|
||||
} else {
|
||||
params.args["data"] = { deleted: true, deletedAt: new Date() };
|
||||
}
|
||||
}
|
||||
if (params.action === "findFirst") {
|
||||
// Add 'deleted' filter
|
||||
// ID filter maintained
|
||||
params.args.where["deleted"] = params.args.where["deleted"] || false;
|
||||
}
|
||||
}
|
||||
return next(params);
|
||||
});
|
||||
};
|
|
@ -0,0 +1,2 @@
|
|||
-- CreateIndex
|
||||
CREATE INDEX "votes_poll_id_idx" ON "votes" USING HASH ("poll_id");
|
|
@ -226,6 +226,7 @@ model Vote {
|
|||
|
||||
@@index([participantId], type: Hash)
|
||||
@@index([optionId], type: Hash)
|
||||
@@index([pollId], type: Hash)
|
||||
@@map("votes")
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue