Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesdixon/c66c52a609f7200817da to your computer and use it in GitHub Desktop.
Save jamesdixon/c66c52a609f7200817da to your computer and use it in GitHub Desktop.
How to load Javascript modules into postgres using plv8
\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