mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-30 06:29:11 +02:00
Soft delete and house keeping endpoint (#184)
This commit is contained in:
parent
97e189132f
commit
3299ba2030
14 changed files with 326 additions and 14 deletions
48
prisma/middlewares/softDeleteMiddleware.ts
Normal file
48
prisma/middlewares/softDeleteMiddleware.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { Prisma, PrismaClient } from "@prisma/client";
|
||||
|
||||
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 };
|
||||
}
|
||||
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 };
|
||||
}
|
||||
}
|
||||
if (params.action === "findUnique" || params.action === "findFirst") {
|
||||
// Change to findFirst - you cannot filter
|
||||
// by anything except ID / unique with findUnique
|
||||
params.action = "findFirst";
|
||||
// Add 'deleted' filter
|
||||
// ID filter maintained
|
||||
params.args.where["deleted"] = false;
|
||||
}
|
||||
if (params.action === "findMany") {
|
||||
// Find many queries
|
||||
if (params.args.where) {
|
||||
if (params.args.where.deleted == undefined) {
|
||||
// Exclude deleted records if they have not been explicitly requested
|
||||
params.args.where["deleted"] = false;
|
||||
}
|
||||
} else {
|
||||
params.args["where"] = { deleted: false };
|
||||
}
|
||||
}
|
||||
}
|
||||
return next(params);
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue