mongocat
Version:
✨ Mongocat 😺 is easy to use, configuration based Denormalization mongoose plugin for read heavy applications. Mongocat reduces write complexity too.
3 lines (2 loc) • 35.8 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("mongoose");function r(){r=function(){return e};var e={},t=Object.prototype,n=t.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function c(e,r,t){return Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}),e[r]}try{c({},"")}catch(e){c=function(e,r,t){return e[r]=t}}function s(e,r,t,n){var o=Object.create((r&&r.prototype instanceof d?r:d).prototype),a=new P(n||[]);return o._invoke=function(e,r,t){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(t.method=o,t.arg=a;;){var i=t.delegate;if(i){var u=w(i,t);if(u){if(u===f)continue;return u}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var c=l(e,r,t);if("normal"===c.type){if(n=t.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:t.done}}"throw"===c.type&&(n="completed",t.method="throw",t.arg=c.arg)}}}(e,t,a),o}function l(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var f={};function d(){}function p(){}function h(){}var m={};c(m,a,(function(){return this}));var v=Object.getPrototypeOf,b=v&&v(v(E([])));b&&b!==t&&n.call(b,a)&&(m=b);var y=h.prototype=d.prototype=Object.create(m);function g(e){["next","throw","return"].forEach((function(r){c(e,r,(function(e){return this._invoke(r,e)}))}))}function x(e,r){var t;this._invoke=function(o,a){function i(){return new r((function(t,i){!function t(o,a,i,u){var c=l(e[o],e,a);if("throw"!==c.type){var s=c.arg,f=s.value;return f&&"object"==typeof f&&n.call(f,"__await")?r.resolve(f.__await).then((function(e){t("next",e,i,u)}),(function(e){t("throw",e,i,u)})):r.resolve(f).then((function(e){s.value=e,i(s)}),(function(e){return t("throw",e,i,u)}))}u(c.arg)}(o,a,t,i)}))}return t=t?t.then(i,i):i()}}function w(e,r){var t=e.iterator[r.method];if(void 0===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=void 0,w(e,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=l(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,f;var o=n.arg;return o?o.done?(r[e.resultName]=o.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function k(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function O(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function P(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(k,this),this.reset(!0)}function E(e){if(e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,o=function r(){for(;++t<e.length;)if(n.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:j}}function j(){return{value:void 0,done:!0}}return p.prototype=h,c(y,"constructor",h),c(h,"constructor",p),p.displayName=c(h,u,"GeneratorFunction"),e.isGeneratorFunction=function(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,c(e,u,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},g(x.prototype),c(x.prototype,i,(function(){return this})),e.AsyncIterator=x,e.async=function(r,t,n,o,a){void 0===a&&(a=Promise);var i=new x(s(r,t,n,o),a);return e.isGeneratorFunction(t)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},g(y),c(y,u,"Generator"),c(y,a,(function(){return this})),c(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=E,P.prototype={constructor:P,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(O),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return i.type="throw",i.arg=e,r.next=t,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return t("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),c=n.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return t(a.catchLoc,!0);if(this.prev<a.finallyLoc)return t(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return t(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return t(a.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var o=this.tryEntries[t];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,f):this.complete(i)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),f},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),O(t),f}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var o=n.arg;O(t)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:E(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=void 0),f}},e}function t(e,r,t,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void t(e)}u.done?r(c):Promise.resolve(c).then(n,o)}function n(e){return function(){var r=this,n=arguments;return new Promise((function(o,a){var i=e.apply(r,n);function u(e){t(i,o,a,u,c,"next",e)}function c(e){t(i,o,a,u,c,"throw",e)}u(void 0)}))}}function o(){return(o=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var a=function r(t){var n={};for(var o in t)if(t[o]instanceof e.Types.ObjectId||"object"!=typeof t[o]||Array.isArray(t[o]))n[o]=t[o];else{var a=r(t[o]);for(var i in a)n[o+"."+i]=a[i]}return n},i=function(){function e(){}return e.filterObject=function(e,r,t){var n=this;return void 0===t&&(t=Object),Object.entries(e).filter((function(e){return r(e[1],e[0])})).reduce((function(e,a){var i,u=a[1];return o({},e,((i={})[a[0]]=Array.isArray(u)?u:u===t(u)?n.filterObject(u,r):u,i))}),{})},e}(),u=function(e){var r={},t=function(t){var n=t.split(".");n.reduce((function(r,o,a){return r[o]||(r[o]=isNaN(Number(n[a+1]))?n.length-1===a?e[t]:{}:[])}),r)};for(var n in e)t(n);return r},c=function(){function e(){}return e.uniqueCombined=function(e,r,t){return e.map((function(e){return r.filter((function(r){return r[t]!==e[t]}))}))},e}();c.uniq=function(e,r){return e.reduce((function(e,t){return e.includes(t[r])||e.push(t[r]),e}),[])},c.uniqueKeys=function(e,r){return[].concat(new Set(e.map((function(e){var t;return null==(t=e[r])?void 0:t.toString()}))))},c.uniqueValues=function(e,r){return e.filter((function(e,t,n){return n.findIndex((function(t){var n,o;return(null==(n=t[r])?void 0:n.toString())===(null==(o=e[r])?void 0:o.toString())}))===t}))};var s=new(require("events")),l={findByIdAndUpdate:"findById",findOneAndUpdate:"findOne",update:"find",updateOne:"findOne"};exports.consumer=function(t){var o=t||{};return o.toPath=o.toPath||o.fromRef.toLowerCase(),function(t){var a=o.key,i=void 0===a?"_id":a,u=o.fromRef,c=o.toRef;i&&(o.withTimestamp&&t.add({denormalizedAt:{type:Date}}),t.pre(["save"],function(){var t=n(r().mark((function t(n){var o,a,s,l;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(!this){r.next=11;break}return console.log({plugin:"consumer",method:"pre save",provider:u,consumer:c}),a=i,s=e.model(u),r.next=6,s.findOne(((o={})[a]=this[a],o),{createdAt:0,updatedAt:0});case 6:if(l=r.sent){r.next=9;break}return r.abrupt("return",n(new Error("Denormalization failed for key: "+i+", value: "+this[i]+", plugin: consumer ,method: save, consumer: "+c+", provider: "+u)));case 9:Object.assign(this,null==l?void 0:l.toObject()),n();case 11:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()))}},exports.denormalizeEmitter=s,exports.polymorphicArrayConsumer=function(t){var u=t||{};return u.toPath=u.toPath||"contents",u.toPathNestedField=u.toPathNestedField||"content",u.strict=u.strict||!1,function(t){var f,d=u.toRef,p=u.availableProviders,h=u.localKey,m=void 0===h?"modelId":h,v=u.foreignKey,b=void 0===v?"_id":v,y=u.fromRefPath,g=void 0===y?"modelType":y,x=u.toPath,w=u.toPathNestedField,k=u.fixedProvider,O=u.morphSchemaMap,P=u.polymorphic,E=void 0===P||P,j=u.inArray,A=void 0===j||j;if(!(x&&w&&m&&b))return new Error("Configuration invalid: toPath:"+x+",toPathNestedField:"+w+", localKey:"+m+" , foreignKey:"+b+", inArray");if(!t.paths[x]||!(null==(f=t.paths[x])?void 0:f.schema.paths[w])){var S;if(!O){var z="Schema configuration invalid: toPath:"+x+" , localKey:"+m+" , foreignKey:"+b+". either defiine static sub schema or dynamic morphSchemaMap";return console.log(z),new Error(z)}t.paths[x].schema.add(((S={})[w]=e.Schema.Types.Mixed,S))}var C=k&&[k]||p&&p||O&&Object.keys(O)||[];function _(o){var i,c=e.model(d),s=e.model(o);try{i=s.watch()}catch(e){throw i=null,new Error("Change Stream not supported")}i.on("change",function(){var e=n(r().mark((function e(n){var s,l,f,p,h,v,b,y,g,k,P,E,j;return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(console.log("PolymorphicArrayConsumer: provider: "+o+" is calling for consumer:"+u.toRef+" on "+x+" field"),"update"!==n.operationType){e.next=25;break}if(p=(null==(s=n.updateDescription)?void 0:s.updatedFields)||{},h=O&&Object.keys(O[o].paths)||Object.keys(null==(l=t.paths[x])?void 0:l.schema.paths[w].schema.paths)||[],v={},b=a(p),h.forEach((function(e){v[e]=b[e]})),y=null==(f=n.documentKey)?void 0:f._id){e.next=10;break}return e.abrupt("return");case 10:return e.prev=10,(g={})[x+"."+m]=y,P=g,j=a(((k={})[E=x+".$."+w]=v,k)),console.log({filterQuery:P,toUpdate:E,updateBody:j}),e.next=18,c.updateMany(P,j);case 18:e.next=25;break;case 20:return e.prev=20,e.t0=e.catch(10),console.log("Failed to denormalize error by changeStream, Model: "+d+" Field: "+x+" Nested: "+w,e.t0.message),e.next=25,i.close();case 25:case"end":return e.stop()}}),e,null,[[10,20]])})));return function(r){return e.apply(this,arguments)}}()),i.on("error",function(){var e=n(r().mark((function e(t){return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return console.log(t.message||"Change Stream not supported"),e.next=3,i.close();case 3:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}())}C.map((function(a){return s.on("denormalize/publish/"+a,function(){var c=n(r().mark((function n(c){var s,l,f,d,p,h,v,y,g,x,k,P,E;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return f=u.toPath,p=void 0===(d=u.inArray)||d,v=e.model(h=u.toRef),y=null!=c&&c._doc?null==c?void 0:c._doc:c,g=O&&Object.keys(O[a].paths)||Object.keys(null==(s=t.paths[f])?void 0:s.schema.paths[w].schema.paths)||[],console.log({schemaFeilds:g,flattenPublishedData:y}),x=i.filterObject(y,(function(e,r){return g.includes(r)}),(function(r){return!(r instanceof e.Types.ObjectId)&&Object(r)})),(l={})[k=f+"."+m]=c[b],P=l,E=Object.entries(x).reduce((function(e,r){var t,n=r[0];return o({},e,((t={})[p?f+".$."+w+"."+n:f+"."+w+"."+n]=r[1],t))}),{}),console.log({filterKey:k,filterQuery:P,updateFlattenData:E,filteredFlattenedData:x}),r.next=13,v.updateMany(P,E,{new:!0});case 13:console.info("[47m[46m%s[0m","✨ Successfully denormalized via hook polymorphicArrayConsumer event emitted ✨","[1m[4m","toRef: "+h+", from provider: "+a+", toPath: "+f+", in polymorphic Array 🐦");case 14:case"end":return r.stop()}}),n)})));return function(e){return c.apply(this,arguments)}}()),"allProviders registered"})),u.changeStreamEnabled&&C.length&&process.nextTick((function(){var e;console.log(((e={on:"PolymorphicArrayConsumer"})[d]=x,e.allProviders=C,e)),C.map(_)})),t.pre(["save"],function(){var t=n(r().mark((function t(n){var o,a,i,u,s,l,f,h,v,y,O,P,j,S;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if((i=null==(a=this)?void 0:a.toObject())&&E&&A){r.next=4;break}return r.abrupt("return");case 4:if(!k||!p){r.next=6;break}return r.abrupt("return",n(new Error("Conflict in config: fixedProvider & availableProviders both shouldn't avilable field: "+x)));case 6:if(s=(u=k||i[g])&&(this.isNew||this.isModified(x))&&A&&E&&!!i&&!!i[x]&&!(null==(o=i[x])||!o.length),console.log({shouldDenormalize:s,localKey:m,ModelType:u,toPath:x,toRef:d}),!s){r.next=24;break}if(x&&u){r.next=12;break}return r.abrupt("return");case 12:return h=e.model(u),y=c.uniqueKeys(v=i[x],m),O=c.uniqueValues(v,m),(l={})[b]={$in:y},P=l,r.next=19,h.find(P);case 19:return j=r.sent,console.log({ProviderModel:h,uniqueKeys:y,uniqueValues:O}),S=null==(f=i[x])?void 0:f.map((function(e){var r=null==j?void 0:j.find((function(r){var t;return(null==(t=r[b])?void 0:t.toString())===e[m].toString()}));return e[w]=r,e})),this[x]=S,r.abrupt("return",n());case 24:return r.abrupt("return",n());case 25:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()),t.pre(Object.keys(l),function(){var t=n(r().mark((function t(n){var o,a,i,u,s,l,f,d,h,v,y,P;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if((o=this)&&k){r.next=3;break}return r.abrupt("return");case 3:if(o.$denormalize=a=o.options.denormalize||!1,a){r.next=7;break}return r.abrupt("return");case 7:if(i=o.getUpdate(),(u=i)&&u[x]){r.next=11;break}return r.abrupt("return");case 11:if(o.$denormalize=a,["createdAt","updatedAt","__v"].forEach((function(e){delete i[e]})),delete u.$set,delete u.$setOnInsert,s=k||u[g],!k||!p){r.next=19;break}return r.abrupt("return",n(new Error("Conflict in config: fixedProvider & availableProviders both shouldn't avilable field: "+x)));case 19:if(k||!p||p.includes(s)){r.next=21;break}return r.abrupt("return",n(new Error("NOT AVILABLE in list: availableProviders: "+p+", but got: "+s+" field: "+x+", plugin: polymorphicConsumer , method: save")));case 21:if(!p||!O){r.next=23;break}return r.abrupt("return",n(new Error("Conflict in config: morphSchemaMap & availableProviders both shouldn't avilable on path: "+x)));case 23:if(!a){r.next=38;break}if(x&&s){r.next=26;break}return r.abrupt("return");case 26:return console.log({shouldDenormalize:a,localKey:m,doc:u,toPath:x}),d=e.model(s),h=c.uniqueKeys(u[x],m),(l={})[b]={$in:h},v=l,r.next=33,d.find(v);case 33:return y=r.sent,console.log({ProviderModel:d,uniqueKeys:h}),P=null==(f=u[x])?void 0:f.map((function(e){var r=null==y?void 0:y.find((function(r){var t;return(null==(t=r[b])?void 0:t.toString())===e[m].toString()}));return e[w]=r,e})),u[x]=P,r.abrupt("return",n());case 38:return r.abrupt("return",n());case 39:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}())}},exports.polymorphicConsumer=function(t){var c=t||{};return c.toPath=c.toPath||"content",function(t){var f=c.fixedProvider,d=c.availableProviders,p=c.localKey,h=void 0===p?"modelId":p,m=c.foreignKey,v=void 0===m?"_id":m,b=c.fromRefPath,y=void 0===b?"modelType":b,g=c.toPath,x=c.morphSchemaMap,w=c.withTimestamp,k=c.denormWhen,O=c.modelSwitcher,P=c.clean,E=c.strict,j=c.exceptionOnFailure,A=c.as,S=c.inArray,z=void 0!==S&&S,C=c.polymorphic,_=void 0===C||C,R=c.toRef,L=c.morphKey,M=void 0===L?"modelId":L,F=c.fromRef,T=void 0===F?"modelType":F;if(!g||!h||!v)return new Error("Configuration invalid: toPath:"+g+" , localKey:"+h+" , foreignKey:"+v);if(P&&(t.methods.toJSON=function(){var e=this.toObject();return delete e[h],delete e[y],e}),w&&t.add({denormalizedAt:{type:Date}}),!t.paths.hasOwnProperty(g)){var N;if(!x){var K="Schema configuration invalid: toPath:"+g+" , localKey:"+h+" , foreignKey:"+v+". either defiine static sub schema or dynamic morphSchemaMap";return console.log(K),new Error(K)}t.add(((N={})[g]=e.Schema.Types.Mixed,N))}var D=f&&[f]||d&&d||x&&Object.keys(x)||c.modelSwitcher&&Object.values(c.modelSwitcher)||[];function I(i){var u,s=e.model(R),l=e.model(i);try{u=l.watch()}catch(e){throw u=null,new Error("Change Stream not supported")}u.on("change",function(){var e=n(r().mark((function e(n){var l,d,p,m,v,b,w,k,O,P,E,j,S,z;return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(console.log("provider: "+i+" is calling for consumer:"+c.toRef+" on "+g+" field"),"update"!==n.operationType){e.next=24;break}if(v=(null==(l=n.updateDescription)?void 0:l.updatedFields)||{},b=t.paths.hasOwnProperty(g).hasOwnProperty("schema").hasOwnProperty("paths")&&Object.keys(null==(d=t.paths[g])||null==(p=d.schema)?void 0:p.paths)||x&&Object.keys(x[i].paths)||[],w={},k=a(v),b.forEach((function(e){w[e]=k[e]})),A&&Object.entries(A).forEach((function(e){var r=e[1];k[r]&&(w[e[0]]=k[r])})),O=null==(m=n.documentKey)?void 0:m._id){e.next=11;break}return e.abrupt("return");case 11:return e.prev=11,S=o(((P={})[h]=O,P[g]={$exists:!0},P),!f&&((E={})[y]=i,E)),z=a(((j={})[g]=w,j)),console.log({filterQuery:S,updateBody:z}),e.next=17,s.updateMany(S,z);case 17:e.next=24;break;case 19:return e.prev=19,e.t0=e.catch(11),console.log("Failed to denormalize error by changeStream, Model: "+s+" Field: "+g),e.next=24,u.close();case 24:case"end":return e.stop()}}),e,null,[[11,19]])})));return function(r){return e.apply(this,arguments)}}()),u.on("error",function(){var e=n(r().mark((function e(t){return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return console.log(t.message||"Change Stream not supported"),e.next=3,u.close();case 3:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}())}D.map((function(a){return s.on("denormalize/publish/"+a,function(){var u=n(r().mark((function n(u){var s,l,f,d,p,h,m,b,y,g,w,k,O,P;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return d=c.toPath,h=void 0!==(p=c.inArray)&&p,b=e.model(m=c.toRef),console.log({options:c,data:u}),y=null!=u&&u._doc?null==u?void 0:u._doc:u,g=x&&Object.keys(x[a].paths)||Object.keys(null==(s=t.paths[d])||null==(l=s.schema)?void 0:l.paths)||[],console.log({schemaFeilds:g,flattenPublishedData:y}),w=i.filterObject(y,(function(e,r){return g.includes(r)}),(function(r){return!(r instanceof e.Types.ObjectId)&&Object(r)})),(f={})[k=d+"."+v]=u[v],O=f,P=Object.entries(w).reduce((function(e,r){var t,n=r[0];return o({},e,((t={})[h?d+".$."+n:d+"."+n]=r[1],t))}),{}),console.log({filterKey:k,filterQuery:O,updateFlattenData:P,filteredFlattenedData:w}),r.next=14,b.updateMany(O,P,{new:!0});case 14:console.info("[47m[46m%s[0m","✨ Successfully denormalized via hook polymorphicConsumer event emitted ✨","[1m[4m","toRef: "+m+", from provider: "+a+", toPath: "+d+", inObject 🐦");case 15:case"end":return r.stop()}}),n)})));return function(e){return u.apply(this,arguments)}}()),"allProviders registered"})),c.changeStreamEnabled&&D.length&&process.nextTick((function(){var e;console.log(((e={})[R]=g,e.allProviders=D,e)),D.map(I)})),t.pre(["save"],function(){var t=n(r().mark((function t(n){var i,c,s,l,p,m,b,P,S,z,C,_,L;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(i=this,c=f||i[T]||i[y],s=i[M]||i[h],g&&s&&c){r.next=5;break}return r.abrupt("return");case 5:if(!f||!d){r.next=7;break}return r.abrupt("return",n(new Error("Conflict in config: fixedProvider & availableProviders both shouldn't avilable field: "+g)));case 7:if(f||!d||d.includes(c)){r.next=9;break}return r.abrupt("return",n(new Error("NOT AVILABLE in list: availableProviders: "+d+", but got: "+c+" field: "+g+", plugin: polymorphicConsumer , method: save")));case 9:if(!d||!x){r.next=11;break}return r.abrupt("return",n(new Error("Conflict in config: morphSchemaMap & availableProviders both shouldn't avilable on path: "+g)));case 11:if(!k||!i[k.field]){r.next=20;break}if(!k.is||k.is===i[k.field]){r.next=14;break}return r.abrupt("return");case 14:if(!k.ne||k.ne!==i[k.field]){r.next=16;break}return r.abrupt("return");case 16:if(!k.in||k.in.includes(i[k.field])){r.next=18;break}return r.abrupt("return");case 18:if(!k.nin||!k.nin.includes(i[k.field])){r.next=20;break}return r.abrupt("return");case 20:if(l=s&&c&&(this.isNew||this.isModified(h)||this.isModified(y)),console.log({shouldDenormalize:l,localKey:h,LocalValue:s,ModelType:c,fixedProvider:f,toPath:g,toRef:R}),!l){r.next=56;break}if(!O||!O[c]){r.next=27;break}m=e.model(O[c]),r.next=34;break;case 27:if(!x||x[c]){r.next=31;break}return r.abrupt("return",n(new Error(c+" is not available on morphSchemaMap's schema.Please define schema for this modelType")));case 31:if(!d||d.includes(c)){r.next=33;break}return r.abrupt("return",n(new Error(c+" is not available on morphSchemaMap's schema.Please define schema for this modelType")));case 33:m=e.model(c);case 34:return r.next=36,m.findOne(((p={})[v]=s,p),{createdAt:0,updatedAt:0});case 36:if(b=r.sent){r.next=43;break}if(!E&&!j){r.next=42;break}return r.abrupt("return",n(new Error("Denormalization failed for foreignKey:"+v+", value: "+s+" field: "+g+", plugin: polymorphicConsumer , method: save")));case 42:return r.abrupt("return",n());case 43:if(P={},x?(S=Object.keys(x[c].paths),z={},C=a(b.toObject()),S.forEach((function(e){z[e]=C[e]})),A&&Object.entries(A).forEach((function(e){var r=e[1];C[r]&&(z[e[0]]=C[r])})),P=u(z)):P=b.toObject(),!A){r.next=53;break}return _=a(b.toObject()),L={},Object.entries(A).forEach((function(e){var r=e[1];_[r]&&(L[e[0]]=_[r])})),L=u(o({},_,L)),this[g]=L,w&&(this.denormalizedAt=(new Date).toISOString()),r.abrupt("return",n());case 53:return i[g]=P,w&&(i.denormalizedAt=(new Date).toISOString()),r.abrupt("return",n());case 56:return r.abrupt("return",n());case 57:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()),t.pre(Object.keys(l),function(){var t=n(r().mark((function t(n){var i,c,s,l,p,m,b,P,S,C,R,L,F,N,K;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(i=this){r.next=3;break}return r.abrupt("return");case 3:if(i.$denormalize=c=i.options.denormalize||!1,c){r.next=7;break}return r.abrupt("return");case 7:if(s=i.getUpdate(),l=s){r.next=11;break}return r.abrupt("return");case 11:if(i.$denormalize=c,["createdAt","updatedAt","__v"].forEach((function(e){delete s[e]})),delete l.$set,delete l.$setOnInsert,console.log("PLUGIN: polymorphicConsumer , op: "+i.op+" doc ",l),p=f||l[T]||l[y],m=l[M]||l[h],g&&m&&p){r.next=21;break}return r.abrupt("return");case 21:if(!f||!d){r.next=23;break}return r.abrupt("return",n(new Error("Conflict in config: fixedProvider & availableProviders both shouldn't avilable field: "+g)));case 23:if(f||!d||d.includes(p)){r.next=25;break}return r.abrupt("return",n(new Error("NOT AVILABLE in list: availableProviders: "+d+", but got: "+p+" field: "+g+", plugin: polymorphicConsumer , method: save")));case 25:if(!d||!x){r.next=27;break}return r.abrupt("return",n(new Error("Conflict in config: morphSchemaMap & availableProviders both shouldn't avilable on path: "+g)));case 27:if(!k||!l[k.field]){r.next=36;break}if(!k.is||k.is===l[k.field]){r.next=30;break}return r.abrupt("return");case 30:if(!k.ne||k.ne!==l[k.field]){r.next=32;break}return r.abrupt("return");case 32:if(!k.in||k.in.includes(l[k.field])){r.next=34;break}return r.abrupt("return");case 34:if(!k.nin||!k.nin.includes(l[k.field])){r.next=36;break}return r.abrupt("return");case 36:if(p){r.next=38;break}return r.abrupt("return");case 38:if(z||!_||!c||!l||!m&&!p){r.next=72;break}if(!O||!O[p]){r.next=43;break}P=e.model(O[p]),r.next=50;break;case 43:if(!x||x[p]){r.next=47;break}return r.abrupt("return",n(new Error(p+" is not available on morphSchemaMap's schema.Please define schema for this modelType")));case 47:if(!d||d.includes(p)){r.next=49;break}return r.abrupt("return",n(new Error(p+" is not available on morphSchemaMap's schema.Please define schema for this modelType")));case 49:P=e.model(p);case 50:return r.next=52,P.findOne(((b={})[v]=m,b),{createdAt:0,updatedAt:0});case 52:if(S=r.sent){r.next=59;break}if(!E&&!j){r.next=58;break}return r.abrupt("return",n(new Error("Denormalization failed for foreignKey:"+v+", value: "+m+" field: "+g+", plugin: polymorphicConsumer , method: "+i.op)));case 58:return r.abrupt("return",n());case 59:if(C={},x?(R=Object.keys(x[p].paths),L={},F=a(S.toObject()),R.forEach((function(e){L[e]=F[e]})),A&&Object.entries(A).forEach((function(e){var r=e[1];F[r]&&(L[e[0]]=F[r])})),C=u(L)):C=S.toObject(),!A){r.next=69;break}return N=a(S.toObject()),K={},Object.entries(A).forEach((function(e){var r=e[1];N[r]&&(K[e[0]]=N[r])})),K=u(o({},N,K)),l[g]=K,w&&(l.denormalizedAt=(new Date).toISOString()),r.abrupt("return",n());case 69:return l[g]=C,w&&(l.denormalizedAt=(new Date).toISOString()),r.abrupt("return",n());case 72:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}())}},exports.provider=function(e){var t=e||{};return t.keyFields=t.keyFields||["_id"],t.ignoredFields=[].concat(t.ignoredFields||[],["createdAt","updatedAt","__v"]),function(e){var a=t.toRef,i=t.ignoredFields,u=t.keyFields;e.post(["save"],function(){var e=n(r().mark((function e(t,n){return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.$__.inserting||s.emit("denormalize/publish/"+(a||this.constructor.modelName),t._doc),e.abrupt("return",n());case 2:case"end":return e.stop()}}),e,this)})));return function(r,t){return e.apply(this,arguments)}}()),e.post(Object.keys(l),n(r().mark((function e(){var t,n,a,c,f,d,p,h,m,v,b,y;return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=(t=this).model.modelName,a=this.getQuery(),c=this.getUpdate(),f=c.$set||{},0!==(d=Object.keys(f).filter((function(e){return!(null!=i&&i.includes(e))}))).length){e.next=8;break}return e.abrupt("return");case 8:for(h in p={},f)d.includes(h)&&(p[h]=f[h]);if(m=Object.keys(a).filter((function(e){return!Array.isArray(u)||u.includes(e)})),v=[a],m.length===(null==u?void 0:u.length)){e.next=20;break}return b=t.op,e.next=16,t.model[l[b]](a);case 16:if((y=e.sent)&&0!==y.length){e.next=19;break}throw new Error("Document not found");case 19:v=Array.isArray(y)?y:[y];case 20:v.forEach((function(e){var r=u&&u.reduce((function(r,t){var n;return o({},r,((n={})[t]=e[t],n))}),{}),a=o({},p,r),i=t.options.denormalize||!1;t.$denormalize=i,i&&(console.log("publish these fields",a),s.emit("denormalize/publish/"+n,a))}));case 21:case"end":return e.stop()}}),e,this)}))))}},exports.updateQueryMethods=l,exports.watchConsumer=function(t){var u=t||{};return u.toPath=u.toPath||u.fromRef.toLowerCase(),function(t){var c=u.toPath,f=u.key,d=void 0===f?"_id":f,p=u.fromRef,h=u.exceptionOnFailure,m=void 0!==h&&h,v=u.toRef,b=u.inArray,y=void 0!==b&&b,g=u.strict,x=void 0!==g&&g;c&&(s.on("denormalize/publish/"+p,function(){var a=n(r().mark((function n(a){var s,l,f,h,m,b,g;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return l=e.model(u.toRef),f=null!=a&&a._doc?null==a?void 0:a._doc:a,h=Object.keys(t.paths),console.log({flattenPublishedData:f}),m=i.filterObject(f,(function(e,r){return h.includes(r)}),(function(r){return!(r instanceof e.Types.ObjectId)&&Object(r)})),(s={})[c+"."+d]=a[d],b=s,g=Object.entries(m).reduce((function(e,r){var t,n=r[0];return o({},e,((t={})[y?c+".$."+n:c+"."+n]=r[1],t))}),{}),r.next=11,l.updateMany(b,g,{new:!0});case 11:console.info("[47m[46m%s[0m","✨ Successfully denormalized via hook watchConsumer event emitted ✨","[1m[4m","toRef: "+v+", from provider: "+p+", toPath: "+c+", inArray: "+y+" 🐦");case 12:case"end":return r.stop()}}),n)})));return function(e){return a.apply(this,arguments)}}()),p&&u.changeStreamEnabled&&(console.log("CHANGE STREAM: "+u.toRef+" > "+c+" << provider: "+u.fromRef),process.nextTick((function(){var o,i=e.model(u.toRef),s=e.model(u.fromRef);try{o=s.watch()}catch(e){throw o=null,new Error("Change Stream not supported")}o&&(o.on("change",function(){var e=n(r().mark((function e(n){var s,l,f,p,h,m,v,b,y,g,x,w,k,O,P,E,j,A,S,z,C,_,R,L;return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(console.log(u.fromRef+" is called for "+u.toRef),"update"!==n.operationType){e.next=49;break}if(l=(null==(s=n.updateDescription)?void 0:s.updatedFields)||{},!u.inArray){e.next=27;break}if(m=Object.keys(null==(f=t.paths[c])||null==(p=f.schema)?void 0:p.paths),v={},b=a(l),m.forEach((function(e){v[e]=b[e]})),y=null==(h=n.documentKey)?void 0:h._id){e.next=11;break}return e.abrupt("return");case 11:return e.prev=11,(g={})[c+"."+d]=y,w=g,k=a(((x={})[c+".$"]=v,x)),console.log(w,k),e.next=17,i.updateMany(w,k);case 17:console.info("[47m[46m%s[0m","✨ Successfully denormalized from changeStream ✨","toRef: "+u.toRef+", from provider: "+u.fromRef+", toPath: "+u.toPath+" 🐦"),e.next=25;break;case 20:return e.prev=20,e.t0=e.catch(11),console.log("Failed to denormalize error by changeStream on mongoose, Model:"+u.toRef+",Field: "+u.toPath,e.t0.message),e.next=25,o.close();case 25:e.next=49;break;case 27:if(u.inArray){e.next=49;break}if(j=Object.keys(null==(O=t.paths[c])||null==(P=O.schema)?void 0:P.paths),A={},S=a(l),j.forEach((function(e){A[e]=S[e]})),z=null==(E=n.documentKey)?void 0:E._id){e.next=35;break}return e.abrupt("return");case 35:return e.prev=35,(C={})[c+"."+d]=z,R=C,L=a(((_={})[c]=A,_)),console.log(R,L),e.next=41,i.updateMany(R,L);case 41:console.info("[47m[46m%s[0m","✨ Successfully denormalized from changeStream✨","toRef: "+u.toRef+", from provider: "+u.fromRef+", toPath: "+u.toPath+" 🐦"),e.next=49;break;case 44:return e.prev=44,e.t1=e.catch(35),console.log("Failed to denormalize error by changeStream on mongoose, Model:"+u.toRef+",Field: "+u.toPath,e.t1.message),e.next=49,o.close();case 49:case"end":return e.stop()}}),e,null,[[11,20],[35,44]])})));return function(r){return e.apply(this,arguments)}}()),o.on("error",n(r().mark((function e(){return r().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.close();case 2:console.log("Change Stream not supported");case 3:case"end":return e.stop()}}),e)})))))}))),t.pre(["save"],function(){var t=n(r().mark((function t(n){var o,a,i,u,s,l,f,h,b,g,w,k;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(o=this,!this.isNew&&!this.isModified(c)||!this[c]){r.next=43;break}if(a={plugin:"watchConsumer",method:"pre save",provider:p,consumer:v,inArray:y?"array":"object",doc:o,isNew:this.isNew?"creating":"updating",modification:c+" "+(this.isModified(c)?"is ":"is not")+" modified"},console.log(a),y||!this[c]||!this[c][d]){r.next=20;break}return u=e.model(p),(i={})[d]=this[c][d],s=i,r.next=9,u.findOne(s,{createdAt:0,updatedAt:0});case 9:if(l=r.sent){r.next=16;break}if(!x&&!m){r.next=15;break}return r.abrupt("return",n(new Error("Denormalization failed for key: "+d+", value: "+this[c][d]+", plugin: watchConsumer, method: save, consumer: "+c+", provider: "+p)));case 15:return r.abrupt("return",n());case 16:return Object.assign(this[c],null==l?void 0:l.toObject()),r.abrupt("return",n());case 20:if(!y||!this[c]){r.next=42;break}if(g=null==(f=o[c])||null==(h=f.filter((function(e){return e[d]})))?void 0:h.map((function(e){return e[d]})),this[c]&&g&&g.length){r.next=29;break}if(delete this[c],!x&&!m){r.next=28;break}return r.abrupt("return",n(new Error("Denormalization failed for feild: "+c+", array: "+g+", plugin: watchConsumer, method: save, consumer: "+v+", provider: "+p)));case 28:return r.abrupt("return",n());case 29:return console.log("plugin: watchConsumer , method: update on: inArray "),w=e.model(p),r.next=33,w.find(((b={})[d]={$in:g},b));case 33:if((k=r.sent).length){r.next=40;break}if(!x&&!m){r.next=39;break}return r.abrupt("return",n(new Error("Denormalization failed for feild: "+c+", array: "+g+", plugin: watchConsumer, method: save, consumer: "+v+", provider: "+p)));case 39:return r.abrupt("return",n());case 40:return Object.assign(this[c],k),r.abrupt("return");case 42:return r.abrupt("return",n());case 43:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()),t.pre(Object.keys(l),function(){var t=n(r().mark((function t(n){var o,a,i,u,s,l,f,h,b,g,w,k,O,P,E;return r().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(o=this){r.next=3;break}return r.abrupt("return");case 3:if(o.$denormalize=a=!!o.options.denormalize||o.options.denormalize,a){r.next=7;break}return r.abrupt("return");case 7:if(i=o.getUpdate(),u=i){r.next=11;break}return r.abrupt("return");case 11:if(o.$denormalize=a,["createdAt","updatedAt","__v"].forEach((function(e){delete i[e]})),delete u.$set,delete u.$setOnInsert,y||!a||!u||!u[c]){r.next=37;break}if(l=u[s=c+"."+d]?u[s]:u[c][d],!u[c]||!u[c][d]){r.next=34;break}return console.log({plugin:"watchConsumer",method:"pre findOneAndUpdate",provider:p,consumer:v,inArray:y,toPath:c,key:d,doc:u}),h=e.model(p),r.next=24,h.findOne(((f={})[d]=l,f));case 24:if(b=r.sent){r.next=31;break}if(!x&&!m){r.next=30;break}return r.abrupt("return",n(new Error("Denormalization failed for toPath:"+c+", value:"+l+" plugin: watchConsumer, method: findOneAndUpdate on: !inArray")));case 30:return r.abrupt("return",n());case 31:return console.log("[47m[46m%s[0m","Denormalizing on feild: "+c+" , queryKey: "+s+" bodyValue: "+l),u[c]=null==b?void 0:b.toObject(),r.abrupt("return",n());case 34:return r.abrupt("return",n());case 37:if(!(y&&a&&u&&u[c])){r.next=59;break}if(console.log({plugin:"watchConsumer",method:"pre findOneAndUpdate",provider:p,consumer:v,toPath:c}),console.log("plugin: watchConsumer, method: findOneAndUpdate TYPE: PRE HOOK on: inArray path: "+c+" data: "+i[c]),console.log({updateParams:i,"updateParams[toPath]":i[c]}),(k=i[c]||[])&&k.length){r.next=44;break}return r.abrupt("return",n());case 44:if((O=null==k||null==(g=k.filter((function(e){return e[d]})))?void 0:g.map((function(e){return e[d]}))).length){r.next=47;break}return r.abrupt("return",n(new Error("Denormalization failed on watchConsumer inArray portion for "+d+" array: "+O+". WRONG KEY INPUT FROM FRONTEND")));case 47:return P=e.model(p),r.next=50,P.find(((w={})[d]={$in:O},w));case 50:if((E=r.sent)&&E.length){r.next=57;break}if(!x&&!m){r.next=56;break}return r.abrupt("return",n(new Error("Denormalization failed for feild: "+c+", array: "+O+", plugin: watchConsumer, method: findOneAndUpdate, provider: "+p)));case 56:return r.abrupt("return",n());case 57:return u[c]=E,r.abrupt("return",n());case 59:return r.abrupt("return",n());case 60:case"end":return r.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()))}};
//# sourceMappingURL=mongocat.cjs.production.min.js.map