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.
Revisions
-
jamesdixon revised this gist
Oct 22, 2015 . 1 changed file with 0 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,3 @@ \set rrule `cat rrule.js` /****************************************************************************** -
jamesdixon revised this gist
Oct 22, 2015 . 1 changed file with 5 additions and 42 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,22 +3,15 @@ How to load Javascript modules into postgres ******************************************************************************/ CREATE EXTENSION IF NOT EXISTS plv8 curl -O https://raw.githubusercontent.com/jkbrzt/rrule/master/lib/rrule.js /****************************************************************************** Open postgres console and set runtime variable to use the code later ******************************************************************************/ psql -d scout \set rrule `cat rrule.js` /****************************************************************************** Now that we have set variable containing the code @@ -28,8 +21,7 @@ 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 @@ -57,33 +49,4 @@ select plv8_startup(); Load both modules into postgres using the previously created plv8 function ******************************************************************************/ do language plv8 ' load_module("rrule"); '; -
rafaelveloso revised this gist
Dec 19, 2014 . 1 changed file with 14 additions and 18 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -42,16 +42,12 @@ 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 + "})")(); } }; $$; @@ -68,9 +64,9 @@ Test the underscore module's extend function ******************************************************************************/ do language plv8 $$ x = { 'a':1 }; y=_.extend(x, { 'a':2,'b':3 }, { 'b':4, 'c':5 }); plv8.elog(NOTICE,JSON.stringify(y)); $$; /****************************************************************************** @@ -80,11 +76,11 @@ Test jpath module's filter function (github module example) do language plv8 $$ var jsonData = { people: [ { name: "John", age: 26, gender: "male" }, { name: "Steve", age: 24, gender: "male" }, { name: "Susan", age: 22, gender: "female" }, { name: "Linda", age: 30, gender: "female" }, { name: "Adam", age: 32, gender: "male"} ] }; //Get all males younger then 25 -
rafaelveloso revised this gist
Dec 19, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -74,7 +74,7 @@ do language plv8 $$ $$; /****************************************************************************** Test jpath module's filter function (github module example) ******************************************************************************/ do language plv8 $$ -
rafaelveloso created this gist
Dec 19, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,93 @@ /****************************************************************************** How to load Javascript modules into postgres ******************************************************************************/ CREATE EXTENSION IF NOT EXISTS plv8 /****************************************************************************** First step is download the Javascript module file Example with undescore-min and node-jpath ******************************************************************************/ wget http://underscorejs.org/underscore-min.js wget https://raw.github.com/adunstan/plv8-jpath/master/lib/plv8-jpath.js /****************************************************************************** Open postgres console and set runtime variable to use the code later ******************************************************************************/ psql -d <database> \set underscore `cat underscore-min.js` \set jpath `cat plv8-jpath.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 ('underscore',true,:'underscore'), ('jpath',true,:'jpath'); /****************************************************************************** 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("underscore"); load_module("jpath"); '; /****************************************************************************** Test the underscore module's extend function ******************************************************************************/ do language plv8 $$ x = {'a':1}; y=_.extend(x,{'a':2,'b':3},{'b':4,'c':5}); plv8.elog(NOTICE,JSON.stringify(y)); $$; /****************************************************************************** Test jpath module's filter function ******************************************************************************/ do language plv8 $$ var jsonData = { people: [ {name: "John", age:26, gender:"male"}, {name: "Steve", age:24, gender:"male"}, {name: "Susan", age:22, gender:"female"}, {name: "Linda", age:30, gender:"female"}, {name: "Adam", age:32, gender:"male"} ] }; //Get all males younger then 25 var match = jpath.filter(jsonData, "people[gender=male && age < 25]"); plv8.elog(NOTICE,JSON.stringify(match)); $$;