PostgreSQL allows user-defined functions. They are written in other programming languages. These languages are generically called procedural languages (PLs). Heroku only supports one extra extension: PLV8.
This extension is very interesting because it provides a procedural language created by the JavaScript V8 engine. It allows you to write functions in JavaScript and use them with SQL.
In the following example, you will create a function in JavaScript that makes a join between two JSON objects.
First, you will install the PLV8 extension and then the join_json
function. This function accepts two JSON objects as parameters:
$ heroku pg:psql --app your-app-name CREATE EXTENSION plv8; CREATE OR REPLACE FUNCTION join_json(first JSON, second JSON) RETURNS JSON AS $$ for (var json_key in second) { first[json_key] = second[json_key]; } return first; $$ LANGUAGE plv8;
Finally, you will run SQL to make a union between the two JSON...