Skip to content

Instantly share code, notes, and snippets.

@daviddahl
Last active June 22, 2017 02:52
Show Gist options
  • Save daviddahl/59c126e550bf09169b1cbcdc6904bf34 to your computer and use it in GitHub Desktop.
Save daviddahl/59c126e550bf09169b1cbcdc6904bf34 to your computer and use it in GitHub Desktop.
plv8-modules
#! /bin/bash
sudo apt-get install npm
cd /var/django
npm install moment moment-timezone
sudo -u postgres psql -d template1 -c "create extension plv8;"
sudo -u postgres psql -d template1 << EOF
\set moment `cat /var/django/node_modules/moment/min/moment-with-locales.js /var/django/node_modules/moment-timezone/moment-timezone.js`
create table plv8_modules(modname text primary key, load_on_start boolean, code text);
insert into plv8_modules values ('moment',true,:'moment');
create or replace function plv8_require(modname varchar)
returns void
language plv8
as
$$
if (plv8[modname]){
return;
}
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 + "})")();
$$;
create or replace function plv8_require_test(modname varchar)
returns text
language plv8
as
$$
var require = plv8.find_function('plv8_require');
require(modname);
return modname;
$$;
create or replace function plv8_require_moment_test()
returns text
language plv8
as
$$
var require = plv8.find_function('plv8_require');
require('moment');
return moment().tz("2014-06-01 12:00", "America/New_York");
$$;
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment