Database Reference
In-Depth Information
PLV8 - V8 Engine JavaScript Procedural
Language
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 objects.
WITH my_join AS (
SELECT
'{"config":"true"}'::JSON f,
'{"user":23}'::JSON s
)
SELECT
f,
s,
join_json(f, s)
FROM my_join;
f | s |
join_json
Search WWH ::




Custom Search