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.

[Postgres] Payload trigger functions?

default discord avatar
andreasb_last year
4

Does Payload have trigger functions similar to Supabase, running on every insert/update/delete? I've seen the collection hooks, but I wonder if there's a more database-native way of dealing with this rather than with JS hooks. My use case is to automatically update an aggregate table whenever data/rows have changed in another table.

  • discord user avatar
    denolfe
    last year

    Payload does not have anything like that at the database level. Since Payload wraps Drizzle, it's theoretically possible for you to write some associated scripts that will achieve this, though.



    From your description, it seems like Payload hooks would be able to achieve this easily. What's the reasoning for not wanting to do it that way?

  • default discord avatar
    andreasb_last year

    Thanks for the super quick answer! 🙌


    I'm very new to Payload so I might not be very familiar with the hooks system.


    Two reasons though:


    1) Already got the business logic in plpgsql, was interested in whether this could easily be reused in Payload or not, and


    2) That Postgres trigger functions (as far as I know) do not require an additional network call, but as far as I can tell this would be the case with using hooks. That's not a biggie, but it introduces another possible point of failure which of course would be nice to avoid if something like trigger functions were available!

  • discord user avatar
    denolfe
    last year

    1. Understandable if you already have that work done


    2. Payload leverages the Local API for hooks, so it isn't necessarily a REST call. Any interactions is direct to DB.

  • default discord avatar
    andreasb_last year

    Neat. I'll give it a try! I'm not a big fan of writing plpgsql if I could choose, so it has been more out of necessity. Appreciate the help!

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.