Thread: Fw: [GENERAL] PLV8 and JS exports / referencing

Fw: [GENERAL] PLV8 and JS exports / referencing

From
Jon Erdman
Date:
Got radio silence on this question in -general, and upon reflection I think it might be a -hacker level question. I'm
notsure who actually implemented PLV8, but I think it might take someone at or near that level to answer. 

If not, sorry for the noise. Thanks!

If anyone wants to try to reproduce it, replace the snipped out bits using the contents of mustache.js from
http://github.com/janl/mustache.jsand then fiddle with my "return" line at the bottom to try to reference any function
that'sdefined in that above snipped portion (such as .render()). Here's a sample sql call to the full defined pg
function,that does work with my hacked up one that removes the factory: 

SELECT mustache('   CREATE TABLE {{{ table_name }}} (       {{{ table_name }}}_id SERIAL PRIMARY KEY       {{ #cols }}
    , {{{ def }}}       {{ /cols }}   );'   , '{           "table_name": "my_table"           , "cols": [
{ "def": "t text" }               , { "def": "i int" }           ]      }' 
);

--
Jon Erdman (aka StuckMojo)   PostgreSQL Zealot



Begin forwarded message:

Date: Wed, 5 Nov 2014 17:01:29 -0600
From: Jon Erdman <postgresql@thewickedtribe.net>
To: pgsql-general@postgresql.org
Subject: [GENERAL] PLV8  and JS exports / referencing



So, I was trying to use mustache.js in PG by defining a V8 function that imports it. Older versions worked fine, but in
newerversions they use a class factory and I can't figure out how to reference the mustache stuff that it creates.
ApparentlyI need to know how our V8 implementation does exports.  

Here's the top of mustache.js with the class factory, and my attempted reference at the bottom (the return which gives
meundefined reference). I tried various invocations and couldn't quite get it. I ended up hacking it up to remove the
factoryand change it to explicitly declare a variable to make it work, but I'd like to avoid that if possible. 

If anyone wants to try to reproduce it, replace the snipped out bits using the contents of mustache.js from
http://github.com/janl/mustache.js 

CREATE OR REPLACE FUNCTION mustache(template text, view json)   RETURNS TEXT   LANGUAGE plv8   IMMUTABLE   STRICT
AS $$

(function (global, factory) { if (typeof exports === "object" && exports) {   factory(exports); // CommonJS } else if
(typeofdefine === "function" && define.amd) {   define(['exports'], factory); // AMD } else {   factory(global.Mustache
={}); // <script> } 
}(this, function (mustache) {
 var Object_toString = Object.prototype.toString; var isArray = Array.isArray || function (object) {
...
... SNIP ...
...
}));

return mustache.render(template, view);
$$;

--
Jon Erdman (aka StuckMojo)   PostgreSQL Zealot

Re: Fw: [GENERAL] PLV8 and JS exports / referencing

From
Sehrope Sarkuni
Date:
On Sat, Nov 8, 2014 at 12:13 PM, Jon Erdman
<postgresql@thewickedtribe.net> wrote:
>
> So, I was trying to use mustache.js in PG by defining a V8 function that imports it. Older versions worked fine, but
innewer versions they use a class factory and I can't figure out how to reference the mustache stuff that it creates.
ApparentlyI need to know how our V8 implementation does exports. 

If you define a variable named "exports" before the Mustache factory
code then it'll make it think it's loading in a CommonJS environment
and the "exports" variable will be populated with the Mustache
methods.

I'm not too familiar with PLV8 (so I'm not sure if there's a
better/direct way to support modules) but the following works fine:

CREATE OR REPLACE FUNCTION mustache(template text, view json)   RETURNS TEXT   LANGUAGE plv8   IMMUTABLE   STRICT
AS $BODY$
var exports = {};
// Copy/paste https://raw.githubusercontent.com/janl/mustache.js/master/mustache.js
var Mustache = exports;
return Mustache.render(template, view);
$BODY$;

test=> SELECT mustache('test: {{foo}}', '{"foo": "bar"}'::json);mustache
-----------test: bar
(1 row)

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/