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

From: Sehrope Sarkuni <sehrope(at)jackdb(dot)com>
To: Jon Erdman <postgresql(at)thewickedtribe(dot)net>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fw: [GENERAL] PLV8 and JS exports / referencing
Date: 2014-11-08 19:23:08
Message-ID: CAH7T-arKMf3F56_c+y3b07F_J-Cyx_Waj69QyZV5JLR11qUPVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Nov 8, 2014 at 12:13 PM, Jon Erdman
<postgresql(at)thewickedtribe(dot)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 in newer versions they use a class factory and I can't figure out how to reference the mustache stuff that it creates. Apparently I 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/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2014-11-08 19:57:59 Re: two dimensional statistics in Postgres
Previous Message Tom Lane 2014-11-08 18:09:23 Re: row_to_json bug with index only scans: empty keys!