Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Additional Soft Delete Fields (e.g., deletedBy) #34

Open
shahadat3669 opened this issue Mar 3, 2025 · 0 comments
Open

Support for Additional Soft Delete Fields (e.g., deletedBy) #34

shahadat3669 opened this issue Mar 3, 2025 · 0 comments

Comments

@shahadat3669
Copy link

Description

Currently, the prisma-extension-soft-delete library provides support for soft deleting records by adding a deletedAt or deleted field to models. However, in many applications, it is also necessary to track who deleted the record by adding an additional field such as deletedBy.

This feature would allow developers to store the ID of the user (or another identifier) responsible for soft deleting the record. This is particularly useful for auditing and security purposes.


Proposed Solution

Enhance the createSoftDeleteExtension function to support an optional deletedBy field (or any additional custom field) that stores the identifier of the user who performed the deletion.

Example Configuration

The extension should allow users to specify an additional field for tracking deletions, like this:

const extendedClient = client.$extends(
  createSoftDeleteExtension({
    models: {
      Comment: {
        field: "deletedAt",
        createValue: (deleted) => (deleted ? new Date() : null),
        additionalFields: {
          deletedBy: (deleted, context) => (deleted ? context.userId : null),
        },
      },
    },
  })
);
Explanation of the New Configuration
  • additionalFields: A new property that allows defining extra fields for soft delete, such as deletedBy.
  • deletedBy: (deleted, context) => ...
    • If deleted is true, it sets the deletedBy field to context.userId.
    • Otherwise, it resets the deletedBy field to null.
  • context: Represents additional metadata (e.g., the currently authenticated user).

Expected Behavior

  • When a record is soft deleted, the deletedBy field should be automatically populated with the user ID.
  • If the record is restored (soft delete reversed), the deletedBy field should be set to null.
  • Developers should have the flexibility to configure multiple additional fields, not just deletedBy.

Use Case Example

If a user with id: 123 soft deletes a comment, the record should look like this:

{
  "id": 1,
  "content": "This is a comment",
  "deletedAt": "2025-03-03T12:00:00.000Z",
  "deletedBy": 123
}

Conclusion

This enhancement would significantly improve the usability of prisma-extension-soft-delete by providing a way to track the user responsible for deletions. It would be great to have support for this feature in the next release! 🚀

Would love to hear thoughts from the maintainers on feasibility and potential implementation! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant