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
gaboluquelast year
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
    last year

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

  • discord user avatar
    jmikrut
    last year

    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
    gaboluquelast year
    @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
    last year

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

  • discord user avatar
    jmikrut
    last year

    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
    gaboluquelast year

    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.