zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] A real soft-delete feature

Open ymc9 opened this issue 2 years ago • 4 comments

Background

Current pattern: https://zenstack.dev/blog/soft-delete

But it has some limitations:

  • You can't call the regular delete method
  • onDelete referential actions don't work

Proposed Solution

The thought is to make it as simple as:

model Foo {
    ...
    deletedAt DateTime? @softDelete
}

And the enhance() API can have an option to opt-in for it:

const db = enhance(prisma, { user: ..., softDelete: true });

When enabled, the enhanced client has additional behavior:

  1. delete and deleteMany (including nested ones) become soft delete
  2. onDelete: Cascade propagate soft-delete to related entities
  3. Read APIs (findXXX, count, aggregate, groupBy) automatically exclude soft-deleted entities

More Context

Context from chat with ikishan on discord:

image

ymc9 avatar Jun 27 '23 09:06 ymc9

const db = enhance(prisma, { user: ..., softDelete: true });

It would be specifically cool if we can have softDelete without enhance/an alternative function that doesn't use the authorization layer

I have a global export of prisma and I use that in my webhooks and cron jobs. I would love if they could also have soft deletes, but there is no "user" in those contexts so enchance wouldn't make sense. Would also be nice if they can respect the validations as well.

nahtnam avatar Feb 19 '24 03:02 nahtnam

const db = enhance(prisma, { user: ..., softDelete: true });

It would be specifically cool if we can have softDelete without enhance/an alternative function that doesn't use the authorization layer

I have a global export of prisma and I use that in my webhooks and cron jobs. I would love if they could also have soft deletes, but there is no "user" in those contexts so enchance wouldn't make sense. Would also be nice if they can respect the validations as well.

With the new unified enhance API in V2, it's probably going to look like:

const dbSoftDelete = enhance(prisma, undefined, { kinds: ['soft-delete'] });

Data validation is also part of access control today. It'll be a separate task to spin it out 😄

ymc9 avatar Feb 19 '24 04:02 ymc9