@pebula/metap
Version:
meta-programming tools
2 lines • 11.6 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@pebula/metap/internal"),require("@pebula/utils")):"function"==typeof define&&define.amd?define("@pebula/metap",["exports","@pebula/metap/internal","@pebula/utils"],t):t(((e=e||self).pebula=e.pebula||{},e.pebula.metap={}),e.pebula.metap.internal,e.pebula.utils)}(this,function(e,t,r){"use strict";var n=function(e,t){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function i(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function o(e,t,r,n){var i,o=arguments.length,a=o<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,n);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(a=(o<3?i(a):o>3?i(t,r,a):i(t,r))||a);return o>3&&a&&Object.defineProperty(t,r,a),a}function a(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function c(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}}function s(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}var u=t.MetaClass.decorator(t.PropMetadata,!0),l=t.MetaClass.decorator(t.ExcludeMetadata,!0),f=t.MetaClass.decorator(t.RelationMetadata,!0),p=t.MetaClass.decorator(t.TypeMetadata);function g(e){return function(n){var i=t.MetaClass.create(t.ModelMetadata,e,n);!function(e,n,i){var o,a;try{for(var s=c(r.getProtoChain(e)),u=s.next();!u.done;u=s.next()){var l=u.value;e!==l&&t.targetStore.hasTarget(l)&&t.targetStore.extend(l,e)}}catch(e){o={error:e}}finally{try{u&&!u.done&&(a=s.return)&&a.call(s)}finally{if(o)throw o.error}}i&&n.build()}(n,i,!0!==i.skip)}}function y(e,t){return t&&t.transform&&t.transform.incoming?t.transform.incoming(e):e}function d(e){return e.name===this}var h=function(){function e(e,t,r,n){this.target=e,this.compiled=t,this.predicate=r,this.renamer=n}return e.prototype.forEach=function(e,r){for(var n=e.length,i=this.compiled.instructions.slice(),o=this.compiled.excluded.slice(),a=0;a<n;a++){var c=t.array.findRemove(i,this.predicate,e[a])||{cls:e[a],obj:e[a],exclude:t.array.findRemove(o,d,e[a])};c.exclude||(!c.prop&&this.renamer&&this.renamer(c),r(c))}},e.prototype.forEachRaw=function(e,r){for(var n=e.length,i=this.compiled.instructions.slice(),o=this.compiled.excluded.slice(),a=0;a<n;a++){!(c=t.array.findRemove(i,this.predicate,e[a])||{cls:e[a],obj:e[a],exclude:t.array.findRemove(o,d,e[a])}).prop&&this.renamer&&this.renamer(c),r(c)}n=i.length;for(a=0;a<n;a++){var c;!(c=i[a]).prop&&this.renamer&&this.renamer(c),r(c)}},e}();var m=function(){function e(e,t){this.target=e,this.compiled=t}return e.prototype.forEach=function(e,t){for(var r=this.compiled.instructions,n=0,i=r.length;n<i;n++)!r[n].exclude&&t(r[n])},e.prototype.forEachRaw=function(e,t){for(var r=this.compiled.instructions,n=0,i=r.length;n<i;n++)t(r[n])},e}();var v=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.coll_obj=function(e){return new t(e?"Expected a collection but got an object":"Expected an object but got a collection")},t}(Error);function b(e,t){return t&&r.isFunction(t[e])}function j(e,r){var n=e.getValues(t.ExcludeMetadata).filter(function(e){return!e.from||e.from===r}),i=e.model();"exclusive"===i.transformStrategy&&(r="outgoing");var o=b(r,i.transformNameStrategy),a=new Map,c=e.getValues(t.PropMetadata).map(function(e){var c={cls:e.name,obj:e.alias[r],exclude:t.array.findRemove(n,function(t){return t.name===e.name}),prop:e};if(!c.exclude&&o&&c.cls===c.obj&&(c.obj=i.transformNameStrategy[r](c.cls)),e.relation)(s=a.get(e)||[])[0]=c,a.set(e,s);else if(e.foreignKeyOf){var s;(s=a.get(e.foreignKeyOf)||[])[1]=c,a.set(e.foreignKeyOf,s)}return c});return Array.from(a.entries()).forEach(function(e){var t=s(e,2),r=t[0],n=t[1];2===n.length&&(n[0].obj=n[1].cls,n[1].cls=r.name)}),{excluded:n,instructions:c}}function M(e){return e.cls===this}function z(e){return e.obj===this}var S=function(){var e=this.meta.getIdentityKey();if(e)return(this.hasOwnProperty("incoming")?this.incoming:this.outgoing).instructions.find(function(t){return t.prop.name===e})},x=function(){return j(this.meta,"incoming")},O=function(){return j(this.meta,"outgoing")},C=function(){var e=this.meta.model();if("exclusive"===e.transformStrategy)return new m(this.meta.target,this.incoming);var t=b("incoming",e.transformNameStrategy)?function(t){return t.cls=e.transformNameStrategy.incoming(t.obj)}:void 0;return new h(this.meta.target,this.incoming,z,t)},w=function(){var e=this.meta.model();if("exclusive"===e.transformStrategy)return new m(this.meta.target,this.outgoing);var t=b("outgoing",e.transformNameStrategy)?function(t){return t.obj=e.transformNameStrategy.outgoing(t.cls)}:void 0;return new h(this.meta.target,this.outgoing,M,t)},I=function(){function e(e){this.meta=e}return e.prototype.serialize=function(e){return e.serialize(this.outgoingContainer)},e.prototype.deserialize=function(e,t){var n=function(r){var n=r.prop&&r.prop.foreignKeyOf||r.prop;t[r.cls]=y(e.getValue(r.obj,n),n)};if(r.isFunction(e.setRef)&&e.setRef(t),!0===e.raw?this.incomingContainer.forEachRaw(e.getKeys(),n):this.incomingContainer.forEach(e.getKeys(),n),r.isFunction(e.getIdentity)&&this.identity){var i=y(e.getIdentity(),this.identity.prop);i&&(t[this.identity.cls]=i)}},o([t.LazyInit(S),a("design:type",Object)],e.prototype,"identity",void 0),o([t.LazyInit(x),a("design:type",Object)],e.prototype,"incoming",void 0),o([t.LazyInit(O),a("design:type",Object)],e.prototype,"outgoing",void 0),o([t.LazyInit(C),a("design:type",Object)],e.prototype,"incomingContainer",void 0),o([t.LazyInit(w),a("design:type",Object)],e.prototype,"outgoingContainer",void 0),e}();var P=new Map;function R(e){var t=P.get(e);return t||(t=new I(e),P.set(e,t)),t}function T(e,t){return R(e).serialize(t)}function E(e,n,i,o){if(void 0===o&&(o=!1),n.isCollection){if(!Array.isArray(i))throw v.coll_obj(!0);for(var a=i.splice(0,i.length),c=t.targetStore.getIdentityKey(e.target,"incoming"),s=function(){var s=void 0;if(a.length>0&&r.isFunction(n.getIdentity)){var u=n.getIdentity();s=t.array.findRemove(a,function(e){return e[c]===u})}s||(s=o?{}:e.model().factory(!1)),R(e).deserialize(n,s),i.push(s)};n.next();)s()}else{if(Array.isArray(i))throw v.coll_obj(!1);R(e).deserialize(n,i)}}var K=function(){function e(){}return e=o([g({resName:"InternalPlainObject"})],e)}();function _(e,r,n){var i;if(e instanceof t.BaseSerializer){if(i=t.targetStore.getTargetMeta(r))return T(i,e)}else if(i=t.targetStore.getTargetMeta(n||r.constructor))return T(i,e.serializer(r))}function A(e,r){var n=t.targetStore.getTargetMeta(e.constructor);return _(n&&n.hasModel&&n.model().mapper||r||k,e)}function B(e,r,n,i){var o;if(e instanceof t.BaseDeserializer?(i=r,o=e):o=e.deserializer(r,n),t.targetStore.hasTarget(o.sourceType)){var a=t.targetStore.getTargetMeta(o.sourceType),c=i||a.model().factory(o.isCollection);return E(a,o,c),c}return E(a=t.targetStore.getTargetMeta(K),o,c=i||o.isCollection?[]:{},!0),c}function D(e,r,n,i){void 0===n&&(n=null);var o=t.targetStore.getTargetMeta(r);return B(o&&o.hasModel&&o.model().mapper||i||k,e,r,n)}var N=function(e){function n(r,n,i){var o=e.call(this,r,n,i)||this;return o.idx=-1,o instanceof L||(o.existing=new t.DualKeyMap),o.identity=t.targetStore.getIdentityKey(o.sourceType,"outgoing"),o.isCollection=Array.isArray(r),o.isCollection||(o.current=o.source),o}return i(n,e),n.prototype.setRef=function(e){this.current&&this.existing.set(this.sourceType,this.getIdentity(),e)},Object.defineProperty(n.prototype,"ref",{get:function(){if(this.current)return this.existing.get(this.sourceType,this.getIdentity())},enumerable:!0,configurable:!0}),n.prototype.getIdentity=function(){return this.current[this.identity]},n.prototype.next=function(){return!!this.isCollection&&(this.current=this.source[++this.idx],!!this.current)},n.prototype.getKeys=function(){return Object.keys(this.current)},n.prototype.getValue=function(e,r){var n=this.current[e];if(r){var i=this.getRelationQuery(r,n);if(i&&(n=i),t.targetStore.hasTarget(r.type.ref))return this.getCache(r.type.ref,n)||this.deserialize(n,r)}return"object"==typeof n?this.plainMapper.deserialize(n):n},n.prototype.deserialize=function(e,t){return B(this.ref?new L(e,t.type.ref,this.existing,this.plainMapper):k.deserializer(e,t.type.ref,this.plainMapper))},n.prototype.getRelationQuery=function(e,n){var i;if(e.relation&&r.isPrimitive(n))return(i={})[t.targetStore.getIdentityKey(e.type.ref,"outgoing")]=n,i},n.prototype.getCache=function(e,r){var n=t.targetStore.getIdentityKey(e,"outgoing"),i=n&&r[n];if(i)return this.existing.get(e,i)},n}(t.BaseDeserializer);var L=function(e){function t(t,r,n,i){var o=e.call(this,t,r,i)||this;return o.existing=n,o}return i(t,e),t}(N);var F=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return i(r,e),r.prototype.serialize=function(e){return this.cache||(this.cache=new Map),Array.isArray(this.source)?this.serializeCollection(this.source,e):this.serializeObject(this.source,e)},r.prototype.serializeObject=function(e,r){var n=this,i={};r.forEach(Object.keys(e),function(r){var o,a,c=r.prop;if(c&&t.targetStore.hasTarget(c.type.ref)){var s=c.type.ref;if(c.relation&&!c.type.container){var u=t.targetStore.getIdentityKey(s);i[r.obj]=e[r.cls][u]}else i[r.obj]=_(new V(e[r.cls],n.cache,n.plainMapper),s)}else{var l=n.plainMapper.serialize((o=e[r.cls],(a=c)&&a.transform&&a.transform.outgoing?a.transform.outgoing(o):o));i[r.obj]=l}});var o=t.targetStore.getIdentityKey(r.target);return o!==t.targetStore.getIdentityKey(r.target,"outgoing")&&delete i[o],i},r.prototype.serializeCollection=function(e,t){var r=this;return e.map(function(e){return r.serializeObject(e,t)})},r}(t.BaseSerializer);var V=function(e){function t(t,r,n){var i=e.call(this,t,n)||this;return i.cache=r,i}return i(t,e),t}(F);var k={serializer:function(e,t){return new F(e,t)},deserializer:function(e,t,r){return new N(e,t,r)}};Object.defineProperty(e,"BaseDeserializer",{enumerable:!0,get:function(){return t.BaseDeserializer}}),Object.defineProperty(e,"BaseSerializer",{enumerable:!0,get:function(){return t.BaseSerializer}}),Object.defineProperty(e,"Errors",{enumerable:!0,get:function(){return t.Errors}}),Object.defineProperty(e,"ModelClassBase",{enumerable:!0,get:function(){return t.ModelClassBase}}),Object.defineProperty(e,"ModelClassCollection",{enumerable:!0,get:function(){return t.ModelClassCollection}}),Object.defineProperty(e,"PlainObjectMapper",{enumerable:!0,get:function(){return t.PlainObjectMapper}}),Object.defineProperty(e,"errors",{enumerable:!0,get:function(){return t.errors}}),e.DirectDeserializeMapper=N,e.DirectSerializeMapper=F,e.Exclude=l,e.Identity=function(){return function(e,r){t.targetStore.getTargetMeta(e.constructor).model().identity=r}},e.Model=g,e.Prop=u,e.Relation=f,e.Type=p,e.autoDeserialize=D,e.autoSerialize=A,e.clone=function(e,t){return D(A(e,t),e.constructor,null,t)},e.deserialize=B,e.deserializeTargetMeta=E,e.directMapper=k,e.serialize=_,e.serializeTargetMeta=T,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=pebula-metap.umd.min.js.map