Backend Boilerplates javascript Intermediate

Prisma Soft Delete Middleware

Globally intercept Prisma queries to filter soft-deleted records transparently.

javascript
module.exports = (prisma) => {
  const reads = ['findUnique','findFirst','findMany'];
  prisma.$use(async (params, next) => {
    if (reads.includes(params.action)) {
      params.args = params.args || {};
      params.args.where = { ...params.args.where, deletedAt: null };
    }
    if (params.action === 'delete') {
      params.action    = 'update';
      params.args.data = { deletedAt: new Date() };
    }
    return next(params);
  });
};

Prerequisites

  • @prisma/client >= 4

Pro Tips

  • → Add DB index on deletedAt.
  • → Override for admin routes that need deleted records.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
javascript
Level
Intermediate
Read time
3 min
Published
Jul 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.