Do you need to create a separate service to handle your other business logic?
I know technically you can store procedures in some DBs like Postgres, but that doesn’t seem ideal to me.
Do you need to create a separate service to handle your other business logic?
I know technically you can store procedures in some DBs like Postgres, but that doesn’t seem ideal to me.
For our MVP, we built out everything that we could in SQL procedures (they're honestly not that terrible, especially in Postgres, just a bit verbose), we could also use `pgv8` and write them in JavaScript. If we need to trigger some special logic, we push data through webhooks to another service that also has access to the database. It works, but it's kind of messy and I'm somewhat worried about the performance of the HTTP client bundled with Postgres. This mechanism could also be implemented with the `LISTEN`/`NOTIFY` interface.
It feels like the better way to do it is having an API gateway / a separate API accepting the same JWT requests as PostgREST and others. We'll try it for our next project and maybe we'll migrate this product too, if we run into any issues with HTP requests.
It's been great for CRUD-heavy apps. Optimizing the RLSes sometimes gets tough, but you can always work around them with views / functions that avoid them.
I think the API gateway model makes more sense in my head.
Or you can take advantage of the `LISTEN/NOTIFY` feature in Postgres and use an extension to push events out to various queues or webhooks [1].
[0] https://hackage.haskell.org/package/postgresql-replicant-0.1...
[1] https://postgrest.org/en/v9.0/ecosystem.html#external-notifi...
For data-heavy apps with very complex schemas, your code can actually get more readable inside stored procedures. I used procedures for a content creation pipeline/headless CMS app that had flexible workflow templates (those also lived in the DB). It works quite well, and is simpler and more readable than most common backend code and much faster to develop.
If you need to trigger async stuff, like network access or data processing, one pattern I enjoy is writing to a log-ish table that gets picked up and processed later by a separate service. This is nice for emails and other types of notifications: your users get a log of sent emails, and you can very easily do it inside a transaction.
If the complexity is on the querying side, then you can get a lot of mileage from views. If you need caching for that, materialized views really help.
However: If you're doing complex numeric/algorithmic stuff, rely on external libraries, or has just plain too much business logic... then doing things in a separate service is definitely better, no question about it.
Triggers, I'm personally not a fan.
How long has Postgres been around? How long has your currently fashionable front-end framework been around?
I avoid triggers but put as much of my applications into stored procedures as I can.
If we then take your argument and instead just apply it to, say, using JS on the backend.. well, JS is not going anywhere any time soon.
pl/pgsql is absolutely awful and it is a disservice to other programming languages to call it a programming language. Writing actual, complicated business logic using this and maybe some mix of JS(because why not pull that into the databse?? great idea) is just going to make every single person who has to maintain this in the future hate your guts.
I'm truly glad I don't have to work with anyone who genuinely thinks such a design would be a good idea.
the comment makes sense if one interprets "frontend" as "all the shit in front of the database". which is a reasonable interpretation in the context of something like postgrest, if not perhaps satisfying to you.
> pl/pgsql is absolutely awful
who cares
> Writing actual, complicated business logic using this and maybe some mix of JS(because why not pull that into the databse?? great idea)
there are a number of other languages available for writing postgresql procedures, and you can add new ones
They say that it's great for production too, but I'm not so sure.