Forked from rafaelveloso/plv8-javascript-modules-loading.sql
Last active
October 22, 2015 06:08
-
-
Save jamesdixon/c66c52a609f7200817da to your computer and use it in GitHub Desktop.
How to load Javascript modules into postgres using plv8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\set rrule `cat rrule.js` | |
/****************************************************************************** | |
Now that we have set variable containing the code | |
we need to create a table to store each of them in | |
postgres. | |
******************************************************************************/ | |
create table plv8_modules(modname text primary key, load_on_start boolean, code text); | |
insert into plv8_modules values ('rrule',true,:'rrule'); | |
/****************************************************************************** | |
Create a a startup function to create a plv8 function | |
that will be used to load the modules, Executing it | |
will register the plv8 function | |
******************************************************************************/ | |
create or replace function plv8_startup() | |
returns void | |
language plv8 | |
as | |
$$ | |
load_module = function(modname) { | |
var rows = plv8.execute("SELECT code from plv8_modules " +" where modname = $1", [modname]); | |
for (var r = 0; r < rows.length; r++) { | |
var code = rows[r].code; | |
eval("(function() { " + code + "})")(); | |
} | |
}; | |
$$; | |
select plv8_startup(); | |
/****************************************************************************** | |
Load both modules into postgres using the previously created plv8 function | |
******************************************************************************/ | |
do language plv8 ' load_module("rrule"); '; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment