Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesdixon/c66c52a609f7200817da to your computer and use it in GitHub Desktop.
Save jamesdixon/c66c52a609f7200817da to your computer and use it in GitHub Desktop.

Revisions

  1. jamesdixon revised this gist Oct 22, 2015. 1 changed file with 0 additions and 13 deletions.
    13 changes: 0 additions & 13 deletions plv8-javascript-modules-loading.sql
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,3 @@
    /******************************************************************************
    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`

    /******************************************************************************
  2. jamesdixon revised this gist Oct 22, 2015. 1 changed file with 5 additions and 42 deletions.
    47 changes: 5 additions & 42 deletions plv8-javascript-modules-loading.sql
    Original 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

    /******************************************************************************
    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
    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 <database>
    \set underscore `cat underscore-min.js`
    \set jpath `cat plv8-jpath.js`
    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 ('underscore',true,:'underscore'),
    ('jpath',true,:'jpath');
    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("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 (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
    var match = jpath.filter(jsonData, "people[gender=male && age < 25]");
    plv8.elog(NOTICE,JSON.stringify(match));
    $$;
    do language plv8 ' load_module("rrule"); ';
  3. @rafaelveloso rafaelveloso revised this gist Dec 19, 2014. 1 changed file with 14 additions and 18 deletions.
    32 changes: 14 additions & 18 deletions plv8-javascript-modules-loading.sql
    Original 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 + "})")();
    }

    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));
    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"}
    { 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
  4. @rafaelveloso rafaelveloso revised this gist Dec 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plv8-javascript-modules-loading.sql
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@ do language plv8 $$
    $$;

    /******************************************************************************
    Test jpath module's filter function
    Test jpath module's filter function (github module example)
    ******************************************************************************/

    do language plv8 $$
  5. @rafaelveloso rafaelveloso created this gist Dec 19, 2014.
    93 changes: 93 additions & 0 deletions plv8-javascript-modules-loading.sql
    Original 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));
    $$;