Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Handle "soft deletes"

default discord avatar
gaboluque2 years ago
6

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
    2 years ago

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

  • discord user avatar
    jmikrut
    2 years 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
    gaboluque2 years ago
    @360823574644129795

    what I mean by omit is exactly what

    @364124941832159242

    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
    2 years ago

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

  • discord user avatar
    jmikrut
    2 years 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
    gaboluque2 years ago

    Thanks guys!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.