Comparing version 2.0.0-beta.3 to 2.0.0-beta.4
import { IRawModel } from 'datx-utils'; | ||
import { PureModel } from '../../PureModel'; | ||
import { PureCollection } from '../../PureCollection'; | ||
import { IReferenceDefinition } from '../../Attribute'; | ||
import { IReferenceDefinition, ParsedRefModel } from '../../Attribute'; | ||
import { TRefValue } from '../../interfaces/TRefValue'; | ||
import { IType } from '../../interfaces/IType'; | ||
export declare function getModelRefType(model: ParsedRefModel, data: any, parentModel: PureModel, key: string, collection?: PureCollection): IType; | ||
export declare function initModelRef<T extends PureModel>(model: T, key: string, referenceDef?: IReferenceDefinition, initialVal?: TRefValue): void; | ||
export declare function initModelField<T extends PureModel>(model: T, key: string, value: any): void; | ||
export declare function initModel(instance: PureModel, rawData: IRawModel, collection?: PureCollection): void; |
@@ -11,2 +11,3 @@ import { IRawModel } from 'datx-utils'; | ||
export declare function isModelReference(value: unknown): false; | ||
export declare function isIdentifier(value: any): boolean; | ||
/** | ||
@@ -13,0 +14,0 @@ * Get the type of the given model |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("datx-utils"),n=require("mobx");(e=exports.ReferenceType||(exports.ReferenceType={}))[e.TO_ONE=0]="TO_ONE",e[e.TO_MANY=1]="TO_MANY",e[e.TO_ONE_OR_MANY=2]="TO_ONE_OR_MANY";var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function o(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var i=function(){return(i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function a(e,t,n,r){var o,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var s,u,l,c=/\$\{\s*([a-zA-Z0-9\-_]+)\s*\}/g;function p(e,t){return void 0===t&&(t={}),new Error(("[datx exception] "+function(e,t){for(var n=e,r=c.exec(n);r;)n=n.replace(r[0],t[r[1]]),r=c.exec(n);return n}(e,t)).replace(/\s+/g," "))}function f(e,n){var r=t.getMeta(e,"ref_"+n);return r?r.value:null}function d(e,r,o){var i;(null===(i=t.getMeta(e,s.Fields,{})[r])||void 0===i?void 0:i.referenceDef)&&(n.isArrayLike(e[r])?e[r].includes(o)||e[r].push(o):e[r]=o)}function _(e,t,r){n.isArrayLike(e[t])?e[t].remove(r):e[t]===r&&(e[t]=null)}function h(e){return 0===Object.keys(e).length}function y(e,n){var r=Object.assign({},e,{model:{id:A(n),type:k(n)}}),o=t.getMeta(n,s.PatchListeners,[]).slice(),i=V(n);i&&"__patchListeners"in i&&(i.__patchListeners||[]).forEach((function(e){o.push(e)})),o.forEach((function(e){e(r)}))}function v(e){var n=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});n.count++,t.setMeta(e,s.Patch,n)}function M(e,r){void 0===r&&(r=exports.PatchType.UPDATE);var o=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});if(o.count--,0===o.count){var i=n.toJS(o.newValue),a=n.toJS(o.oldValue);h(i)&&h(a)||y({newValue:i,oldValue:a,patchType:r},e),t.setMeta(e,s.Patch,{count:0,oldValue:{},newValue:{}})}else t.setMeta(e,s.Patch,o)}function b(e,n,r,o){v(e),function(e,n,r,o){var i=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});if(!(e[n]===r&&!o||o&&r===o.value)){var a=t.getMeta(e,s.Fields,{});n in i.oldValue||(o?i.oldValue[n]=o.value:n in a&&a[n].referenceDef?i.oldValue[n]=I(e[n]):i.oldValue[n]=e[n]),i.newValue[n]=n in a&&a[n].referenceDef?I(r):r,t.setMeta(e,s.Patch,i)}}(e,n,r,o),M(e)}function m(e,t,n,r,o){return"function"==typeof e?k(e(t,n,r,o)):e}function g(e,r,o,i){var a=t.getMeta(e,s.Fields,{}),u=a[r]||{referenceDef:o},l=V(e);if(!l&&i)throw p("The model needs to be in a collection to be referenceable");if(o&&(a[r]={referenceDef:o}),u.referenceDef.property)t.assignComputed(e,r,(function(){return function(e,r){var o,i=null===(o=t.getMeta(e,s.Fields,{})[r])||void 0===o?void 0:o.referenceDef;if(!i||!i.property)return null;var a=V(e);if(!a)return null;var u=a.getAllModels().filter((function(t){return function(e,t,n){return null!==e[t]&&void 0!==e[t]&&(e[t]instanceof T?e[t]===n:e[t].includes(n))}(t,i.property,e)})),l=n.observable.array(u,{deep:!1});return n.intercept(l,(function(n){return function(e,n,r){var o,i=null===(o=t.getMeta(e,s.Fields,{})[n])||void 0===o?void 0:o.referenceDef;return i?"splice"===r.type?function(e,t,n,r){var o=r.property;return n.added.forEach((function(t){return d(t,o,e)})),e[t].slice(n.index,n.index+n.removedCount).forEach((function(t){return _(t,o,e)})),null}(e,n,r,i):function(e,n,r,o){var i=o.property,a=e[n].length>r.index?e[n][r.index]:null;return r.newValue&&d(r.newValue,i,e),a&&_(a,i,e),t.warn("This shouldn't have happened. Please open an issue: https://github.com/infinum/datx/issues/new"),null}(e,n,r,i):null}(e,r,n)})),l}(e,r)}),(function(e){!function(e,t,n){throw p("Back references are read only")}()}));else{var c=function(e){if(e===exports.ReferenceType.TO_ONE)return Y;if(e===exports.ReferenceType.TO_ONE_OR_MANY)return W;if(e===exports.ReferenceType.TO_MANY)return q;throw t.error("Unknown reference type")}(u.referenceDef.type),h=u.referenceDef.type===exports.ReferenceType.TO_MANY?[]:null;i&&(h=t.mapItems(i,(function(t){return"object"!=typeof t||P(t)?"object"==typeof t&&P(t)?(null==l?void 0:l.findOne(t))||null:(null==l?void 0:l.findOne(m(u.referenceDef.model,t,e,r,l),t))||null:(null==l?void 0:l.add(t,m(u.referenceDef.model,t,e,r,l)))||null})));var y=new c(h,l,!1,e,r,!0);b(e,r,y.value),t.setMeta(e,"ref_"+r,y),t.assignComputed(e,r,(function(){return f(e,r)}),(function(n){b(e,r,n),function(e,n,r){var o=t.getMeta(e,"ref_"+n);o&&(o.value=r)}(e,r,n)}))}}function O(e,r,o){var i=t.getMeta(e,s.Fields,{})[r],a=t.getMeta(e.constructor,u.TypeField,"__datx__type__",!0),l=t.getMeta(e.constructor,u.IdField,"__datx__id__",!0);r===a?t.assignComputed(e,r,(function(){return k(e)}),(function(){throw p("Model type can't be changed after initialization.")})):r===l?t.assignComputed(e,r,(function(){return A(e)}),(function(){throw p("Model ID can't be updated directly. Use the `updateModelId` helper function instead.")})):i.referenceDef?g(e,r,void 0,o):(t.setMeta(e,"data__"+r,void 0),t.assignComputed(e,r,(function(){return t.getMeta(e,"data__"+r)}),(function(o){var i,a="object"!=typeof(i=o)||null===i||i instanceof T?o:n.observable(o);b(e,r,o),t.setMeta(e,"data__"+r,a)})),e[r]=o)}function w(e,r,o){var a=e.constructor,l=t.getMeta(e.constructor,u.Fields,{},!0,!0),c=null==r?void 0:r[t.META_FIELD],p=Object.assign({},l,null==c?void 0:c.fields);t.setMeta(e,s.Collection,o);var f=t.getMeta(e.constructor,u.TypeField,"__datx__type__",!0);t.setMeta(e,s.TypeField,r[f]||(null==c?void 0:c.type)||a.type);var d=t.getMeta(e.constructor,u.IdField,"__datx__id__",!0);t.setMeta(e,s.IdField,r[d]||(null==c?void 0:c.id)||a.getAutoId()),t.setMeta(e,s.OriginalId,null==c?void 0:c.originalId),Object.keys(r).filter((function(e){return e!==t.META_FIELD})).filter((function(e){return!(e in p)})).forEach((function(e){var t=r[e],o=t instanceof T||n.isArrayLike(t)&&(t[0]instanceof T||P(t[0]))||P(t);p[e]={referenceDef:!!o&&{type:exports.ReferenceType.TO_ONE_OR_MANY,model:k(t)}}})),t.setMeta(e,s.Fields,p),Object.keys(p).forEach((function(t){var n,o=p[t],s=i(((n={})[t]=o.defaultValue,n),r);O(e,t,L(a,s,t))})),J(e)}!function(e){e.Fields="fields",e.IdField="id",e.TypeField="type",e.Collection="collection",e.OriginalId="originalId",e.Patch="patch",e.PatchListeners="patchListeners",e.Commit="commit"}(s||(s={})),function(e){e.Fields="fields",e.IdField="idField",e.TypeField="typeField",e.MapParse="parse",e.MapSerialize="serialize"}(u||(u={})),(l=exports.PatchType||(exports.PatchType={}))[l.CRATE=0]="CRATE",l[l.UPDATE=1]="UPDATE",l[l.REMOVE=2]="REMOVE";var T=function(){function e(e,t){void 0===e&&(e={}),v(this),w(this,e,t),M(this,exports.PatchType.CRATE)}return e.preprocess=function(e){return e},e.getAutoId=function(){return"number"==typeof this.autoIdValue?--this.autoIdValue:this.autoIdValue},e.toJSON=function(){return this.type},e.type=t.DEFAULT_TYPE,e.autoIdValue=0,e.enableAutoId=!0,e}(),x=function(e,t){return e};function L(e,n,r){return t.getMeta(e,u.MapParse+"_"+r,x,!0)(n[r],n)}function E(e,n,r){return t.getMeta(e,u.MapSerialize+"_"+r,x,!0)(n[r],n)}function P(e){return n.isArrayLike(e)?e.every(P):"object"==typeof e&&null!==e&&"type"in e&&"id"in e&&2===Object.keys(e).length}function k(e){return"function"==typeof e||P(e)?e.type:"object"==typeof e?t.getMeta(e,s.TypeField)||e.constructor.type:e}function A(e){if(e instanceof T){var n=t.getMeta(e,s.IdField);if(void 0!==n)return n;throw p("Model without an ID")}return e}function V(e){return t.getMeta(e,s.Collection)}function j(e){return!(e instanceof T)}function I(e){return e instanceof T?{id:A(e),type:k(e)}:e}var F=[s.Patch,s.PatchListeners,s.Collection,s.Commit,s.OriginalId,"get__","set__","data__","ref_"];function D(e){var r,o,i,a,u=t.getMetaObj(e),l=t.getMeta(e,s.Fields,{}),c=Object.keys(u).filter((function(e){return!F.some((function(t){return e.startsWith(t)}))})),p=((r={})[t.META_FIELD]=Object.assign({},(i=c.map((function(e){return[e,u[e]]})),a={},i.forEach((function(e){var t=e[0],n=e[1];a[t]=n})),a),((o={})[s.IdField]=A(e),o[s.TypeField]=k(e),o[s.Collection]=void 0,o)),r);return Object.keys(l).forEach((function(n){if(l[n].referenceDef){var r=t.getMeta(e,"ref_"+n);p[n]=(null==r?void 0:r.snapshot)||null}else p[n]=E(e.constructor,e,n)})),n.toJS(p)}function N(e){var n=D(e),r=n[t.META_FIELD]||{};r[s.OriginalId]=r[s.IdField],delete r[s.IdField];var o=V(e);if(o){var i=k(e);return o.add(n,i)}var a=e.constructor;return t.warn("The model is not in the collection. Referencing the original model won't be possible"),new a(n)}var B=[s.Fields,s.IdField,s.TypeField];function R(e,r,o){n.runInAction((function(){var i,a;if(!(e instanceof T))throw p("The given parameter is not a valid model");var u=t.getMeta(e,s.Fields,{}),l=n.isArrayLike(o)&&o[0]instanceof T||o instanceof T;if(r in u){if(l&&!u[r].referenceDef)throw p("You should save this value as a reference.");e[r]=o}else l?n.extendObservable(u,((i={})[r]={referenceDef:{type:exports.ReferenceType.TO_ONE_OR_MANY,models:Array.from(new Set(t.mapItems(o,k)))}},i)):n.extendObservable(u,((a={})[r]={referenceDef:!1},a)),t.setMeta(e,s.Fields,u),O(e,r,o)}))}function C(e,n){v(e);var r=t.getMeta(e.constructor,u.IdField,"__datx__id__"),o=t.getMeta(e.constructor,u.TypeField,"__datx__type__"),i=Object.keys(n instanceof T?D(n):n);return t.mergeMeta(e,function(e,t){var n={};return Object.keys(e).filter((function(e){return!t.includes(e)})).forEach((function(t){n[t]=e[t]})),n}(n[t.META_FIELD]||{},B)),i.forEach((function(i){i!==t.META_FIELD&&i!==r&&i!==o?R(e,i,n[i]):i===t.META_FIELD&&Object.keys(n[i]||{}).forEach((function(r){B.includes(r)||t.setMeta(e,r,n[i][r])}))})),M(e),e}function S(e,n){t.setMeta(e,s.Collection,n);var r=t.getMeta(e,s.Fields,{});v(e),Object.keys(r).forEach((function(r){var o=t.getMeta(e,"ref_"+r);o&&o.setCollection(n)})),M(e)}function J(e){t.setMeta(e,s.Commit,i(i({},D(e)),{__META__:void 0}))}function z(e){var n=t.getMeta(e,s.Commit);n&&C(e,n)}function U(e,n){var r,o,i=t.getMeta(e,s.Commit);return!(i&&(r=t.getMeta(e,s.Fields,{})[n].referenceDef?t.mapItems(e[n],I):e[n],o=i[n],JSON.stringify(r)===JSON.stringify(o)))}var q=function(){function e(e,t,r,o,i,a){var s=this;if(void 0===e&&(e=[]),void 0===r&&(r=!1),void 0===a&&(a=!0),this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__rawList=n.observable.array([]),(null==e?void 0:e.length)>0&&!t)throw p("The model needs to be in a collection to be referenceable");if(e&&!n.isArrayLike(e))throw p("The reference must be an array of values.");n.runInAction((function(){s.__rawList.replace(e||[]),s.setCollection(t)}))}return e.prototype.setCollection=function(e){var t=this;this.__collection=e,this.__disposer&&this.__disposer(),e&&(this.__rawList.forEach((function(e,n){var r=t.__getModel(e);r&&(t.__rawList[n]=r)})),this.__disposer=n.reaction((function(){return t.__rawList.filter(j).filter(Boolean).filter((function(t){return e.findOne(t.type,t.id)})).length>0}),this.__reMap.bind(this)))},Object.defineProperty(e.prototype,"value",{get:function(){return this.__getList()},set:function(e){if(this.__readonly)throw p("This is a read-only bucket");if(!n.isArrayLike(e))throw p("The reference must be an array of values.");this.__rawList.replace(e),this.__model&&this.__key&&b(this.__model,this.__key,e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.value.length},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__rawList.map(I)},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue.slice()},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),e.prototype.__getList=function(){var e=this,t=this.__rawList.map(this.__getModel.bind(this)).filter((function(t){return!e.__skipMissing||Boolean(t)})).filter((function(e){return Boolean(e&&V(e))})),r=n.observable.array(t,{deep:!1});return n.intercept(r,this.__partialRawListUpdate.bind(this)),r},e.prototype.__getModel=function(e){if(e instanceof T||null===e)return e;if(!this.__collection)throw p("The model needs to be in a collection to be referenceable");return this.__collection.findOne(e.type,e.id)},e.prototype.__partialRawListUpdate=function(e){if(this.__readonly)throw p("This is a read-only bucket");if("splice"===e.type){var t=e.added;return this.__rawList.slice(e.index,e.removedCount),this.__rawList.splice.apply(this.__rawList,[e.index,e.removedCount].concat(t)),null}var n=this.__getModel(e.newValue);return n&&(this.__rawList[e.index]=n),null},e.prototype.__reMap=function(){for(var e=0;e<this.__rawList.length;e++)if(j(this.__rawList[e])){var t=this.__getModel(this.__rawList[e]);t&&(this.__rawList[e]=t)}},a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"length",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),a([n.action],e.prototype,"__partialRawListUpdate",null),a([n.action],e.prototype,"__reMap",null),e}(),Y=function(){function e(e,t,r,o,i,a){if(void 0===r&&(r=!1),void 0===a&&(a=!0),this.__collection=t,this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__rawValue=null,e&&!this.__collection)throw p("The model needs to be in a collection to be referenceable");if(n.isArrayLike(e))throw p("The reference can't be an array of values.");if(!(P(e)||e instanceof T||null===e))throw p("The value needs to be a reference");this.__rawValue=e}return e.prototype.setCollection=function(e){this.__collection=e},Object.defineProperty(e.prototype,"value",{get:function(){return this.__rawValue?this.__getModel(this.__rawValue):null},set:function(e){if(!this.__collection)throw p("The model needs to be in a collection to be referenceable");if(this.__readonly)throw p("This is a read-only bucket");if(n.isArrayLike(e))throw p("The reference can't be an array of values.");if(!(P(e)||e instanceof T||null===e))throw p("The value needs to be a reference");this.__rawValue=e,this.__model&&this.__key&&b(this.__model,this.__key,e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__rawValue?I(this.__rawValue):null},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),e.prototype.__getModel=function(e){if(e instanceof T||null===e)return e;if(!this.__collection)throw p("The model needs to be in a collection to be referenceable");return this.__collection.findOne(e.type,e.id)||(this.__skipMissing?null:e)},a([n.observable],e.prototype,"__rawValue",void 0),a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),e}(),W=function(){function e(e,t,r,o,i,a){void 0===r&&(r=!1),void 0===a&&(a=!0),this.__collection=t,this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__isList=!0,this.__isList=n.isArrayLike(e),this.__isList?this.__toManyBucket=new q(e,t,r,o,i,a):this.__toOneBucket=new Y(e,t,r,o,i,a)}return e.prototype.setCollection=function(e){this.__collection=e,this.__toManyBucket&&this.__toManyBucket.setCollection(e),this.__toOneBucket&&this.__toOneBucket.setCollection(e)},Object.defineProperty(e.prototype,"value",{get:function(){return this.__isList?this.__toManyBucket.value:this.__toOneBucket.value},set:function(e){this.__isList=n.isArrayLike(e),this.__isList?this.__toManyBucket?this.__toManyBucket.value=e:this.__toManyBucket=new q(e,this.__collection):this.__toOneBucket?this.__toOneBucket.value=e:this.__toOneBucket=new Y(e,this.__collection)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"__readonlyValue",{set:function(e){if(this.__isList=n.isArrayLike(e),this.__isList)if(this.__toManyBucket){var t=this.__toManyBucket.__readonly;this.__toManyBucket.__readonly=!1,this.__toManyBucket.value=e,this.__toManyBucket.__readonly=t}else this.__toManyBucket=new q(e,this.__collection);else this.__toOneBucket?(t=this.__toOneBucket.__readonly,this.__toOneBucket.__readonly=!1,this.__toOneBucket.value=e,this.__toOneBucket.__readonly=t):this.__toOneBucket=new Y(e,this.__collection)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__isList?this.__toManyBucket.refValue:this.__toOneBucket.refValue},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),a([n.observable],e.prototype,"__isList",void 0),a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),e}();function Z(e,n,r){var o,a,l;if(!n&&0!==n)throw p("The type needs to be defined if the object is not an instance of the model.");var c=r.constructor,f=c.types.find((function(e){return e.type===n}));if(!f){var d=c.defaultModel;if(d)return new d(Object.assign({},e,((o={})[t.META_FIELD]=Object.assign({},e[t.META_FIELD]||{},{type:n}),o)),r);throw p("No model is defined for the type "+n+".")}var _=null===(a=null==e?void 0:e[t.META_FIELD])||void 0===a?void 0:a[s.IdField],h=t.getMeta(f,u.IdField),y=h&&null!==(l=null==e?void 0:e[h])&&void 0!==l?l:_,v=y&&r.findOne(n,y);if(v){var M=t.getMeta(f,u.Fields),b=Object.keys(i(i({},e),M)),m={};return b.forEach((function(t){m[t]=L(f,e,t)})),C(v,m)}return new f(f.preprocess(e),r)}var $=function(e){function r(t,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=e.call(this,o.map((function(e){return e instanceof T?e:{id:e,type:k(t)}})),n)||this;return a.__collection=n,a.unique=i,a.modelType=k(t),a.sortMethod=r,a}return o(r,e),Object.defineProperty(r.prototype,"length",{get:function(){return this.value.length},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"list",{get:function(){var e=this,t=this.value.slice();if(this.sortMethod){var r="string"==typeof this.sortMethod?function(t){return t[e.sortMethod]}:this.sortMethod;t.sort((function(e,t){return r(e)-r(t)}))}var o=n.observable.array(t,{deep:!1});return n.intercept(o,this.__partialListUpdate.bind(this)),o},enumerable:!1,configurable:!0}),r.prototype.toJSON=function(){return{modelType:this.modelType,models:this.__rawList.map(A).slice(),unique:this.unique}},r.prototype.add=function(e){var n=this,r=t.mapItems(e,(function(e){return n.__collection.add(e,n.modelType)}));return t.mapItems(r,(function(e){n.unique&&-1!==n.__indexOf(e)||n.__rawList.push(e)})),r},r.prototype.hasItem=function(e){var t=A(e);return Boolean(this.__getList().find((function(e){return A(e)===t})))},r.prototype.remove=function(e){var t=this.__getModel(this.__normalizeModel(e));t&&this.__rawList.remove(t)},r.prototype.removeAll=function(){this.__rawList.replace([])},r.prototype.__partialListUpdate=function(e){var t=this;if("splice"===e.type){if(this.sortMethod&&e.added.length>0)throw p("New models can't be added directly to a sorted view list");var n=e.added.map(this.__normalizeModel.bind(this)),r=this.__rawList.slice(e.index,e.removedCount);return this.unique&&n.forEach((function(e){if(-1!==t.__indexOf(e)&&-1===t.__indexOf(e,r))throw p("The models in this view need to be unique")})),this.__rawList.splice.apply(this.__rawList,[e.index,e.removedCount].concat(n)),null}if(this.sortMethod&&e.newValue)throw p("New models can't be added directly to a sorted view list");var o=this.__getModel(this.__normalizeModel(e.newValue));if(o){var i=this.__indexOf(o);if(this.unique&&-1!==i&&i!==e.index)throw p("The models in this view need to be unique");this.__rawList[e.index]=o}return null},r.prototype.__normalizeModel=function(e){return e instanceof T?e:{id:e,type:this.modelType}},r.prototype.__indexOf=function(e,t){return void 0===t&&(t=this.__rawList),t.findIndex((function(t){return t instanceof T&&e instanceof T?t===e:!(!j(t)||e instanceof T)&&t.id===e.id&&t.type===e.type}))},a([n.observable],r.prototype,"sortMethod",void 0),a([n.computed],r.prototype,"length",null),a([n.computed],r.prototype,"list",null),a([n.action],r.prototype,"add",null),a([n.action],r.prototype,"remove",null),a([n.action],r.prototype,"removeAll",null),a([n.action],r.prototype,"__partialListUpdate",null),r}(q),G=function(){function e(e){var t=this;void 0===e&&(e=[]),this.__data=n.observable.array([],{deep:!1}),this.__views=[],this.__dataMap={},this.__dataList={},n.extendObservable(this,{}),n.isArrayLike(e)?this.insert(e):e&&"models"in e&&this.insert(e.models);var r=this.constructor,o=e&&"views"in e?e.views:{};Object.keys(r.views).forEach((function(e){var n=r.views[e],i=o[e]||n;t.addView(e,i.modelType,{mixins:n.mixins,models:i.models||[],sortMethod:n.sortMethod,unique:i.unique})}))}return e.prototype.addView=function(e,t,n){var r=void 0===n?{}:n,o=r.sortMethod,i=r.models,a=void 0===i?[]:i,s=r.unique,u=r.mixins;if(e in this&&this[e])throw p("The name is already taken");var l=u?u.reduce((function(e,t){return t(e)}),$):$;return this.__views.push(e),this[e]=new l(t,this,o,a,s),this[e]},e.prototype.insert=function(e){var n=function(e,n){return n.map((function(n){return function(e,n){var r;return Z(n,null===(r=null==n?void 0:n[t.META_FIELD])||void 0===r?void 0:r[s.TypeField],e)}(e,n)}))}(this,e);return this.__insertModel(n),n},e.prototype.hasItem=function(e){var t=A(e);return Boolean(this.findOne(e,t))},e.prototype.add=function(e,t){return n.isArrayLike(e)?this.__addArray(e,t):this.__addSingle(e,t)},e.prototype.filter=function(e){return this.__data.filter(e)},e.prototype.findOne=function(e,t){if(t instanceof T)return t;if(P(e))return this.__findOneByType(e.type,e.id);if(!t)throw new Error("The identifier is missing");return this.__findOneByType(e,t)},e.prototype.findAll=function(e){var t;if(e){var r=k(e);return r in this.__dataList||n.set(this.__dataList,((t={})[r]=n.observable.array([]),t)),this.__dataList[r]}return this.__data},e.prototype.find=function(e){return this.__data.find(e)||null},e.prototype.removeOne=function(e,t){var n=null;"object"==typeof e?n=e:t&&(n=this.findOne(e,t)),n&&this.__removeModel(n)},e.prototype.removeAll=function(e){this.__removeModel(this.findAll(e).slice())},e.prototype.reset=function(){this.__data.forEach((function(e){t.setMeta(e,s.Collection,void 0),y({oldValue:D(e),patchType:exports.PatchType.REMOVE},e)})),this.__data.replace([]),this.__dataList=n.observable({},{},{deep:!1}),this.__dataMap=n.observable({},{},{deep:!1})},e.prototype.toJSON=function(){var e=this,t={};return this.__views.forEach((function(n){t[n]=e[n].toJSON()})),{models:this.__data.map(D),views:t}},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.__data.length},enumerable:!1,configurable:!0}),e.prototype.getAllModels=function(){return this.__data.slice()},e.prototype.__findOneByType=function(e,t){var r,o=k(e);if(!o)return null;var i=o.toString(),a=t.toString();return o in this.__dataMap?a in this.__dataMap[i]||n.set(this.__dataMap[i],a,null):n.set(this.__dataMap,i,n.observable.object(((r={})[a]=null,r),{},{deep:!1})),this.__dataMap[i][a]||null},e.prototype.__addArray=function(e,t){var n=this;return e.filter(Boolean).map((function(e){return n.__addSingle(e,t)}))},e.prototype.__addSingle=function(e,t){if(!e||"number"==typeof e||"string"==typeof e||P(e))return e;if(e instanceof T)return this.hasItem(e)||this.__insertModel(e),e;if(!t&&0!==t)throw p("The type needs to be defined if the object is not an instance of the model.");var n=k(t),r=Z(e,n,this);return this.__insertModel(r,n),r},e.prototype.__removeModel=function(e,r,o){var i=this;if(n.isArrayLike(e))e.forEach((function(e){i.__removeModel(e,r,o)}));else{var a=r||k(e),s=o||A(e);y({oldValue:n.toJS(D(e)),patchType:exports.PatchType.REMOVE},e),this.__data.remove(e),this.__dataList[a].remove(e),n.set(this.__dataMap[a],s.toString(),void 0),this.__data.forEach((function(e){var r=t.getMeta(e,u.Fields,{},!0,!0);Object.keys(r||{}).map((function(n){return t.getMeta(e,"ref_"+n)})).filter(Boolean).forEach((function(t){n.isArrayLike(t.value)&&t.value.includes(e)?t.value=t.value.filter((function(t){return t!==e})):t.value===e&&(t.value=null)}))})),S(e,void 0)}},e.prototype.__insertModel=function(e,t,r){var o,i=this;if(n.isArrayLike(e))e.forEach((function(e){i.__insertModel(e,t,r)}));else{var a=V(e);if(a&&a!==this)throw p("A model can be in a single collection at once");var s=t||k(e),u=r||A(e),l=s.toString(),c=this.findOne(s,u);c?C(c,e):(this.__data.push(e),s in this.__dataList?this.__dataList[s].push(e):n.set(this.__dataList,l,n.observable.array([e],{deep:!1})),s in this.__dataMap?n.set(this.__dataMap[s],u.toString(),e):n.set(this.__dataMap,l,n.observable.object(((o={})[u]=e,o),{},{deep:!1})),S(e,this),y({newValue:D(e),patchType:exports.PatchType.CRATE},e))}},e.prototype.__changeModelId=function(e,t,n){this.__dataMap[n][t]=this.__dataMap[n][e],delete this.__dataMap[n][e]},e.types=[],e.defaultModel=T,e.views={},a([n.observable.shallow],e.prototype,"__dataMap",void 0),a([n.observable.shallow],e.prototype,"__dataList",void 0),a([n.action],e.prototype,"insert",null),a([n.action],e.prototype,"add",null),a([n.action],e.prototype,"removeOne",null),a([n.action],e.prototype,"removeAll",null),a([n.action],e.prototype,"reset",null),a([n.computed],e.prototype,"length",null),e}();function H(e,t){for(var n=e;n;){if(n===t)return!0;n=Object.getPrototypeOf(n)}return!1}function K(e){return H(e,T)}function Q(e){return H(e,G)}function X(e,t,n){n&&"initializer"in n&&(n.initializer=void 0)}function ee(e){return e instanceof T||K(e)||P(e)?k(e):e}function te(e,t,n,r){return e?{type:exports.ReferenceType.TO_ONE,model:ee(e)}:t?{type:exports.ReferenceType.TO_ONE_OR_MANY,model:ee(t)}:!!n&&{type:exports.ReferenceType.TO_MANY,model:ee(n),property:r}}function ne(e){var n=void 0===e?{}:e,r=n.defaultValue,o=n.isIdentifier,i=n.isType,a=n.toOne,s=n.toOneOrMany,l=n.toMany,c=n.referenceProperty,p=n.parse,f=n.serialize;return function(e,n,d){X(0,0,d);var _=function(e){return"function"==typeof e?e:e.constructor}(e),h=t.getMeta(_,u.Fields,{});h[n]={referenceDef:te(a,s,l,c),defaultValue:r},t.setMeta(_,u.Fields,h),t.setMeta(_,u.MapParse+"_"+n,p),t.setMeta(_,u.MapSerialize+"_"+n,f),o&&t.setMeta(_,u.IdField,n),i&&t.setMeta(_,u.TypeField,n)}}function re(e,t){return void 0===t&&(t={}),function(n,r,o){X(0,0,o),Object.hasOwnProperty.call(n.constructor,"views")||(n.constructor.views={}),n.constructor.views[r]=Object.assign({modelType:e},t)}}var oe="@prop was deprecated, use @Attribute instead",ie=Object.assign((function(e,n,r){t.deprecated(oe),ne()(e,n,r)}),{defaultValue:function(e){return t.deprecated(oe),ne({defaultValue:e})},toOne:function(e){return t.deprecated(oe),ne({toOne:e})},toMany:function(e,n){return t.deprecated(oe),ne({toMany:e,referenceProperty:n})},toOneOrMany:function(e){return t.deprecated(oe),ne({toOneOrMany:e})},identifier:ne({isIdentifier:!0}),type:ne({isType:!0})}),ae=re;function se(e){var t=function(e){switch(e){case exports.PatchType.CRATE:return exports.PatchType.REMOVE;case exports.PatchType.REMOVE:return exports.PatchType.CRATE;default:return exports.PatchType.UPDATE}}(e.patchType);return{model:e.model,newValue:e.oldValue,oldValue:e.newValue,patchType:t}}function ue(e){if(Q(e))return function(e){function t(t){var n=e.call(this,t)||this;return n.__patchListeners=[],Object.defineProperty(n,"__patchListeners",{enumerable:!1}),n}return o(t,e),t.prototype.applyPatch=function(e){var t=this.findOne(e.model.type,e.model.id);if(e.patchType===exports.PatchType.REMOVE)t&&this.removeOne(t);else if(e.patchType===exports.PatchType.UPDATE){if(!t||!e.newValue)throw p(t?"New patch value isn't set for an update patch":"The model should already exist for an update patch");C(t,e.newValue)}else{if(t)throw p("The model shouldn't exist before a create patch");if(!e.newValue)throw p("New patch value isn't set for a create patch");this.add(e.newValue,e.model.type)}},t.prototype.undoPatch=function(e){this.applyPatch(se(e))},t.prototype.onPatch=function(e){var t=this;return this.__patchListeners.push(e),function(){t.__patchListeners=t.__patchListeners.filter((function(t){return t!==e}))}},t}(e);if(K(e))return function(e){function n(n,r){var o=e.call(this,n,r)||this;return t.setMeta(o,s.PatchListeners,[]),o}return o(n,e),n.prototype.applyPatch=function(e){e.model.type===k(this)&&e.model.id===A(this)&&C(this,e.newValue||{})},n.prototype.undoPatch=function(e){this.applyPatch(se(e))},n.prototype.onPatch=function(e){var n=this,r=t.getMeta(this,s.PatchListeners,[]);return r.push(e),function(){t.setMeta(n,s.PatchListeners,r.filter((function(t){return t!==e})))}},n}(e);throw p("Only Models and Collections can be decorated with patches")}function le(e){var r=e;if(!K(r))throw p("This mixin can only decorate models");var i=function(){function e(e){this.__instance=e}return Object.defineProperty(e.prototype,"collection",{get:function(){return V(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"id",{get:function(){return A(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"original",{get:function(){var e=t.getMeta(this.__instance,s.OriginalId),n=V(this.__instance);return e&&(null==n?void 0:n.findOne(this.__instance,e))||void 0},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refs",{get:function(){var e=this,n=t.getMeta(this.__instance,s.Fields,{}),r={};return Object.keys(n).filter((function(e){return n[e].referenceDef})).forEach((function(n){var o=t.getMeta(e.__instance,"ref_"+n);o&&(r[n]=o&&o.refValue||null)})),r},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"dirty",{get:function(){var e=this,n=t.getMeta(this.__instance,s.Fields,{}),r={};return Object.keys(n).forEach((function(t){r[t]=U(e.__instance,t)})),r},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return D(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"type",{get:function(){return k(this.__instance)},enumerable:!1,configurable:!0}),a([n.computed],e.prototype,"collection",null),a([n.computed],e.prototype,"id",null),a([n.computed],e.prototype,"original",null),a([n.computed],e.prototype,"refs",null),a([n.computed],e.prototype,"dirty",null),a([n.computed],e.prototype,"snapshot",null),a([n.computed],e.prototype,"type",null),e}();return function(e){function t(t,n){var r=e.call(this,t,n)||this;return r.meta=new i(r),Object.defineProperty(r,"meta",{enumerable:!1}),r}return o(t,e),t}(r)}function ce(e){var t=e;if(!K(e))throw p("This mixin can only decorate models");return function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.prototype.update=function(e){C(this,e)},t.prototype.clone=function(){return N(this)},t.prototype.assign=function(e,t){R(this,e,t)},t.prototype.addReference=function(e,t,n){g(this,e,{type:n.type,model:k(n.model)},t)},t.prototype.commit=function(){J(this)},t.prototype.revert=function(){z(this)},t.prototype.toJSON=function(){return D(this)},a([n.action],t.prototype,"update",null),a([n.action],t.prototype,"assign",null),t}(t)}var pe=function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return o(n,e),n.prototype.valueOf=function(){var e=this.meta.snapshot;return delete e[t.META_FIELD],Object.assign({},e,{meta:{id:this.meta.id,type:this.meta.type}})},n.prototype.toString=function(){return JSON.stringify(this.valueOf(),null,2)},n}(ue(ce(le(T)))),fe=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.defaultModel=pe,t}(ue(G)),de={ToMany:q,ToOne:Y,ToOneOrMany:W};exports.Attribute=ne,exports.Bucket=de,exports.Collection=fe,exports.Model=pe,exports.PureCollection=G,exports.PureModel=T,exports.View=$,exports.ViewAttribute=re,exports.assignModel=R,exports.cloneModel=N,exports.commitModel=J,exports.getModelCollection=V,exports.getModelId=A,exports.getModelType=k,exports.getOriginalModel=function(e){var n=V(e),r=t.getMeta(e,s.OriginalId);if(r){if(!n)throw p("The model needs to be in a collection to be referenceable");return n.findOne(e,r)}throw p("The given model is not a clone.")},exports.getRef=f,exports.getRefId=function(e,n){var r=t.getMeta(e,"ref_"+n);return r?r.refValue:null},exports.initModelRef=g,exports.isAttributeDirty=U,exports.isCollection=Q,exports.isModel=K,exports.isView=function(e){return H(e,$)},exports.modelMapParse=L,exports.modelMapSerialize=E,exports.modelToDirtyJSON=function(e){var t=i({},D(e));return Object.keys(e).forEach((function(n){U(e,n)||delete t[n]})),t},exports.modelToJSON=D,exports.prop=ie,exports.revertModel=z,exports.updateModel=C,exports.updateModelId=function(e,r){n.runInAction((function(){var o=V(e),i=A(e),a=k(e);t.setMeta(e,s.IdField,r),o&&o.__changeModelId(i,r,a),function(e,r,o,i){var a=V(e);a&&a.getAllModels().forEach((function(e){(function(e,n){var r=t.getMeta(e,u.Fields,{},!0,!0);return Object.keys(r).filter((function(e){return r[e].referenceDef})).filter((function(e){return!r[e].referenceDef.property})).filter((function(e){return r[e].referenceDef.model===n}))})(e,i).map((function(n){return t.getMeta(e,"ref_"+n)})).filter(Boolean).forEach((function(e){if(n.isArrayLike(e.value)){var t=e.value.findIndex((function(e){return A(e)===o&&k(e)===i}));-1!==t&&(e.value[t]=r)}else e.value&&A(e.value)===o&&k(e.value)===i&&(e.value={id:r,type:i})}))}))}(e,r,i,a)}))},exports.view=ae,exports.withActions=ce,exports.withMeta=le,exports.withPatches=ue; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("datx-utils"),n=require("mobx");(e=exports.ReferenceType||(exports.ReferenceType={}))[e.TO_ONE=0]="TO_ONE",e[e.TO_MANY=1]="TO_MANY",e[e.TO_ONE_OR_MANY=2]="TO_ONE_OR_MANY";var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function o(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var i=function(){return(i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function a(e,t,n,r){var o,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var s,u,l,c=/\$\{\s*([a-zA-Z0-9\-_]+)\s*\}/g;function f(e,t){return void 0===t&&(t={}),new Error(("[datx exception] "+function(e,t){for(var n=e,r=c.exec(n);r;)n=n.replace(r[0],t[r[1]]),r=c.exec(n);return n}(e,t)).replace(/\s+/g," "))}function p(e,n){var r=t.getMeta(e,"ref_"+n);return r?r.value:null}function d(e,r,o){var i;(null===(i=t.getMeta(e,s.Fields,{})[r])||void 0===i?void 0:i.referenceDef)&&(n.isArrayLike(e[r])?e[r].includes(o)||e[r].push(o):e[r]=o)}function _(e,t,r){n.isArrayLike(e[t])?e[t].remove(r):e[t]===r&&(e[t]=null)}function h(e){return 0===Object.keys(e).length}function y(e,n){var r=Object.assign({},e,{model:{id:V(n),type:A(n)}}),o=t.getMeta(n,s.PatchListeners,[]).slice(),i=j(n);i&&"__patchListeners"in i&&(i.__patchListeners||[]).forEach((function(e){o.push(e)})),o.forEach((function(e){e(r)}))}function v(e){var n=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});n.count++,t.setMeta(e,s.Patch,n)}function M(e,r){void 0===r&&(r=exports.PatchType.UPDATE);var o=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});if(o.count--,0===o.count){var i=n.toJS(o.newValue),a=n.toJS(o.oldValue);h(i)&&h(a)||y({newValue:i,oldValue:a,patchType:r},e),t.setMeta(e,s.Patch,{count:0,oldValue:{},newValue:{}})}else t.setMeta(e,s.Patch,o)}function b(e,n,r,o){v(e),function(e,n,r,o){var i=t.getMeta(e,s.Patch,{count:0,newValue:{},oldValue:{}});if(!(e[n]===r&&!o||o&&r===o.value)){var a=t.getMeta(e,s.Fields,{});n in i.oldValue||(o?i.oldValue[n]=o.value:n in a&&a[n].referenceDef?i.oldValue[n]=F(e[n]):i.oldValue[n]=e[n]),i.newValue[n]=n in a&&a[n].referenceDef?F(r):r,t.setMeta(e,s.Patch,i)}}(e,n,r,o),M(e)}function m(e,t,n,r,o){return"function"==typeof e?A(e(t,n,r,o)):e}function g(e,r,o,i){var a=t.getMeta(e,s.Fields,{}),u=a[r]||{referenceDef:o},l=j(e);if(!l&&i)throw f("The model needs to be in a collection to be referenceable");if(o&&(a[r]={referenceDef:o}),u.referenceDef.property)t.assignComputed(e,r,(function(){return function(e,r){var o,i=null===(o=t.getMeta(e,s.Fields,{})[r])||void 0===o?void 0:o.referenceDef;if(!i||!i.property)return null;var a=j(e);if(!a)return null;var u=a.getAllModels().filter((function(t){return function(e,t,n){return null!==e[t]&&void 0!==e[t]&&(e[t]instanceof T?e[t]===n:e[t].includes(n))}(t,i.property,e)})),l=n.observable.array(u,{deep:!1});return n.intercept(l,(function(n){return function(e,n,r){var o,i=null===(o=t.getMeta(e,s.Fields,{})[n])||void 0===o?void 0:o.referenceDef;return i?"splice"===r.type?function(e,t,n,r){var o=r.property;return n.added.forEach((function(t){return d(t,o,e)})),e[t].slice(n.index,n.index+n.removedCount).forEach((function(t){return _(t,o,e)})),null}(e,n,r,i):function(e,n,r,o){var i=o.property,a=e[n].length>r.index?e[n][r.index]:null;return r.newValue&&d(r.newValue,i,e),a&&_(a,i,e),t.warn("This shouldn't have happened. Please open an issue: https://github.com/infinum/datx/issues/new"),null}(e,n,r,i):null}(e,r,n)})),l}(e,r)}),(function(e){!function(e,t,n){throw f("Back references are read only")}()}));else{var c=function(e){if(e===exports.ReferenceType.TO_ONE)return W;if(e===exports.ReferenceType.TO_ONE_OR_MANY)return Z;if(e===exports.ReferenceType.TO_MANY)return Y;throw t.error("Unknown reference type")}(u.referenceDef.type),h=u.referenceDef.type===exports.ReferenceType.TO_MANY?[]:null;i&&(h=t.mapItems(i,(function(t){return"object"!=typeof t||P(t)?"object"==typeof t&&P(t)?(null==l?void 0:l.findOne(t))||t:(null==l?void 0:l.findOne(m(u.referenceDef.model,t,e,r,l),t))||{id:t,type:m(u.referenceDef.model,t,e,r,l)}:(null==l?void 0:l.add(t,m(u.referenceDef.model,t,e,r,l)))||null})));var y=new c(h,l,!1,e,r,!0);b(e,r,y.value),t.setMeta(e,"ref_"+r,y),t.assignComputed(e,r,(function(){return p(e,r)}),(function(o){b(e,r,o),function(e,r,o){var i=t.getMeta(e,"ref_"+r);if(k(o)||n.isArrayLike(o)){var a=t.getMeta(e,s.Fields,{})[r],u=m(a.referenceDef.model,a.referenceDef.defaultValue,e,r,j(e));o=t.mapItems(o,(function(e){return k(e)?{id:e,type:u}:F(e)}))}i&&(i.value=o)}(e,r,o)}))}}function O(e,r,o){var i=t.getMeta(e,s.Fields,{})[r],a=t.getMeta(e.constructor,u.TypeField,"__datx__type__",!0),l=t.getMeta(e.constructor,u.IdField,"__datx__id__",!0);r===a?t.assignComputed(e,r,(function(){return A(e)}),(function(){throw f("Model type can't be changed after initialization.")})):r===l?t.assignComputed(e,r,(function(){return V(e)}),(function(){throw f("Model ID can't be updated directly. Use the `updateModelId` helper function instead.")})):i.referenceDef?g(e,r,void 0,o):(t.setMeta(e,"data__"+r,void 0),t.assignComputed(e,r,(function(){return t.getMeta(e,"data__"+r)}),(function(o){var i,a="object"!=typeof(i=o)||null===i||i instanceof T?o:n.observable(o);b(e,r,o),t.setMeta(e,"data__"+r,a)})),e[r]=o)}function w(e,r,o){var a=e.constructor,l=t.getMeta(e.constructor,u.Fields,{},!0,!0),c=null==r?void 0:r[t.META_FIELD],f=Object.assign({},l,null==c?void 0:c.fields);t.setMeta(e,s.Collection,o);var p=t.getMeta(e.constructor,u.TypeField,"__datx__type__",!0);t.setMeta(e,s.TypeField,r[p]||(null==c?void 0:c.type)||a.type);var d=t.getMeta(e.constructor,u.IdField,"__datx__id__",!0);t.setMeta(e,s.IdField,r[d]||(null==c?void 0:c.id)||a.getAutoId()),t.setMeta(e,s.OriginalId,null==c?void 0:c.originalId),Object.keys(r).filter((function(e){return e!==t.META_FIELD})).filter((function(e){return!(e in f)})).forEach((function(e){var t=r[e],o=t instanceof T||n.isArrayLike(t)&&(t[0]instanceof T||P(t[0]))||P(t);f[e]={referenceDef:!!o&&{type:exports.ReferenceType.TO_ONE_OR_MANY,model:A(t)}}})),t.setMeta(e,s.Fields,f),Object.keys(f).forEach((function(t){var n,o=f[t],s=i(((n={})[t]=o.defaultValue,n),r);O(e,t,L(a,s,t))})),z(e)}!function(e){e.Fields="fields",e.IdField="id",e.TypeField="type",e.Collection="collection",e.OriginalId="originalId",e.Patch="patch",e.PatchListeners="patchListeners",e.Commit="commit"}(s||(s={})),function(e){e.Fields="fields",e.IdField="idField",e.TypeField="typeField",e.MapParse="parse",e.MapSerialize="serialize"}(u||(u={})),(l=exports.PatchType||(exports.PatchType={}))[l.CRATE=0]="CRATE",l[l.UPDATE=1]="UPDATE",l[l.REMOVE=2]="REMOVE";var T=function(){function e(e,t){void 0===e&&(e={}),v(this),w(this,e,t),M(this,exports.PatchType.CRATE)}return e.preprocess=function(e){return e},e.getAutoId=function(){return"number"==typeof this.autoIdValue?--this.autoIdValue:this.autoIdValue},e.toJSON=function(){return this.type},e.type=t.DEFAULT_TYPE,e.autoIdValue=0,e.enableAutoId=!0,e}(),x=function(e,t){return e};function L(e,n,r){return t.getMeta(e,u.MapParse+"_"+r,x,!0)(n[r],n)}function E(e,n,r){return t.getMeta(e,u.MapSerialize+"_"+r,x,!0)(n[r],n)}function P(e){return n.isArrayLike(e)?e.every(P):"object"==typeof e&&null!==e&&"type"in e&&"id"in e&&2===Object.keys(e).length}function k(e){return"string"==typeof e||"number"==typeof e}function A(e){return"function"==typeof e||P(e)?e.type:"object"==typeof e?t.getMeta(e,s.TypeField)||e.constructor.type:e}function V(e){if(e instanceof T){var n=t.getMeta(e,s.IdField);if(void 0!==n)return n;throw f("Model without an ID")}return e}function j(e){return t.getMeta(e,s.Collection)}function I(e){return!(e instanceof T)}function F(e){return e instanceof T?{id:V(e),type:A(e)}:e}var D=[s.Patch,s.PatchListeners,s.Collection,s.Commit,s.OriginalId,"get__","set__","data__","ref_"];function N(e){var r,o,i,a,u=t.getMetaObj(e),l=t.getMeta(e,s.Fields,{}),c=Object.keys(u).filter((function(e){return!D.some((function(t){return e.startsWith(t)}))})),f=((r={})[t.META_FIELD]=Object.assign({},(i=c.map((function(e){return[e,u[e]]})),a={},i.forEach((function(e){var t=e[0],n=e[1];a[t]=n})),a),((o={})[s.IdField]=V(e),o[s.TypeField]=A(e),o[s.Collection]=void 0,o)),r);return Object.keys(l).forEach((function(n){if(l[n].referenceDef){var r=t.getMeta(e,"ref_"+n);f[n]=(null==r?void 0:r.snapshot)||null}else f[n]=E(e.constructor,e,n)})),n.toJS(f)}function B(e){var n=N(e),r=n[t.META_FIELD]||{};r[s.OriginalId]=r[s.IdField],delete r[s.IdField];var o=j(e);if(o){var i=A(e);return o.add(n,i)}var a=e.constructor;return t.warn("The model is not in the collection. Referencing the original model won't be possible"),new a(n)}var R=[s.Fields,s.IdField,s.TypeField];function C(e,r,o){n.runInAction((function(){var i,a;if(!(e instanceof T))throw f("The given parameter is not a valid model");var u=t.getMeta(e,s.Fields,{}),l=n.isArrayLike(o)&&o.length>0&&o[0]instanceof T||o instanceof T;if(r in u){if(l&&!u[r].referenceDef)throw f("You should save this value as a reference.");e[r]=o}else l?n.extendObservable(u,((i={})[r]={referenceDef:{type:exports.ReferenceType.TO_ONE_OR_MANY,models:Array.from(new Set(t.mapItems(o,A)))}},i)):n.extendObservable(u,((a={})[r]={referenceDef:!1},a)),t.setMeta(e,s.Fields,u),O(e,r,o)}))}function S(e,n){v(e);var r=t.getMeta(e.constructor,u.IdField,"__datx__id__"),o=t.getMeta(e.constructor,u.TypeField,"__datx__type__"),i=Object.keys(n instanceof T?N(n):n);return t.mergeMeta(e,function(e,t){var n={};return Object.keys(e).filter((function(e){return!t.includes(e)})).forEach((function(t){n[t]=e[t]})),n}(n[t.META_FIELD]||{},R)),i.forEach((function(i){i!==t.META_FIELD&&i!==r&&i!==o?C(e,i,n[i]):i===t.META_FIELD&&Object.keys(n[i]||{}).forEach((function(r){R.includes(r)||t.setMeta(e,r,n[i][r])}))})),M(e),e}function J(e,n){t.setMeta(e,s.Collection,n);var r=t.getMeta(e,s.Fields,{});v(e),Object.keys(r).forEach((function(r){var o=t.getMeta(e,"ref_"+r);o&&o.setCollection(n)})),M(e)}function z(e){t.setMeta(e,s.Commit,i(i({},N(e)),{__META__:void 0}))}function U(e){var n=t.getMeta(e,s.Commit);n&&S(e,n)}function q(e,n){var r,o,i=t.getMeta(e,s.Commit);return!(i&&(r=t.getMeta(e,s.Fields,{})[n].referenceDef?t.mapItems(e[n],F):e[n],o=i[n],JSON.stringify(r)===JSON.stringify(o)))}var Y=function(){function e(e,t,r,o,i,a){var s=this;if(void 0===e&&(e=[]),void 0===r&&(r=!1),void 0===a&&(a=!0),this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__rawList=n.observable.array([]),(null==e?void 0:e.length)>0&&!t)throw f("The model needs to be in a collection to be referenceable");if(e&&!n.isArrayLike(e))throw f("The reference must be an array of values.");n.runInAction((function(){s.__rawList.replace(e||[]),s.setCollection(t)}))}return e.prototype.setCollection=function(e){var t=this;this.__collection=e,this.__disposer&&this.__disposer(),e&&(this.__rawList.forEach((function(e,n){var r=t.__getModel(e);r&&(t.__rawList[n]=r)})),this.__disposer=n.reaction((function(){return t.__rawList.filter(I).filter(Boolean).filter((function(t){return e.findOne(t.type,t.id)})).length>0}),this.__reMap.bind(this)))},Object.defineProperty(e.prototype,"value",{get:function(){return this.__getList()},set:function(e){if(this.__readonly)throw f("This is a read-only bucket");if(!n.isArrayLike(e))throw f("The reference must be an array of values.");this.__rawList.replace(e),this.__model&&this.__key&&b(this.__model,this.__key,e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.value.length},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__rawList.map(F)},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue.slice()},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),e.prototype.__getList=function(){var e=this,t=this.__rawList.map(this.__getModel.bind(this)).filter((function(t){return!e.__skipMissing||Boolean(t)})).filter((function(e){return Boolean(e&&j(e))})),r=n.observable.array(t,{deep:!1});return n.intercept(r,this.__partialRawListUpdate.bind(this)),r},e.prototype.__getModel=function(e){if(e instanceof T||null===e)return e;if(!this.__collection)throw f("The model needs to be in a collection to be referenceable");return this.__collection.findOne(e.type,e.id)},e.prototype.__partialRawListUpdate=function(e){if(this.__readonly)throw f("This is a read-only bucket");if("splice"===e.type){var t=e.added;return this.__rawList.slice(e.index,e.removedCount),this.__rawList.splice.apply(this.__rawList,[e.index,e.removedCount].concat(t)),null}var n=this.__getModel(e.newValue);return n&&(this.__rawList[e.index]=n),null},e.prototype.__reMap=function(){for(var e=0;e<this.__rawList.length;e++)if(I(this.__rawList[e])){var t=this.__getModel(this.__rawList[e]);t&&(this.__rawList[e]=t)}},a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"length",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),a([n.action],e.prototype,"__partialRawListUpdate",null),a([n.action],e.prototype,"__reMap",null),e}(),W=function(){function e(e,t,r,o,i,a){if(void 0===r&&(r=!1),void 0===a&&(a=!0),this.__collection=t,this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__rawValue=null,e&&!this.__collection)throw f("The model needs to be in a collection to be referenceable");if(n.isArrayLike(e))throw f("The reference can't be an array of values.");if(!(P(e)||e instanceof T||null===e))throw f("The value needs to be a reference");this.__rawValue=e}return e.prototype.setCollection=function(e){this.__collection=e},Object.defineProperty(e.prototype,"value",{get:function(){return this.__rawValue?this.__getModel(this.__rawValue):null},set:function(e){if(!this.__collection)throw f("The model needs to be in a collection to be referenceable");if(this.__readonly)throw f("This is a read-only bucket");if(n.isArrayLike(e))throw f("The reference can't be an array of values.");if(!(P(e)||e instanceof T||null===e))throw f("The value needs to be a reference");this.__rawValue=e,this.__model&&this.__key&&b(this.__model,this.__key,e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__rawValue?F(this.__rawValue):null},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),e.prototype.__getModel=function(e){if(e instanceof T||null===e)return e;if(!this.__collection)throw f("The model needs to be in a collection to be referenceable");return this.__collection.findOne(e.type,e.id)||(this.__skipMissing?null:e)},a([n.observable],e.prototype,"__rawValue",void 0),a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),e}(),Z=function(){function e(e,t,r,o,i,a){void 0===r&&(r=!1),void 0===a&&(a=!0),this.__collection=t,this.__readonly=r,this.__model=o,this.__key=i,this.__skipMissing=a,this.__isList=!0,this.__isList=n.isArrayLike(e),this.__isList?this.__toManyBucket=new Y(e,t,r,o,i,a):this.__toOneBucket=new W(e,t,r,o,i,a)}return e.prototype.setCollection=function(e){this.__collection=e,this.__toManyBucket&&this.__toManyBucket.setCollection(e),this.__toOneBucket&&this.__toOneBucket.setCollection(e)},Object.defineProperty(e.prototype,"value",{get:function(){return this.__isList?this.__toManyBucket.value:this.__toOneBucket.value},set:function(e){this.__isList=n.isArrayLike(e),this.__isList?this.__toManyBucket?this.__toManyBucket.value=e:this.__toManyBucket=new Y(e,this.__collection):this.__toOneBucket?this.__toOneBucket.value=e:this.__toOneBucket=new W(e,this.__collection)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"__readonlyValue",{set:function(e){if(this.__isList=n.isArrayLike(e),this.__isList)if(this.__toManyBucket){var t=this.__toManyBucket.__readonly;this.__toManyBucket.__readonly=!1,this.__toManyBucket.value=e,this.__toManyBucket.__readonly=t}else this.__toManyBucket=new Y(e,this.__collection);else this.__toOneBucket?(t=this.__toOneBucket.__readonly,this.__toOneBucket.__readonly=!1,this.__toOneBucket.value=e,this.__toOneBucket.__readonly=t):this.__toOneBucket=new W(e,this.__collection)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refValue",{get:function(){return this.__isList?this.__toManyBucket.refValue:this.__toOneBucket.refValue},enumerable:!1,configurable:!0}),e.prototype.toJSON=function(){return this.refValue},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),a([n.observable],e.prototype,"__isList",void 0),a([n.computed],e.prototype,"value",null),a([n.computed],e.prototype,"refValue",null),a([n.computed],e.prototype,"snapshot",null),e}();function $(e,n,r){var o,a,l;if(!n&&0!==n)throw f("The type needs to be defined if the object is not an instance of the model.");var c=r.constructor,p=c.types.find((function(e){return e.type===n}));if(!p){var d=c.defaultModel;if(d)return new d(Object.assign({},e,((o={})[t.META_FIELD]=Object.assign({},e[t.META_FIELD]||{},{type:n}),o)),r);throw f("No model is defined for the type "+n+".")}var _=null===(a=null==e?void 0:e[t.META_FIELD])||void 0===a?void 0:a[s.IdField],h=t.getMeta(p,u.IdField),y=h&&null!==(l=null==e?void 0:e[h])&&void 0!==l?l:_,v=y&&r.findOne(n,y);if(v){var M=t.getMeta(p,u.Fields),b=Object.keys(i(i({},e),M)),m={};return b.forEach((function(t){m[t]=L(p,e,t)})),S(v,m)}return new p(p.preprocess(e),r)}var G=function(e){function r(t,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=e.call(this,o.map((function(e){return e instanceof T?e:{id:e,type:A(t)}})),n)||this;return a.__collection=n,a.unique=i,a.modelType=A(t),a.sortMethod=r,a}return o(r,e),Object.defineProperty(r.prototype,"length",{get:function(){return this.value.length},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"list",{get:function(){var e=this,t=this.value.slice();if(this.sortMethod){var r="string"==typeof this.sortMethod?function(t){return t[e.sortMethod]}:this.sortMethod;t.sort((function(e,t){return r(e)-r(t)}))}var o=n.observable.array(t,{deep:!1});return n.intercept(o,this.__partialListUpdate.bind(this)),o},enumerable:!1,configurable:!0}),r.prototype.toJSON=function(){return{modelType:this.modelType,models:this.__rawList.map(V).slice(),unique:this.unique}},r.prototype.add=function(e){var n=this,r=t.mapItems(e,(function(e){return n.__collection.add(e,n.modelType)}));return t.mapItems(r,(function(e){n.unique&&-1!==n.__indexOf(e)||n.__rawList.push(e)})),r},r.prototype.hasItem=function(e){var t=V(e);return Boolean(this.__getList().find((function(e){return V(e)===t})))},r.prototype.remove=function(e){var t=this.__getModel(this.__normalizeModel(e));t&&this.__rawList.remove(t)},r.prototype.removeAll=function(){this.__rawList.replace([])},r.prototype.__partialListUpdate=function(e){var t=this;if("splice"===e.type){if(this.sortMethod&&e.added.length>0)throw f("New models can't be added directly to a sorted view list");var n=e.added.map(this.__normalizeModel.bind(this)),r=this.__rawList.slice(e.index,e.removedCount);return this.unique&&n.forEach((function(e){if(-1!==t.__indexOf(e)&&-1===t.__indexOf(e,r))throw f("The models in this view need to be unique")})),this.__rawList.splice.apply(this.__rawList,[e.index,e.removedCount].concat(n)),null}if(this.sortMethod&&e.newValue)throw f("New models can't be added directly to a sorted view list");var o=this.__getModel(this.__normalizeModel(e.newValue));if(o){var i=this.__indexOf(o);if(this.unique&&-1!==i&&i!==e.index)throw f("The models in this view need to be unique");this.__rawList[e.index]=o}return null},r.prototype.__normalizeModel=function(e){return e instanceof T?e:{id:e,type:this.modelType}},r.prototype.__indexOf=function(e,t){return void 0===t&&(t=this.__rawList),t.findIndex((function(t){return t instanceof T&&e instanceof T?t===e:!(!I(t)||e instanceof T)&&t.id===e.id&&t.type===e.type}))},a([n.observable],r.prototype,"sortMethod",void 0),a([n.computed],r.prototype,"length",null),a([n.computed],r.prototype,"list",null),a([n.action],r.prototype,"add",null),a([n.action],r.prototype,"remove",null),a([n.action],r.prototype,"removeAll",null),a([n.action],r.prototype,"__partialListUpdate",null),r}(Y),H=function(){function e(e){var t=this;void 0===e&&(e=[]),this.__data=n.observable.array([],{deep:!1}),this.__views=[],this.__dataMap={},this.__dataList={},n.extendObservable(this,{}),n.isArrayLike(e)?this.insert(e):e&&"models"in e&&this.insert(e.models);var r=this.constructor,o=e&&"views"in e?e.views:{};Object.keys(r.views).forEach((function(e){var n=r.views[e],i=o[e]||n;t.addView(e,i.modelType,{mixins:n.mixins,models:i.models||[],sortMethod:n.sortMethod,unique:i.unique})}))}return e.prototype.addView=function(e,t,n){var r=void 0===n?{}:n,o=r.sortMethod,i=r.models,a=void 0===i?[]:i,s=r.unique,u=r.mixins;if(e in this&&this[e])throw f("The name is already taken");var l=u?u.reduce((function(e,t){return t(e)}),G):G;return this.__views.push(e),this[e]=new l(t,this,o,a,s),this[e]},e.prototype.insert=function(e){var n=function(e,n){return n.map((function(n){return function(e,n){var r;return $(n,null===(r=null==n?void 0:n[t.META_FIELD])||void 0===r?void 0:r[s.TypeField],e)}(e,n)}))}(this,e);return this.__insertModel(n),n},e.prototype.hasItem=function(e){var t=V(e);return Boolean(this.findOne(e,t))},e.prototype.add=function(e,t){return n.isArrayLike(e)?this.__addArray(e,t):this.__addSingle(e,t)},e.prototype.filter=function(e){return this.__data.filter(e)},e.prototype.findOne=function(e,t){if(t instanceof T)return t;if(P(e))return this.__findOneByType(e.type,e.id);if(null==t)throw new Error("The identifier is missing");return this.__findOneByType(e,t)},e.prototype.findAll=function(e){var t;if(e){var r=A(e);return r in this.__dataList||n.set(this.__dataList,((t={})[r]=n.observable.array([]),t)),this.__dataList[r]}return this.__data},e.prototype.find=function(e){return this.__data.find(e)||null},e.prototype.removeOne=function(e,t){var n=null;"object"==typeof e?n=e:t&&(n=this.findOne(e,t)),n&&this.__removeModel(n)},e.prototype.removeAll=function(e){this.__removeModel(this.findAll(e).slice())},e.prototype.reset=function(){this.__data.forEach((function(e){t.setMeta(e,s.Collection,void 0),y({oldValue:N(e),patchType:exports.PatchType.REMOVE},e)})),this.__data.replace([]),this.__dataList=n.observable({},{},{deep:!1}),this.__dataMap=n.observable({},{},{deep:!1})},e.prototype.toJSON=function(){var e=this,t={};return this.__views.forEach((function(n){t[n]=e[n].toJSON()})),{models:this.__data.map(N),views:t}},Object.defineProperty(e.prototype,"snapshot",{get:function(){return this.toJSON()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"length",{get:function(){return this.__data.length},enumerable:!1,configurable:!0}),e.prototype.getAllModels=function(){return this.__data.slice()},e.prototype.__findOneByType=function(e,t){var r,o=A(e);if(!o)return null;var i=o.toString(),a=t.toString();return o in this.__dataMap?a in this.__dataMap[i]||n.set(this.__dataMap[i],a,null):n.set(this.__dataMap,i,n.observable.object(((r={})[a]=null,r),{},{deep:!1})),this.__dataMap[i][a]||null},e.prototype.__addArray=function(e,t){var n=this;return e.filter(Boolean).map((function(e){return n.__addSingle(e,t)}))},e.prototype.__addSingle=function(e,t){if(!e||"number"==typeof e||"string"==typeof e||P(e))return e;if(e instanceof T)return this.hasItem(e)||this.__insertModel(e),e;if(!t&&0!==t)throw f("The type needs to be defined if the object is not an instance of the model.");var n=A(t),r=$(e,n,this);return this.__insertModel(r,n),r},e.prototype.__removeModel=function(e,r,o){var i=this;if(n.isArrayLike(e))e.forEach((function(e){i.__removeModel(e,r,o)}));else{var a=r||A(e),s=o||V(e);y({oldValue:n.toJS(N(e)),patchType:exports.PatchType.REMOVE},e),this.__data.remove(e),this.__dataList[a].remove(e),n.set(this.__dataMap[a],s.toString(),void 0),this.__data.forEach((function(e){var r=t.getMeta(e,u.Fields,{},!0,!0);Object.keys(r||{}).map((function(n){return t.getMeta(e,"ref_"+n)})).filter(Boolean).forEach((function(t){n.isArrayLike(t.value)&&t.value.includes(e)?t.value=t.value.filter((function(t){return t!==e})):t.value===e&&(t.value=null)}))})),J(e,void 0)}},e.prototype.__insertModel=function(e,t,r){var o,i=this;if(n.isArrayLike(e))e.forEach((function(e){i.__insertModel(e,t,r)}));else{var a=j(e);if(a&&a!==this)throw f("A model can be in a single collection at once");var s=t||A(e),u=r||V(e),l=s.toString(),c=this.findOne(s,u);c&&c!==e?S(c,e):(this.__data.push(e),s in this.__dataList?this.__dataList[s].push(e):n.set(this.__dataList,l,n.observable.array([e],{deep:!1})),s in this.__dataMap?n.set(this.__dataMap[s],u.toString(),e):n.set(this.__dataMap,l,n.observable.object(((o={})[u]=e,o),{},{deep:!1})),J(e,this),y({newValue:N(e),patchType:exports.PatchType.CRATE},e))}},e.prototype.__changeModelId=function(e,t,n){this.__dataMap[n][t]=this.__dataMap[n][e],delete this.__dataMap[n][e]},e.types=[],e.defaultModel=T,e.views={},a([n.observable.shallow],e.prototype,"__dataMap",void 0),a([n.observable.shallow],e.prototype,"__dataList",void 0),a([n.action],e.prototype,"insert",null),a([n.action],e.prototype,"add",null),a([n.action],e.prototype,"removeOne",null),a([n.action],e.prototype,"removeAll",null),a([n.action],e.prototype,"reset",null),a([n.computed],e.prototype,"length",null),e}();function K(e,t){for(var n=e;n;){if(n===t)return!0;n=Object.getPrototypeOf(n)}return!1}function Q(e){return K(e,T)}function X(e){return K(e,H)}function ee(e,t,n){n&&"initializer"in n&&(n.initializer=void 0)}function te(e){return e instanceof T||Q(e)||P(e)?A(e):e}function ne(e,t,n,r){return e?{type:exports.ReferenceType.TO_ONE,model:te(e)}:t?{type:exports.ReferenceType.TO_ONE_OR_MANY,model:te(t)}:!!n&&{type:exports.ReferenceType.TO_MANY,model:te(n),property:r}}function re(e){var n=void 0===e?{}:e,r=n.defaultValue,o=n.isIdentifier,i=n.isType,a=n.toOne,s=n.toOneOrMany,l=n.toMany,c=n.referenceProperty,f=n.parse,p=n.serialize;return function(e,n,d){ee(0,0,d);var _=function(e){return"function"==typeof e?e:e.constructor}(e),h=t.getMeta(_,u.Fields,{});h[n]={referenceDef:ne(a,s,l,c),defaultValue:r},t.setMeta(_,u.Fields,h),t.setMeta(_,u.MapParse+"_"+n,f),t.setMeta(_,u.MapSerialize+"_"+n,p),o&&t.setMeta(_,u.IdField,n),i&&t.setMeta(_,u.TypeField,n)}}function oe(e,t){return void 0===t&&(t={}),function(n,r,o){ee(0,0,o),Object.hasOwnProperty.call(n.constructor,"views")||(n.constructor.views={}),n.constructor.views[r]=Object.assign({modelType:e},t)}}var ie="@prop was deprecated, use @Attribute instead",ae=Object.assign((function(e,n,r){t.deprecated(ie),re()(e,n,r)}),{defaultValue:function(e){return t.deprecated(ie),re({defaultValue:e})},toOne:function(e){return t.deprecated(ie),re({toOne:e})},toMany:function(e,n){return t.deprecated(ie),re({toMany:e,referenceProperty:n})},toOneOrMany:function(e){return t.deprecated(ie),re({toOneOrMany:e})},identifier:re({isIdentifier:!0}),type:re({isType:!0})}),se=oe;function ue(e){var t=function(e){switch(e){case exports.PatchType.CRATE:return exports.PatchType.REMOVE;case exports.PatchType.REMOVE:return exports.PatchType.CRATE;default:return exports.PatchType.UPDATE}}(e.patchType);return{model:e.model,newValue:e.oldValue,oldValue:e.newValue,patchType:t}}function le(e){if(X(e))return function(e){function t(t){var n=e.call(this,t)||this;return n.__patchListeners=[],Object.defineProperty(n,"__patchListeners",{enumerable:!1}),n}return o(t,e),t.prototype.applyPatch=function(e){var t=this.findOne(e.model.type,e.model.id);if(e.patchType===exports.PatchType.REMOVE)t&&this.removeOne(t);else if(e.patchType===exports.PatchType.UPDATE){if(!t||!e.newValue)throw f(t?"New patch value isn't set for an update patch":"The model should already exist for an update patch");S(t,e.newValue)}else{if(t)throw f("The model shouldn't exist before a create patch");if(!e.newValue)throw f("New patch value isn't set for a create patch");this.add(e.newValue,e.model.type)}},t.prototype.undoPatch=function(e){this.applyPatch(ue(e))},t.prototype.onPatch=function(e){var t=this;return this.__patchListeners.push(e),function(){t.__patchListeners=t.__patchListeners.filter((function(t){return t!==e}))}},t}(e);if(Q(e))return function(e){function n(n,r){var o=e.call(this,n,r)||this;return t.setMeta(o,s.PatchListeners,[]),o}return o(n,e),n.prototype.applyPatch=function(e){e.model.type===A(this)&&e.model.id===V(this)&&S(this,e.newValue||{})},n.prototype.undoPatch=function(e){this.applyPatch(ue(e))},n.prototype.onPatch=function(e){var n=this,r=t.getMeta(this,s.PatchListeners,[]);return r.push(e),function(){t.setMeta(n,s.PatchListeners,r.filter((function(t){return t!==e})))}},n}(e);throw f("Only Models and Collections can be decorated with patches")}function ce(e){var r=e;if(!Q(r))throw f("This mixin can only decorate models");var i=function(){function e(e){this.__instance=e}return Object.defineProperty(e.prototype,"collection",{get:function(){return j(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"id",{get:function(){return V(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"original",{get:function(){var e=t.getMeta(this.__instance,s.OriginalId),n=j(this.__instance);return e&&(null==n?void 0:n.findOne(this.__instance,e))||void 0},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"refs",{get:function(){var e=this,n=t.getMeta(this.__instance,s.Fields,{}),r={};return Object.keys(n).filter((function(e){return n[e].referenceDef})).forEach((function(n){var o=t.getMeta(e.__instance,"ref_"+n);o&&(r[n]=o&&o.refValue||null)})),r},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"dirty",{get:function(){var e=this,n=t.getMeta(this.__instance,s.Fields,{}),r={};return Object.keys(n).forEach((function(t){r[t]=q(e.__instance,t)})),r},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return N(this.__instance)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"type",{get:function(){return A(this.__instance)},enumerable:!1,configurable:!0}),a([n.computed],e.prototype,"collection",null),a([n.computed],e.prototype,"id",null),a([n.computed],e.prototype,"original",null),a([n.computed],e.prototype,"refs",null),a([n.computed],e.prototype,"dirty",null),a([n.computed],e.prototype,"snapshot",null),a([n.computed],e.prototype,"type",null),e}();return function(e){function t(t,n){var r=e.call(this,t,n)||this;return r.meta=new i(r),Object.defineProperty(r,"meta",{enumerable:!1}),r}return o(t,e),t}(r)}function fe(e){var t=e;if(!Q(e))throw f("This mixin can only decorate models");return function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.prototype.update=function(e){S(this,e)},t.prototype.clone=function(){return B(this)},t.prototype.assign=function(e,t){C(this,e,t)},t.prototype.addReference=function(e,t,n){g(this,e,{type:n.type,model:A(n.model)},t)},t.prototype.commit=function(){z(this)},t.prototype.revert=function(){U(this)},t.prototype.toJSON=function(){return N(this)},a([n.action],t.prototype,"update",null),a([n.action],t.prototype,"assign",null),t}(t)}var pe=function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return o(n,e),n.prototype.valueOf=function(){var e=this.meta.snapshot;return delete e[t.META_FIELD],Object.assign({},e,{meta:{id:this.meta.id,type:this.meta.type}})},n.prototype.toString=function(){return JSON.stringify(this.valueOf(),null,2)},n}(le(fe(ce(T)))),de=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.defaultModel=pe,t}(le(H)),_e={ToMany:Y,ToOne:W,ToOneOrMany:Z};exports.Attribute=re,exports.Bucket=_e,exports.Collection=de,exports.Model=pe,exports.PureCollection=H,exports.PureModel=T,exports.View=G,exports.ViewAttribute=oe,exports.assignModel=C,exports.cloneModel=B,exports.commitModel=z,exports.getModelCollection=j,exports.getModelId=V,exports.getModelType=A,exports.getOriginalModel=function(e){var n=j(e),r=t.getMeta(e,s.OriginalId);if(r){if(!n)throw f("The model needs to be in a collection to be referenceable");return n.findOne(e,r)}throw f("The given model is not a clone.")},exports.getRef=p,exports.getRefId=function(e,n){var r=t.getMeta(e,"ref_"+n);return r?r.refValue:null},exports.initModelRef=g,exports.isAttributeDirty=q,exports.isCollection=X,exports.isModel=Q,exports.isView=function(e){return K(e,G)},exports.modelMapParse=L,exports.modelMapSerialize=E,exports.modelToDirtyJSON=function(e){var t=i({},N(e));return Object.keys(e).forEach((function(n){q(e,n)||delete t[n]})),t},exports.modelToJSON=N,exports.prop=ae,exports.revertModel=U,exports.updateModel=S,exports.updateModelId=function(e,r){n.runInAction((function(){var o=j(e),i=V(e),a=A(e);t.setMeta(e,s.IdField,r),o&&o.__changeModelId(i,r,a),function(e,r,o,i){var a=j(e);a&&a.getAllModels().forEach((function(e){(function(e,n){var r=t.getMeta(e,u.Fields,{},!0,!0);return Object.keys(r).filter((function(e){return r[e].referenceDef})).filter((function(e){return!r[e].referenceDef.property})).filter((function(e){return r[e].referenceDef.model===n}))})(e,i).map((function(n){return t.getMeta(e,"ref_"+n)})).filter(Boolean).forEach((function(e){if(n.isArrayLike(e.value)){var t=e.value.findIndex((function(e){return V(e)===o&&A(e)===i}));-1!==t&&(e.value[t]=r)}else e.value&&V(e.value)===o&&A(e.value)===i&&(e.value={id:r,type:i})}))}))}(e,r,i,a)}))},exports.view=se,exports.withActions=fe,exports.withMeta=ce,exports.withPatches=le; |
{ | ||
"name": "datx", | ||
"version": "2.0.0-beta.3", | ||
"version": "2.0.0-beta.4", | ||
"description": "A MobX data store", | ||
@@ -62,4 +62,4 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"datx-utils": "^2.0.0-beta.3" | ||
"datx-utils": "^2.0.0-beta.4" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
143668
2742
Updateddatx-utils@^2.0.0-beta.4