zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

Feature Request: Soft Delete #362

Closed bvkimball closed 1 year ago

bvkimball commented 1 year ago

Is your feature request related to a problem? Please describe. Currently, Prisma Schema Language doesn't provide a built-in way to handle soft deletes for objects. As a result, developers have to manually manage soft deleted objects in their applications. This can be cumbersome, especially when dealing with larger projects.

Describe the solution you'd like I propose adding an attribute to Zenstack that will allow users to easily implement soft deletes for objects. This could be done by introducing a new @deletedAt attribute, which can be applied to models in the schema definition. The attribute would automatically add the necessary fields for soft deletion, such as a deletedAt timestamp field, and would handle the relevant queries to filter out soft deleted records.

Example usage:

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  author    User     @relation(fields: [authorId], references: [id])
  authorId  Int
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  deletedAt DateTime @deletedAt
}

Describe alternatives you've considered An alternative is to manually implement soft deletes in the application logic by adding a deletedAt field and filtering out soft deleted records in every query. However, this can be error-prone and time-consuming, as developers need to ensure that they handle soft deletes correctly in all parts of their application.

Additional context Integrating the @deletedAt attribute into the Zenstack would provide a more streamlined and efficient way for developers to implement soft deletes in their applications. It would also promote best practices for handling soft deleted records and reduce the chances of bugs related to improper handling of soft deletes.

The prisma client should filter out deleted records for all standard actions ie find*. Open question would be how to find "deleted" Objects, this could be a new function for findDeleted or an extra option in the find* methods for includeDeleted: true.

jiashengguo commented 1 year ago

@bvkimball Actually using access policy we could already achieve that in ZenStack:

model Post {
  ...
  deleted Boolean @default(false) @omit
  @@deny('read', deleted)
  ...
}

There is a blog post about it:

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

bvkimball commented 1 year ago

awesome... sorry i missed this, I would still need to implement a middleware or extension as described to "override" the default delete* actions right?

jiashengguo commented 1 year ago

No. All you need to do is to define the policy in the schema as in the example above. ZenStack will take care of the rest. 😄