-
-
Notifications
You must be signed in to change notification settings - Fork 100
Custom language declaration (functions, do statements, plpgsql, plv8, ...)
Olivier Guimbal edited this page Feb 8, 2021
·
3 revisions
When creating a function (or executing a DO statement), you must provide a language.
You can register such languages in pg-mem via the db.registerLanguage()
api.
For instance, a simple implementation that will always throw on created functions call would be:
db.registerLanguage('mylang', () => {
return () => {
throw new Error('Functions not supported !')
}
});
Doing that, this will work:
CREATE FUNCTION myfunc(integer) AS $$ whatever you want $$ LANGUAGE mylang
However, this will throw a Functions not supported !
error:
SELECT myfunc(42);
If you declare simple plv8 functions, you could mock plv8 like this:
db.registerLanguage('plv8', ({ code, args }) => {
const argNames = args.map((x, i) => x.name ?? ('$' + i));
return new Function(...argNames, code) as CompiledFunction;
});
This, however, would only work for plv8 code that only use its arguments, and does not access any data, nor out arguments, such as:
create function add(x int, y int) returns int
as $$ x + y $$ language plv8;
select add(2, 40); -- 42 !
Please file an issue if you'd like more support for this !