我正在关注 [这篇文章 ( http://adpgtech.blogspot.com.br/2013/03/loading-useful-modules-in-plv8.html ) 将 Node 模块加载到 Postgres PLV8 中。它可以工作,除了plv8_startup()
在 Postgres 启动时运行该函数。我在 Docker 容器中运行 Postgres,所以在我的 Docker 文件中,我调用以下命令让它运行上述函数:
sed -e "\$aplv8.start_proc = 'plv8_startup'" /var/lib/postgresql/data/postgresql.conf
不幸的是,当我调用使用 PLV8 的函数时,我正在加载的外部模块是未定义的。但是,如果我在数据库启动并运行后进入 PSQL ,节点模块会为该连接select plv8_startup()
正确加载。可悲的是,如果我打开到数据库的新连接并尝试运行我的 PLV8 函数,外部模块再次未定义,我不得不再次运行。select plv8_startup()
作为参考,这是我的文件:
// Dockerfile
FROM postgres:9.4
# install PLV8
RUN apt-get update && apt-get install -y postgresql-9.4-plv8
# copy any functions to be loaded
COPY sql/*.sql /sql/
# setup database and schema
COPY setup-schema.sh /docker-entrypoint-initdb.d/
块引用
// setup-schema.sh
#!/bin/bash
# setup PLV8 to load on startup
sed -e "\$aplv8.start_proc = 'plv8_startup'" /var/lib/postgresql/data/postgresql.conf
psql -U postgres <<EOSQL
CREATE DATABASE scout;
CREATE USER scout;
GRANT ALL PRIVILEGES ON DATABASE scout to scout;
EOSQL
# setup PLV8 module loading
psql -U postgres --dbname test -c 'CREATE EXTENSION plv8'
psql -U scout --dbname test < /sql/plv8_startup.sql
块引用
// plv8_startup.sql (loaded by Dockerfile)
-- create a table to store external javascript modules we'd like to use in our functions
CREATE TABLE plv8_modules(modname text primary key, load_on_start boolean, code text);
-- function makes our external javascript modules available to our functions
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 + "})")();
}
};
// now load all the modules marked for loading on start
var rows = plv8.execute("SELECT modname, code from plv8_modules where load_on_start");
for (var r = 0; r < rows.length; r++)
{
var code = rows[r].code;
eval("(function() { " + code + "})")();
}
$$;
-- load the rrule.js module
INSERT INTO plv8_modules VALUES ('code', true, '(function(u){var l=typeof module!=="undefined"&&module.exports;var k=function(){if(!k._nlp){if(l){k._nlp=require("./nlp")}else{if(!(k._nlp=u._RRuleNLP)){throw new Error("You need to include rrule/nlp.js for fromText/toText to work.")}}}return k._nlp};var w={MONTH_DAYS:[31,28,31,30,31,30,31,31,30,31,30,31],ONE_DAY:1000*60*60*24,MAXYEAR:9999,ORDINAL_BASE:new Date(1970,0,1),PY_WEEKDAYS:[6,0,1,2,3,4,5],getYearDay:function(C){var D=new Date(C.getFullYear(),C.getMonth(),C.getDate());return Math.ceil((D-new Date(C.getFullYear(),0,1))/w.ONE_DAY)+1},isLeapYear:function(C){if(C instanceof Date){C=C.getFullYear()}return((C%4===0)&&(C%100!==0))||(C%400===0)},tzOffset:function(C){return C.getTimezoneOffset()*60*1000},daysBetween:function(G,F){var E=G.getTime()-w.tzOffset(G);var D=F.getTime()-w.tzOffset(F);var C=Math.abs(E-D);return Math.round(C/w.ONE_DAY)},toOrdinal:function(C){return w.daysBetween(C,w.ORDINAL_BASE)},fromOrdinal:function(C){var D=C*w.ONE_DAY;return new Date(w.ORDINAL_BASE.getTime()-w.tzOffset(w.ORDINAL_BASE)+D+w.tzOffset(new Date(D)))},monthRange:function(D,E){var C=new Date(D,E,1);return[w.getWeekday(C),w.getMonthDays(C)]},getMonthDays:function(C){var D=C.getMonth();return D==1&&w.isLeapYear(C)?29:w.MONTH_DAYS[D]},getWeekday:function(C){return w.PY_WEEKDAYS[C.getDay()]},combine:function(C,D){D=D||C;return new Date(C.getFullYear(),C.getMonth(),C.getDate(),D.getHours(),D.getMinutes(),D.getSeconds(),D.getMilliseconds())},clone:function(C){var D=new Date(C.getTime());return D},cloneDates:function(E){var D=[];for(var C=0;C<E.length;C++){D.push(w.clone(E[C]))}return D},sort:function(C){C.sort(function(E,D){return E.getTime()-D.getTime()})},timeToUntilString:function(F){var D=new Date(F);var C,G=[D.getUTCFullYear(),D.getUTCMonth()+1,D.getUTCDate(),"T",D.getUTCHours(),D.getUTCMinutes(),D.getUTCSeconds(),"Z"];for(var E=0;E<G.length;E++){C=G[E];if(!/[TZ]/.test(C)&&C<10){G[E]="0"+String(C)}}return G.join("")},untilStringToDate:function(E){var C=/^(\d{4})(\d{2})(\d{2})(T(\d{2})(\d{2})(\d{2})Z)?$/;var D=C.exec(E);if(!D){throw new Error("Invalid UNTIL value: "+E)}return new Date(Date.UTC(D[1],D[2]-1,D[3],D[5]||0,D[6]||0,D[7]||0))}};w.Time=function(C,F,D,E){this.hour=C;this.minute=F;this.second=D;this.millisecond=E||0};w.Time.prototype={getHours:function(){return this.hour},getMinutes:function(){return this.minute},getSeconds:function(){return this.second},getMilliseconds:function(){return this.millisecond},getTime:function(){return((this.hour*60*60)+(this.minute*60)+this.second)*1000+this.millisecond}};var q=function(F,C){if(arguments.length===1){C=F;F=0}var D=[];for(var E=F;E<C;E++){D.push(E)}return D};var j=function(D,E){var C=0,F=[];if(D instanceof Array){for(;C<E;C++){F[C]=[].concat(D)}}else{for(;C<E;C++){F[C]=D}}return F};var a=function(D,C){var E=D%C;return(E*C<0)?E+C:E};var p=function(D,C){return{div:Math.floor(D/C),mod:a(D,C)}};var s=function(C){return(C instanceof Array&&C.length==0)?false:Boolean(C)};var f=function(C,D){return C.indexOf(D)!=-1};var d=[].concat(j(1,31),j(2,28),j(3,31),j(4,30),j(5,31),j(6,30),j(7,31),j(8,31),j(9,30),j(10,31),j(11,30),j(12,31),j(1,7));var z=[].concat(j(1,31),j(2,29),j(3,31),j(4,30),j(5,31),j(6,30),j(7,31),j(8,31),j(9,30),j(10,31),j(11,30),j(12,31),j(1,7));var i=q(1,29),h=q(1,30),y=q(1,31),x=q(1,32);var b=[].concat(x,h,x,y,x,y,x,x,y,x,y,x,x.slice(0,7));var m=[].concat(x,i,x,y,x,y,x,x,y,x,y,x,x.slice(0,7));i=q(-28,0);h=q(-29,0);y=q(-30,0);x=q(-31,0);var r=[].concat(x,h,x,y,x,y,x,x,y,x,y,x,x.slice(0,7));var B=[].concat(x,i,x,y,x,y,x,x,y,x,y,x,x.slice(0,7));var o=[0,31,60,91,121,152,182,213,244,274,305,335,366];var g=[0,31,59,90,120,151,181,212,243,273,304,334,365];var e=(function(){for(var C=[],D=0;D<55;D++){C=C.concat(q(7))}return C}());var v=function(C,D){if(D===0){throw new Error("Can''t create weekday with n == 0")}this.weekday=C;this.n=D};v.prototype={nth:function(C){return this.n==C?this:new v(this.weekday,C)},equals:function(C){return this.weekday==C.weekday&&this.n==C.n},toString:function(){var C=["MO","TU","WE","TH","FR","SA","SU"][this.weekday];if(this.n){C=(this.n>0?"+":"")+String(this.n)+C}return C},getJsWeekday:function(){return this.weekday==6?0:this.weekday+1}};var c=function(C,S){this._string=null;C=C||{};this._cache=S?null:{all:false,before:[],after:[],between:[]};this.origOptions={};var Q=[],J=Object.keys(C),L=Object.keys(c.DEFAULT_OPTIONS);J.forEach(function(V){this.origOptions[V]=C[V];if(!f(L,V)){Q.push(V)}},this);if(Q.length){throw new Error("Invalid options: "+Q.join(", "))}if(!c.FREQUENCIES[C.freq]&&C.byeaster===null){throw new Error("Invalid frequency: "+String(C.freq))}L.forEach(function(V){if(!f(J,V)){C[V]=c.DEFAULT_OPTIONS[V]}});var I=this.options=C;if(I.byeaster!==null){I.freq=c.YEARLY}if(!I.dtstart){I.dtstart=new Date()}var P=I.dtstart.getTime()%1000;if(I.wkst===null){I.wkst=c.MO.weekday}else{if(typeof I.wkst=="number"){}else{I.wkst=I.wkst.weekday}}if(I.bysetpos!==null){if(typeof I.bysetpos=="number"){I.bysetpos=[I.bysetpos]}for(var O=0;O<I.bysetpos.length;O++){var G=I.bysetpos[O];if(G==0||!(-366<=G&&G<=366)){throw new Error("bysetpos must be between 1 and 366, or between -366 and -1")}}}if(!(s(I.byweekno)||s(I.byyearday)||s(I.bymonthday)||I.byweekday!==null||I.byeaster!==null)){switch(I.freq){case c.YEARLY:if(!I.bymonth){I.bymonth=I.dtstart.getMonth()+1}I.bymonthday=I.dtstart.getDate();break;case c.MONTHLY:I.bymonthday=I.dtstart.getDate();break;case c.WEEKLY:I.byweekday=w.getWeekday(I.dtstart);break}}if(I.bymonth!==null&&!(I.bymonth instanceof Array)){I.bymonth=[I.bymonth]}if(I.byyearday!==null&&!(I.byyearday instanceof Array)){I.byyearday=[I.byyearday]}if(I.bymonthday===null){I.bymonthday=[];I.bynmonthday=[]}else{if(I.bymonthday instanceof Array){var R=[],T=[];for(O=0;O<I.bymonthday.length;O++){var G=I.bymonthday[O];if(G>0){R.push(G)}else{if(G<0){T.push(G)}}}I.bymonthday=R;I.bynmonthday=T}else{if(I.bymonthday<0){I.bynmonthday=[I.bymonthday];I.bymonthday=[]}else{I.bynmonthday=[];I.bymonthday=[I.bymonthday]}}}if(I.byweekno!==null&&!(I.byweekno instanceof Array)){I.byweekno=[I.byweekno]}if(I.byweekday===null){I.bynweekday=null}else{if(typeof I.byweekday=="number"){I.byweekday=[I.byweekday];I.bynweekday=null}else{if(I.byweekday instanceof v){if(!I.byweekday.n||I.freq>c.MONTHLY){I.byweekday=[I.byweekday.weekday];I.bynweekday=null}else{I.bynweekday=[[I.byweekday.weekday,I.byweekday.n]];I.byweekday=null}}else{var D=[],F=[];for(O=0;O<I.byweekday.length;O++){var H=I.byweekday[O];if(typeof H=="number"){D.push(H)}else{if(!H.n||I.freq>c.MONTHLY){D.push(H.weekday)}else{F.push([H.weekday,H.n])}}}I.byweekday=s(D)?D:null;I.bynweekday=s(F)?F:null}}}if(I.byhour===null){I.byhour=(I.freq<c.HOURLY)?[I.dtstart.getHours()]:null}else{if(typeof I.byhour=="number"){I.byhour=[I.byhour]}}if(I.byminute===null){I.byminute=(I.freq<c.MINUTELY)?[I.dtstart.getMinutes()]:null}else{if(typeof I.byminute=="number"){I.byminute=[I.byminute]}}if(I.bysecond===null){I.bysecond=(I.freq<c.SECONDLY)?[I.dtstart.getSeconds()]:null}else{if(typeof I.bysecond=="number"){I.bysecond=[I.bysecond]}}if(I.freq>=c.HOURLY){this.timeset=null}else{this.timeset=[];for(O=0;O<I.byhour.length;O++){var E=I.byhour[O];for(var N=0;N<I.byminute.length;N++){var K=I.byminute[N];for(var M=0;M<I.bysecond.length;M++){var U=I.bysecond[M];this.timeset.push(new w.Time(E,K,U,P))}}}w.sort(this.timeset)}};c.FREQUENCIES=["YEARLY","MONTHLY","WEEKLY","DAILY","HOURLY","MINUTELY","SECONDLY"];c.YEARLY=0;c.MONTHLY=1;c.WEEKLY=2;c.DAILY=3;c.HOURLY=4;c.MINUTELY=5;c.SECONDLY=6;c.MO=new v(0);c.TU=new v(1);c.WE=new v(2);c.TH=new v(3);c.FR=new v(4);c.SA=new v(5);c.SU=new v(6);c.DEFAULT_OPTIONS={freq:null,dtstart:null,interval:1,wkst:c.MO,count:null,until:null,bysetpos:null,bymonth:null,bymonthday:null,bynmonthday:null,byyearday:null,byweekno:null,byweekday:null,bynweekday:null,byhour:null,byminute:null,bysecond:null,byeaster:null};c.parseText=function(C,D){return k().parseText(C,D)};c.fromText=function(C,D){return k().fromText(C,D)};c.optionsToString=function(N){var L,M,I,J,H,C=[];M=Object.keys(N);I=Object.keys(c.DEFAULT_OPTIONS);for(var E=0;E<M.length;E++){if(!f(I,M[E])){continue}L=M[E].toUpperCase();J=N[M[E]];H=[];if(J===null||J instanceof Array&&!J.length){continue}switch(L){case"FREQ":J=c.FREQUENCIES[N.freq];break;case"WKST":J=J.toString();break;case"BYWEEKDAY":L="BYDAY";if(!(J instanceof Array)){J=[J]}for(var G,D=0;D<J.length;D++){G=J[D];if(G instanceof v){}else{if(G instanceof Array){G=new v(G[0],G[1])}else{G=new v(G)}}H[D]=G.toString()}J=H;break;case"DTSTART":case"UNTIL":J=w.timeToUntilString(J);break;default:if(J instanceof Array){for(var D=0;D<J.length;D++){H[D]=String(J[D])}J=H}else{J=String(J)}}C.push([L,J])}var K=[];for(var E=0;E<C.length;E++){var F=C[E];K.push(F[0]+"="+F[1].toString())}return K.join(";")};c.prototype={all:function(D){if(D){return this._iter(new A("all",{},D))}else{var C=this._cacheGet("all");if(C===false){C=this._iter(new t("all",{}));this._cacheAdd("all",C)}return C}},between:function(H,F,G,E){var D={before:F,after:H,inc:G};if(E){return this._iter(new A("between",D,E))}else{var C=this._cacheGet("between",D);if(C===false){C=this._iter(new t("between",D));this._cacheAdd("between",C,D)}return C}},before:function(E,F){var D={dt:E,inc:F},C=this._cacheGet("before",D);if(C===false){C=this._iter(new t("before",D));this._cacheAdd("before",C,D)}return C},after:function(E,F){var D={dt:E,inc:F},C=this._cacheGet("after",D);if(C===false){C=this._iter(new t("after",D));this._cacheAdd("after",C,D)}return C},count:function(){return this.all().length},toString:function(){return c.optionsToString(this.origOptions)},toText:function(C,D){return k().toText(this,C,D)},isFullyConvertibleToText:function(){return k().isFullyConvertible(this)},_cacheAdd:function(E,D,C){if(!this._cache){return}if(D){D=(D instanceof Date)?w.clone(D):w.cloneDates(D)}if(E=="all"){this._cache.all=D}else{C._value=D;this._cache[E].push(C)}},_cacheGet:function(I,E){if(!this._cache){return false}var G=false;if(I=="all"){G=this._cache.all}else{loopItems:for(var H,F=0;F<this._cache[I].length;F++){H=this._cache[I][F];for(var D in E){if(E.hasOwnProperty(D)&&String(E[D])!=String(H[D])){continue loopItems}}G=H._value;break}}if(!G&&this._cache.all){var C=new t(I,E);for(var F=0;F<this._cache.all.length;F++){if(!C.accept(this._cache.all[F])){break}}G=C.getValue();this._cacheAdd(I,G,E)}return G instanceof Array?w.cloneDates(G):(G instanceof Date?w.clone(G):G)},clone:function(){return new c(this.origOptions)},_iter:function(P){var aj=this.options.dtstart,U=this.options.dtstart%1000;var G=aj.getFullYear(),C=aj.getMonth()+1,ab=aj.getDate(),Y=aj.getHours(),M=aj.getMinutes(),ag=aj.getSeconds(),L=w.getWeekday(aj),J=w.getYearDay(aj);var ad=this.options.freq,D=this.options.interval,ap=this.options.wkst,Q=this.options.until,aq=this.options.bymonth,ar=this.options.byweekno,V=this.options.byyearday,W=this.options.byweekday,I=this.options.byeaster,aF=this.options.bymonthday,av=this.options.bynmonthday,aC=this.options.bysetpos,aD=this.options.byhour,K=this.options.byminute,ae=this.options.bysecond;var am=new n(this);am.rebuild(G,C);var R={};R[c.YEARLY]=am.ydayset;R[c.MONTHLY]=am.mdayset;R[c.WEEKLY]=am.wdayset;R[c.DAILY]=am.ddayset;R[c.HOURLY]=am.ddayset;R[c.MINUTELY]=am.ddayset;R[c.SECONDLY]=am.ddayset;R=R[ad];var ak;if(ad<c.HOURLY){ak=this.timeset}else{var H={};H[c.HOURLY]=am.htimeset;H[c.MINUTELY]=am.mtimeset;H[c.SECONDLY]=am.stimeset;H=H[ad];if((ad>=c.HOURLY&&s(aD)&&!f(aD,Y))||(ad>=c.MINUTELY&&s(K)&&!f(K,M))||(ad>=c.SECONDLY&&s(ae)&&!f(ae,M))){ak=[]}else{ak=H.call(am,Y,M,ag,U)}}var Z,ai=0,at=this.options.count;var E=0;var az,ay,ax,N,aB,F,au,ah,O,aa,T,an;while(true){au=R.call(am,G,C,ab);O=au[0];aa=au[1];T=au[2];Z=false;for(ay=aa;ay<T;ay++){az=O[ay];if((s(aq)&&!f(aq,am.mmask[az]))||(s(ar)&&!am.wnomask[az])||(s(W)&&!f(W,am.wdaymask[az]))||(s(am.nwdaymask)&&!am.nwdaymask[az])||(I!==null&&!f(am.eastermask,az))||((s(aF)||s(av))&&!f(aF,am.mdaymask[az])&&!f(av,am.nmdaymask[az]))||(s(V)&&((az<am.yearlen&&!f(V,az+1)&&!f(V,-am.yearlen+az))||(az>=am.yearlen&&!f(V,az+1-am.yearlen)&&!f(V,-am.nextyearlen+az-am.yearlen))))){O[az]=null;Z=true}}if(s(aC)&&s(ak)){var S,ao,ac=[];for(az,ay=0;ay<aC.length;ay++){var ah=aC[ay];if(ah<0){S=Math.floor(ah/ak.length);ao=a(ah,ak.length)}else{S=Math.floor((ah-1)/ak.length);ao=a((ah-1),ak.length)}try{au=[];for(ax=aa;ax<T;ax++){var af=O[ax];if(af===null){continue}au.push(af)}if(S<0){az=au.slice(S)[0]}else{az=au[S]}var aw=ak[ao];var aA=w.fromOrdinal(am.yearordinal+az);var al=w.combine(aA,aw);if(!f(ac,al)){ac.push(al)}}catch(aE){}}w.sort(ac);for(ay=0;ay<ac.length;ay++){var al=ac[ay];if(Q&&al>Q){this._len=ai;return P.getValue()}else{if(al>=aj){++ai;if(!P.accept(al)){return P.getValue()}if(at){--at;if(!at){this._len=ai;return P.getValue()}}}}}}else{for(ay=aa;ay<T;ay++){az=O[ay];if(az!==null){var aA=w.fromOrdinal(am.yearordinal+az);for(ax=0;ax<ak.length;ax++){var aw=ak[ax];var al=w.combine(aA,aw);if(Q&&al>Q){this._len=ai;return P.getValue()}else{if(al>=aj){++ai;if(!P.accept(al)){return P.getValue()}if(at){--at;if(!at){this._len=ai;return P.getValue()}}}}}}}}an=false;if(ad==c.YEARLY){G+=D;if(G>w.MAXYEAR){this._len=ai;return P.getValue()}am.rebuild(G,C)}else{if(ad==c.MONTHLY){C+=D;if(C>12){aB=Math.floor(C/12);F=a(C,12);C=F;G+=aB;if(C==0){C=12;--G}if(G>w.MAXYEAR){this._len=ai;return P.getValue()}}am.rebuild(G,C)}else{if(ad==c.WEEKLY){if(ap>L){ab+=-(L+1+(6-ap))+D*7}else{ab+=-(L-ap)+D*7}L=ap;an=true}else{if(ad==c.DAILY){ab+=D;an=true}else{if(ad==c.HOURLY){if(Z){Y+=Math.floor((23-Y)/D)*D}while(true){Y+=D;N=p(Y,24);aB=N.div;F=N.mod;if(aB){Y=F;ab+=aB;an=true}if(!s(aD)||f(aD,Y)){break}}ak=H.call(am,Y,M,ag)}else{if(ad==c.MINUTELY){if(Z){M+=Math.floor((1439-(Y*60+M))/D)*D}while(true){M+=D;N=p(M,60);aB=N.div;F=N.mod;if(aB){M=F;Y+=aB;N=p(Y,24);aB=N.div;F=N.mod;if(aB){Y=F;ab+=aB;an=true;Z=false}}if((!s(aD)||f(aD,Y))&&(!s(K)||f(K,M))){break}}ak=H.call(am,Y,M,ag)}else{if(ad==c.SECONDLY){if(Z){ag+=Math.floor((86399-(Y*3600+M*60+ag))/D)*D}while(true){ag+=D;N=p(ag,60);aB=N.div;F=N.mod;if(aB){ag=F;M+=aB;N=p(M,60);aB=N.div;F=N.mod;if(aB){M=F;Y+=aB;N=p(Y,24);aB=N.div;F=N.mod;if(aB){Y=F;ab+=aB;an=true}}}if((!s(aD)||f(aD,Y))&&(!s(K)||f(K,M))&&(!s(ae)||f(ae,ag))){break}}ak=H.call(am,Y,M,ag)}}}}}}}if(an&&ab>28){var X=w.monthRange(G,C-1)[1];if(ab>X){while(ab>X){ab-=X;++C;if(C==13){C=1;++G;if(G>w.MAXYEAR){this._len=ai;return P.getValue()}}X=w.monthRange(G,C-1)[1]}am.rebuild(G,C)}}}}};c.parseString=function(D){D=D.replace(/^\s+|\s+$/,"");if(!D.length){return null}var F,E,K,J,G,L=D.split(";"),N={};for(F=0;F<L.length;F++){G=L[F].split("=");K=G[0];J=G[1];switch(K){case"FREQ":N.freq=c[J];break;case"WKST":N.wkst=c[J];break;case"COUNT":case"INTERVAL":case"BYSETPOS":case"BYMONTH":case"BYMONTHDAY":case"BYYEARDAY":case"BYWEEKNO":case"BYHOUR":case"BYMINUTE":case"BYSECOND":if(J.indexOf(",")!=-1){J=J.split(",");for(E=0;E<J.length;E++){if(/^[+-]?\d+$/.test(J[E])){J[E]=Number(J[E])}}}else{if(/^[+-]?\d+$/.test(J)){J=Number(J)}}K=K.toLowerCase();N[K]=J;break;case"BYDAY":var C,H,I,M=J.split(",");N.byweekday=[];for(E=0;E<M.length;E++){I=M[E];if(I.length==2){H=c[I];N.byweekday.push(H)}else{I=I.match(/^([+-]?\d)([A-Z]{2})$/);C=Number(I[1]);H=I[2];H=c[H].weekday;N.byweekday.push(new v(H,C))}}break;case"DTSTART":N.dtstart=w.untilStringToDate(J);break;case"UNTIL":N.until=w.untilStringToDate(J);break;case"BYEASTER":N.byeaster=Number(J);break;default:throw new Error("Unknown RRULE property ''"+K+"''")}}return N};c.fromString=function(C){return new c(c.parseString(C))};var n=function(C){this.rrule=C;this.lastyear=null;this.lastmonth=null;this.yearlen=null;this.nextyearlen=null;this.yearordinal=null;this.yearweekday=null;this.mmask=null;this.mrange=null;this.mdaymask=null;this.nmdaymask=null;this.wdaymask=null;this.wnomask=null;this.nwdaymask=null;this.eastermask=null};n.prototype.easter=function(D,C){C=C||0;var T=D%19,R=Math.floor(D/100),P=D%100,O=Math.floor(R/4),M=R%4,L=Math.floor((R+8)/25),K=Math.floor((R-L+1)/3),J=Math.floor(19*T+R-O-K+15)%30,I=Math.floor(P/4),G=P%4,F=Math.floor(32+2*M+2*I-J-G)%7,E=Math.floor((T+11*J+22*F)/451),S=Math.floor((J+F-7*E+114)/31),H=(J+F-7*E+114)%31+1,Q=Date.UTC(D,S-1,H+C),N=Date.UTC(D,0,1);return[Math.ceil((Q-N)/(1000*60*60*24))]};n.prototype.rebuild=function(L,X){var H=this.rrule;if(L!=this.lastyear){this.yearlen=w.isLeapYear(L)?366:365;this.nextyearlen=w.isLeapYear(L+1)?366:365;var O=new Date(L,0,1);this.yearordinal=w.toOrdinal(O);this.yearweekday=w.getWeekday(O);var M=w.getWeekday(new Date(L,0,1));if(this.yearlen==365){this.mmask=[].concat(d);this.mdaymask=[].concat(m);this.nmdaymask=[].concat(B);this.wdaymask=e.slice(M);this.mrange=[].concat(g)}else{this.mmask=[].concat(z);this.mdaymask=[].concat(b);this.nmdaymask=[].concat(r);this.wdaymask=e.slice(M);this.mrange=[].concat(o)}if(!s(H.options.byweekno)){this.wnomask=null}else{this.wnomask=j(0,this.yearlen+7);var V,I,E;V=I=a(7-this.yearweekday+H.options.wkst,7);if(V>=4){V=0;E=this.yearlen+a(this.yearweekday-H.options.wkst,7)}else{E=this.yearlen-V}var N=Math.floor(E/7);var Y=a(E,7);var C=Math.floor(N+(Y/4));for(var P,T,S=0;S<H.options.byweekno.length;S++){P=H.options.byweekno[S];if(P<0){P+=C+1}if(!(0<P&&P<=C)){continue}if(P>1){T=V+(P-1)*7;if(V!=I){T-=7-I}}else{T=V}for(var Q=0;Q<7;Q++){this.wnomask[T]=1;T++;if(this.wdaymask[T]==H.options.wkst){break}}}if(f(H.options.byweekno,1)){var T=V+C*7;if(V!=I){T-=7-I}if(T<this.yearlen){for(var S=0;S<7;S++){this.wnomask[T]=1;T+=1;if(this.wdaymask[T]==H.options.wkst){break}}}}if(V){var K;if(!f(H.options.byweekno,-1)){var R=w.getWeekday(new Date(L-1,0,1));var U=a(7-R+H.options.wkst,7);var D=w.isLeapYear(L-1)?366:365;if(U>=4){U=0;K=Math.floor(52+a(D+a(R-H.options.wkst,7),7)/4)}else{K=Math.floor(52+a(this.yearlen-V,7)/4)}}else{K=-1}if(f(H.options.byweekno,K)){for(var T=0;T<V;T++){this.wnomask[T]=1}}}}}if(s(H.options.bynweekday)&&(X!=this.lastmonth||L!=this.lastyear)){var F=[];if(H.options.freq==c.YEARLY){if(s(H.options.bymonth)){for(S=0;S<H.options.bymonth.length;S++){X=H.options.bymonth[S];F.push(this.mrange.slice(X-1,X+1))}}else{F=[[0,this.yearlen]]}}else{if(H.options.freq==c.MONTHLY){F=[this.mrange.slice(X-1,X+1)]}}if(s(F)){this.nwdaymask=j(0,this.yearlen);for(var S=0;S<F.length;S++){var W=F[S];var G=W[0],J=W[1];J-=1;for(var Q=0;Q<H.options.bynweekday.length;Q++){var M=H.options.bynweekday[Q][0],P=H.options.bynweekday[Q][1];if(P<0){T=J+(P+1)*7;T-=a(this.wdaymask[T]-M,7)}else{T=G+(P-1)*7;T+=a(7-this.wdaymask[T]+M,7)}if(G<=T&&T<=J){this.nwdaymask[T]=1}}}}this.lastyear=L;this.lastmonth=X}if(H.options.byeaster!==null){this.eastermask=this.easter(L,H.options.byeaster)}};n.prototype.ydayset=function(D,E,C){return[q(this.yearlen),0,this.yearlen]};n.prototype.mdayset=function(F,G,D){var I=j(null,this.yearlen);var H=this.mrange[G-1];var C=this.mrange[G];for(var E=H;E<C;E++){I[E]=E}return[I,H,C]};n.prototype.wdayset=function(F,G,C){var I=j(null,this.yearlen+7);var E=w.toOrdinal(new Date(F,G-1,C))-this.yearordinal;var H=E;for(var D=0;D<7;D++){I[E]=E;++E;if(this.wdaymask[E]==this.rrule.options.wkst){break}}return[I,H,E]};n.prototype.ddayset=function(E,F,C){var G=j(null,this.yearlen);var D=w.toOrdinal(new Date(E,F-1,C))-this.yearordinal;G[D]=D;return[G,D,D+1]};n.prototype.htimeset=function(C,I,F,H){var J=[],E=this.rrule;for(var G=0;G<E.options.byminute.length;G++){I=E.options.byminute[G];for(var D=0;D<E.options.bysecond.length;D++){F=E.options.bysecond[D];J.push(new w.Time(C,I,F,H))}}w.sort(J);return J};n.prototype.mtimeset=function(C,H,F,G){var I=[],E=this.rrule;for(var D=0;D<E.options.bysecond.length;D++){F=E.options.bysecond[D];I.push(new w.Time(C,H,F,G))}w.sort(I);return I};n.prototype.stimeset=function(C,F,D,E){return[new w.Time(C,F,D,E)]};var t=function(D,C){this.init(D,C)};t.prototype={init:function(D,C){this.method=D;this.args=C;this._result=[];this.minDate=null;this.maxDate=null;if(D=="between"){this.maxDate=C.inc?C.before:new Date(C.before.getTime()-1);this.minDate=C.inc?C.after:new Date(C.after.getTime()+1)}else{if(D=="before"){this.maxDate=C.inc?C.dt:new Date(C.dt.getTime()-1)}else{if(D=="after"){this.minDate=C.inc?C.dt:new Date(C.dt.getTime()+1)}}}},accept:function(D){var E=this.minDate&&D<this.minDate,C=this.maxDate&&D>this.maxDate;if(this.method=="between"){if(E){return true}if(C){return false}}else{if(this.method=="before"){if(C){return false}}else{if(this.method=="after"){if(E){return true}this.add(D);return false}}}return this.add(D)},add:function(C){this._result.push(C);return true},getValue:function(){switch(this.method){case"all":case"between":return this._result;case"before":case"after":return this._result.length?this._result[this._result.length-1]:null}}};var A=function(F,D,E){var C=["all","between"];if(!f(C,F)){throw new Error(''Invalid method "''+F+''". Only all and between works with iterator.'')}this.add=function(G){if(E(G,this._result.length)){this._result.push(G);return true}return false};this.init(F,D)};A.prototype=t.prototype;if(l){module.exports={RRule:c}}if(typeof ender==="undefined"){u.RRule=c}if(typeof define==="function"&&define.amd){define("rrule",[],function(){return c})}}(this));');
块引用
// this is the function I am attempting to call when the external node module is said to be undefined
CREATE OR REPLACE FUNCTION public.generate_recurrences(recurrence_pattern CHARACTER VARYING, start_date date, end_date date)
RETURNS SETOF TEXT
LANGUAGE plv8
IMMUTABLE STRICT
AS $function$
// parse the RRULE string
var rule = RRule.fromString(recurrence_pattern);
// return all occurrences between start date and end date
var recurrences = rule.between(start_date, end_date);
for(var i = 0; i < recurrences.length; i++) {
plv8.return_next(new Date(recurrences[i]).toISOString());
}
$function$
任何建议将不胜感激!