prisma-soft-delete-middleware is a utility library for Prisma that provides an efficient way to implement soft deletion for database records using Prisma's middleware system. The current stable version is `1.3.1`. It is actively maintained with a history of regular updates, including recent bug fixes and feature enhancements, though its release cadence may slow as the community shifts towards Prisma Extensions. This library's key differentiator is its ability to automatically handle cascading soft deletes across related models and to filter out soft-deleted records from various Prisma queries, including `findFirst`, `findMany`, `findUnique`, and operations involving `include` or `select`. It achieves this by intelligently modifying query arguments before they reach the database, leveraging `prisma-nested-middleware` for complex nested scenarios. While functional and maintained, Prisma's middleware system is deprecated in favor of Prisma Extensions, and users are encouraged to migrate to `prisma-extension-soft-delete` for future-proof applications.
npm install prisma-soft-delete-middlewareVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to set up the soft delete middleware with a PrismaClient, create and soft-delete a record, and then query for it both with and without explicitly including soft-deleted records.
Consider migrating to `prisma-extension-soft-delete` for long-term compatibility and to leverage the latest Prisma features. Install `npm install prisma-extension-soft-delete` and update your client setup.
Ensure all models intended for soft deletion include a `deleted: Boolean @default(false)` field or a custom field defined in the `createSoftDeleteMiddleware` configuration.
Upgrade to `prisma-soft-delete-middleware@1.2.0` or newer if you are using Prisma v5 with `find*OrThrow` methods.
If using a non-boolean `deleted` field (e.g., `DateTime`), configure `createSoftDeleteMiddleware` with `{ models: { YourModel: { field: 'deletedAt', deleteValue: new Date() } } }` to match your schema's update behavior.Ensure `prisma.$use(createSoftDeleteMiddleware({ models: { /* your models */ } }))` is called before any database operations.Pass `includeSoftDeleted: true` directly as a top-level argument to the query function (e.g., `prisma.user.findMany({ includeSoftDeleted: true })`) as enabled by the middleware.Add the `deleted: Boolean @default(false)` field to the respective model in your `schema.prisma` file, then run `npx prisma generate`.
Ensure all models you wish to apply soft delete to are explicitly listed in the `createSoftDeleteMiddleware` configuration: `{ models: { User: true, Post: true } }`.