olivierwilkinson / prisma-extension-soft-delete

Prisma extension for adding soft delete to Prisma models, even when using nested queries
Apache License 2.0
97 stars 14 forks source link

Setting multiple fields on delete - deletedAt and deletedById #16

Open rsslldnphy opened 7 months ago

rsslldnphy commented 7 months ago

In our app we set a timestamp + userId pair when an entity is created, updated, or deleted - so for deletions, deletedAt and deletedById (a foreign key of the user table).

It would be great if createValue would allow us to set multiple fields when a record is deleted so both these could be set. Or alternatively, to keep the API backwards compatible, a createValues plural option could be added so the behaviour of the original option doesn't need to change.

I'd imagine it being used something like this:

const softdeletes = (user: User) => createSoftDeleteExtension({
    models: {
        User: true,
        Post: true,
    }
    defaultConfig: {
        field: "deletedAt",
        createValues: (deleted) => (
            deleted ? { deletedAt: new Date(), deletedById: user.id } : { }
        )
    }
})

export const getConnectionForUser = (user: User) => {
    return prisma.$extend(sofdeletes(user))
}

If this is of interest, I'd be happy having a crack at a PR?

StiliyanKushev commented 1 month ago

+1 for this. This would allow us to have a proper unique compound constrain based on both isDeleted and deletedAt along with some other property like name.