Handle "soft deletes"

default discord avatar
gaboluque
3 months ago
8

Is there a way to handle "soft deletes"?


E.g set user status = DELETED



A hook that will omit DELETED users by default?

  • discord user avatar
    alessiogr
    Payload Team
    3 months ago

    Heya! What do you mean with "omit"? Not display those in the API response?

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    you could add a custom field that called

    deleted

    and then write access control to return a query constraint like the following:



    {
      deleted: {
        not_equals: true
      }
    }


    that way any posts marked as deleted will still exist in the database, but will automatically be omitted from all API responses

  • default discord avatar
    gaboluque
    3 months ago

    @alessiogr what I mean by omit is exactly what @jmikrut is suggesting. Having records "deleted", never queried in the DB, but still there.



    I ended up adding a

    beforeOperation

    hook, with something like this:



      return { ...args, where: { ...args.where, status: { not_equals: UserStatus.DELETED, ...args.status } } }


    Although I'm not sure how this will affect other queries 👀 . WDYT? Maybe the Access Control is more performant?

  • discord user avatar
    dribbens
    Payload Team
    3 months ago

    As long as you have an index on UserStatus, it will be very performant.

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    i would use access control instead of a

    beforeOperation

    hook but honestly either is the same performance. and yes, as dan said, make sure to index the field 👍

  • default discord avatar
    gaboluque
    3 months ago

    Thanks guys!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.