PLJS – JavaScript Language Plugin for PostreSQL
github.com
github.com
I am not against having business logic in database, there are very legitimate uses for that. However, few things must be noted.
First, code is idempotent: you compile, package, deploy code, then nuke it all out and replace with different version, be it older or newer. Swapping application binaries/images/containers is the gold standard in deployment. Databases are... Perpetual. Rolling back DDLs without rolling back data can be a very complex exercise.
Second, there is no automatic separation of interface and implementation in databases. Database engine will not automatically enforce constraints embedded in stored procedures. Which means extra care must be taken and additional processes must be implemented to control access, otherwise you risk corrupting data.
Embedding application code in database breaks separation boundary, which changes project management. Now you have database development and deployment much more closely tied to application code development and deployment. Without huge care in project management this effectively inhibits any non-linear development flows.
At the very least, this approach makes things feel much safer. I would like to know if there better ways to do it, and if there are similar approaches for Postgres procedures.
It lets you write a function in nodejs that actually executes in plv8 inside the database.
To your project code though, it mostly looks like a normal js function (with some limitations).
Amongst other benefits, it allows you to manage your plv8 functions as a normal part of your repo.
actually i wrote a couple tables/stored procedures that (should probably mostly) do this for postgres after looking at some other patch management libraries by depesz and steve purcell a couple weeks ago
Plv8 is widely available from cloud hosting providers including AWS, GCP and Azure.
also, there are a ton of different variables. if you're interested in talking about it, there's a discord: https://discord.gg/5fJN52Se
Though v8 has become much slower every release when crossing the membrane that pljs is much faster under many use cases, even in early alpha.
Here is more context for the QuickJS engine experiment as the runtime for a pl language.
passing through v8's javascript/c++ membrane has always been painful, and appears to be getting worse.
The ability to do advanced regexp replacements with a function parameter in JS far outstrips anything native in Postgres.
Array manipulation, reordering, and deduplication are worlds easier in JS than native Postgres.
Manipulating byte buffers in JS are much faster and more flexible than introspecting bytea columns in native Postgres.
JSON parsing and object transformation are much easier, faster, and clearer than native Postgres (even with the recent jsonb improvements).
I was away at a conference (jsconf, amusingly), and simply added the arcgis to geojson converter that was already written in javascript as part of another open source project we had released, and voila, we could suddenly support it in the database. one of the product engineers quickly added an additional database call to the backend, and we were all set.
simple problems, simple solutions, especially when dealing with json conversion.