js-data
Version:
Robust, framework-agnostic in-memory data store.
10 lines • 71.7 kB
JavaScript
/*!
* js-data
* @version 3.0.6 - Homepage <http://www.js-data.io/>
* @author js-data project authors
* @copyright (c) 2014-2016 js-data project authors
* @license MIT <https://github.com/js-data/js-data/blob/master/LICENSE>
*
* @overview js-data is a framework-agnostic, datastore-agnostic ORM/ODM for Node.js and the Browser.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("js-data",["exports"],t):t(e.JSData={})}(this,function(e){"use strict";var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},b=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},f=function(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)},n="[object Number]",r="[object RegExp]",t=Object.prototype.toString,a=/^(.+)\.(.+)$/,s={400:function _(){return"expected: "+arguments[0]+", found: "+(arguments[2]?arguments[1]:o(arguments[1]))},404:function _(){return arguments[0]+" not found"}},c=function toStr(e){return t.call(e)},l=function isPlainObject(e){return!!e&&"object"===(void 0===e?"undefined":o(e))&&e.constructor===Object},O={Promise:Promise,_:function _(n,e){O.forOwn(e,function(e,t){t&&void 0===n[t]&&!O.isFunction(e)&&0!==t.indexOf("_")&&(n[t]=e)})},_forRelation:function _forRelation(e,t,n,i){var r=t.relation,o=null,a=void 0;if(e||(e={}),e.with||(e.with=[]),0<=(a=O._getIndex(e.with,r))?o=r:0<=(a=O._getIndex(e.with,t.localField))&&(o=t.localField),e.withAll)n.call(i,t,{});else if(o){var s={};O.fillIn(s,t.getRelation()),O.fillIn(s,e),s.with=e.with.slice(),s._activeWith=s.with.splice(a,1)[0],s.with.forEach(function(e,t){e&&0===e.indexOf(o)&&e.length>=o.length&&"."===e[o.length]?s.with[t]=e.substr(o.length+1):s.with[t]=""}),n.call(i,t,s)}},_getIndex:function _getIndex(e,n){var i=-1;return e.forEach(function(e,t){return e===n?(i=t,!1):O.isObject(e)&&e.relation===n?(i=t,!1):void 0}),i},addHiddenPropsToTarget:function addHiddenPropsToTarget(e,n){var i={};Object.keys(n).forEach(function(e){var t=Object.getOwnPropertyDescriptor(n,e);t.enumerable=!1,i[e]=t}),Object.defineProperties(e,i)},areDifferent:function areDifferent(e,t,n){n||(n={});var i=O.diffObjects(e,t,n);return 0<Object.keys(i.added).length+Object.keys(i.removed).length+Object.keys(i.changed).length},classCallCheck:function classCallCheck$$1(e,t){if(!(e instanceof t))throw O.err(""+t.name)(500,"Cannot call a class as a function")},copy:function copy(e,n,t,i,r,o){if(n){if(e===n)throw O.err("utils.copy")(500,"Cannot copy! Source and destination are identical.");if(t=t||[],i=i||[],O.isObject(e)){var a=t.indexOf(e);if(-1!==a)return i[a];t.push(e),i.push(n)}var s=void 0;if(O.isArray(e)){var c=void 0;for(c=n.length=0;c<e.length;c++)s=O.copy(e[c],null,t,i,r,o),O.isObject(e[c])&&(t.push(e[c]),i.push(s)),n.push(s)}else for(var l in O.isArray(n)?n.length=0:O.forOwn(n,function(e,t){delete n[t]}),e)if(e.hasOwnProperty(l)){if(O.isBlacklisted(l,r))continue;s=O.copy(e[l],null,t,i,r,o),O.isObject(e[l])&&(t.push(e[l]),i.push(s)),n[l]=s}}else(n=e)&&(O.isArray(e)?n=O.copy(e,[],t,i,r,o):O.isDate(e)?n=new Date(e.getTime()):O.isRegExp(e)?(n=new RegExp(e.source,e.toString().match(/[^/]*$/)[0])).lastIndex=e.lastIndex:O.isObject(e)&&(n=o?O.copy(e,{},t,i,r,o):O.copy(e,Object.create(Object.getPrototypeOf(e)),t,i,r,o)));return n},deepFillIn:function deepFillIn(i,e){return e&&O.forOwn(e,function(e,t){var n=i[t];l(e)&&l(n)?O.deepFillIn(n,e):i.hasOwnProperty(t)&&void 0!==i[t]||(i[t]=e)}),i},deepMixIn:function deepMixIn(e,t){if(t)for(var n in t){var i=t[n],r=e[n];l(i)&&l(r)?O.deepMixIn(r,i):e[n]=i}return e},diffObjects:function diffObjects(i,r,e){e||(e={});var o=e.equalsFn,t=e.ignore,a={added:{},changed:{},removed:{}};O.isFunction(o)||(o=O.deepEqual);var n=Object.keys(i).filter(function(e){return!O.isBlacklisted(e,t)}),s=Object.keys(r).filter(function(e){return!O.isBlacklisted(e,t)});return n.forEach(function(e){var t=r[e],n=i[e];o(t,n)||(void 0===t?a.added[e]=n:a.changed[e]=n)}),s.forEach(function(e){var t=r[e];void 0===i[e]&&void 0!==t&&(a.removed[e]=void 0)}),a},equal:function equal(e,t){return e==t},err:function err(i,r){return function(e){var t="["+i+":"+r+"] ",n=s[e].apply(null,Array.prototype.slice.call(arguments,1));return n=""+t+n+"\nhttp://www.js-data.io/v3.0/docs/errors#"+e,new Error(n)}},eventify:function eventify(e,s,r){e=e||this;var t={};s||r||(s=function getter(){return t},r=function setter(e){t=e}),Object.defineProperties(e,{emit:{value:function value(){for(var e=s.call(this)||{},t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];var r=n.shift(),o=e[r]||[],a=void 0;for(a=0;a<o.length;a++)o[a].f.apply(o[a].c,n);for(o=e.all||[],n.unshift(r),a=0;a<o.length;a++)o[a].f.apply(o[a].c,n)}},off:{value:function value(e,t){var n=s.call(this)[e];if(n)if(t){for(var i=0;i<n.length;i++)if(n[i].f===t){n.splice(i,1);break}}else n.splice(0,n.length);else r.call(this,{})}},on:{value:function value(e,t,n){s.call(this)||r.call(this,{});var i=s.call(this);i[e]=i[e]||[],i[e].push({c:n,f:t})}}})},extend:function extend(e,t){var i=this,r=void 0;e||(e={}),t||(t={}),e.hasOwnProperty("constructor")?(r=e.constructor,delete e.constructor):r=function subClass(){O.classCallCheck(this,r);for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];i.apply(this,t)},r.prototype=Object.create(i&&i.prototype,{constructor:{configurable:!0,enumerable:!1,value:r,writable:!0}});var n=Object;return n.setPrototypeOf?n.setPrototypeOf(r,i):t.strictEs6Class?r.__proto__=i:O.forOwn(i,function(e,t){r[t]=e}),r.hasOwnProperty("__super__")||Object.defineProperty(r,"__super__",{configurable:!0,value:i}),O.addHiddenPropsToTarget(r.prototype,e),O.fillIn(r,t),r},fillIn:function fillIn(n,e){O.forOwn(e,function(e,t){n.hasOwnProperty(t)&&void 0!==n[t]||(n[t]=e)})},findIndex:function findIndex(e,n){var i=-1;return e&&e.forEach(function(e,t){if(n(e))return i=t,!1}),i},forEachRelation:function forEachRelation(e,t,n,i){var r=e.relationList||[];r.length&&r.forEach(function(e){O._forRelation(t,e,n,i)})},forOwn:function forOwn(e,t,n){var i=Object.keys(e),r=i.length,o=void 0;for(o=0;o<r&&!1!==t.call(n,e[i[o]],i[o],e);o++);},fromJson:function fromJson(e){return O.isString(e)?JSON.parse(e):e},get:function get$$1(e,t){if(t){for(var n=t.split("."),i=n.pop();t=n.shift();)if(null==(e=e[t]))return;return e[i]}},getSuper:function getSuper(e,t){var n=t?e:e.constructor;return n.hasOwnProperty("__super__")?n.__super__:Object.getPrototypeOf(n)||n.__proto__},intersection:function intersection(e,t){if(!e||!t)return[];e=Array.isArray(e)?e:[e],t=Array.isArray(t)?t:[t];var n=[],i=void 0,r=void 0,o=e.length;for(r=0;r<o;r++)i=e[r],-1===n.indexOf(i)&&-1!==t.indexOf(i)&&n.push(i);return n},isArray:Array.isArray,isBlacklisted:function isBlacklisted(e,t){if(!t||!t.length)return!1;for(var n=void 0,i=0;i<t.length;i++)if(c(t[i])===r&&t[i].test(e)||t[i]===e)return!!(n=e);return!!n},isBoolean:function isBoolean(e){return"[object Boolean]"===c(e)},isDate:function isDate(e){return e&&"object"===(void 0===e?"undefined":o(e))&&"[object Date]"===c(e)},isFunction:function isFunction(e){return"function"==typeof e||e&&"[object Function]"===c(e)},isInteger:function isInteger(e){return c(e)===n&&e==function toInteger(e){if(!e)return 0;if((e=+e)==1/0||e===-1/0)return 17976931348623157e292*(e<0?-1:1);var t=e%1;return e==e?t?e-t:e:0}(e)},isNull:function isNull(e){return null===e},isNumber:function isNumber(e){var t=void 0===e?"undefined":o(e);return"number"===t||e&&"object"===t&&c(e)===n},isObject:function isObject(e){return"[object Object]"===c(e)},isRegExp:function isRegExp(e){return c(e)===r},isSorN:function isSorN(e){return O.isString(e)||O.isNumber(e)},isString:function isString(e){return"string"==typeof e||e&&"object"===(void 0===e?"undefined":o(e))&&"[object String]"===c(e)},isUndefined:function isUndefined(e){return void 0===e},logify:function logify(e){O.addHiddenPropsToTarget(e,{dbg:function dbg(){if(O.isFunction(this.log)){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];this.log.apply(this,["debug"].concat(f(t)))}},log:function log(e){for(var t=arguments.length,n=Array(1<t?t-1:0),i=1;i<t;i++)n[i-1]=arguments[i];if(e&&!n.length&&(n.push(e),e="debug"),"debug"!==e||this.debug){var r,o,a=e.toUpperCase()+": ("+(this.name||this.constructor.name)+")";if(O.isFunction(console[e]))(r=console)[e].apply(r,[a].concat(f(n)));else(o=console).log.apply(o,[a].concat(f(n)))}}})},noDupeAdd:function noDupeAdd(e,t,n){e&&(this.findIndex(e,n)<0&&e.push(t))},omit:function omit(e,n){var i={};return O.forOwn(e,function(e,t){-1===n.indexOf(t)&&(i[t]=e)}),i},pick:function pick(n,e){return e.reduce(function(e,t){return e[t]=n[t],e},{})},plainCopy:function plainCopy(e){return O.copy(e,void 0,void 0,void 0,void 0,!0)},reject:function reject(e){return O.Promise.reject(e)},remove:function remove(e,t){if(e&&e.length){var n=this.findIndex(e,t);0<=n&&e.splice(n,1)}},resolve:function resolve(e){return O.Promise.resolve(e)},set:function set$$1(n,e,t){if(O.isObject(e))O.forOwn(e,function(e,t){O.set(n,t,e)});else{var i=a.exec(e);i?function mkdirP(t,e){return e&&e.split(".").forEach(function(e){t[e]||(t[e]={}),t=t[e]}),t}(n,i[1])[i[2]]=t:n[e]=t}},deepEqual:function deepEqual(n,i){if(n===i)return!0;var r=!0;if(O.isArray(n)&&O.isArray(i)){if(n.length!==i.length)return!1;for(var e=n.length;e--;)if(!O.deepEqual(n[e],i[e]))return!1}else{if(!O.isObject(n)||!O.isObject(i))return!1;O.forOwn(n,function(e,t){if(!(r=O.deepEqual(e,i[t])))return!1}),r&&O.forOwn(i,function(e,t){if(!(r=O.deepEqual(e,n[t])))return!1})}return r},toJson:JSON.stringify,unset:function unset(e,t){for(var n=t.split("."),i=n.pop();t=n.shift();)if(null==(e=e[t]))return;e[i]=void 0}},x=function safeSetProp(e,t,n){e&&e._set?e._set("props."+t,n):O.set(e,t,n)},C=function safeSetLink(e,t,n){e&&e._set?e._set("links."+t,n):O.set(e,t,n)};function Settable(){var n={};Object.defineProperties(this,{_get:{value:function value(e){return O.get(n,e)}},_set:{value:function value(e,t){return O.set(n,e,t)}},_unset:{value:function value(e){return O.unset(n,e)}}})}function Component(e){Settable.call(this),e||(e={}),this.debug=!!e.hasOwnProperty("debug")&&!!e.debug,Object.defineProperty(this,"_listeners",{value:{},writable:!0})}Settable.extend=O.extend;var u=Settable.extend({constructor:Component});Component.extend=O.extend,O.logify(Component.prototype),O.eventify(Component.prototype,function(){return this._listeners},function(e){this._listeners=e});var d="Query",h="Index inaccessible after first operation",p={limit:"",offset:"",orderBy:"",skip:"",sort:"",where:""},i=/([.*+?^=!:${}()|[\]/\\])/g,v=/%/g,g=/_/g;var y=u.extend({constructor:function Query(e){O.classCallCheck(this,Query),this.collection=e,this.data=null},_applyWhereFromObject:function _applyWhereFromObject(e){var i=[],r=[],o=[];return O.forOwn(e,function(e,n){O.isObject(e)||(e={"==":e}),O.forOwn(e,function(e,t){i.push(n),r.push(t),o.push(e)})}),{fields:i,ops:r,predicates:o}},_applyWhereFromArray:function _applyWhereFromArray(r){var o=this,a=[];return r.forEach(function(e,t){if(!O.isString(e)){var n=r[t-1],i=(O.isArray(e)?o._applyWhereFromArray:o._applyWhereFromObject).call(o,e);"or"===n&&(i.isOr=!0),a.push(i)}}),a.isArray=!0,a},_testObjectGroup:function _testObjectGroup(e,t,n,i){var r=void 0,o=n.fields,a=n.ops,s=n.predicates,c=a.length;for(r=0;r<c;r++){var l=a[r],u="|"===l.charAt(0);l=u?l.substr(1):l;var d=this.evaluate(O.get(i,o[r]),l,s[r]);void 0!==d&&(e=t?d:u?e||d:e&&d),t=!1}return{keep:e,first:t}},_testArrayGroup:function _testArrayGroup(e,t,n,i){var r=void 0,o=n.length;for(r=0;r<o;r++){var a=n[r],s=(a.isArray?this._testArrayGroup:this._testObjectGroup).call(this,!0,!0,a,i);e=n[r-1]?a.isOr?e||s.keep:e&&s.keep:s.keep,t=s.first}return{keep:e,first:t}},between:function between(e,t,n){if(n||(n={}),this.data)throw O.err(d+"#between")(500,"Cannot access index");return this.data=this.collection.getIndex(n.index).between(e,t,n),this},compare:function compare(e,t,n,i){var r=e[t],o=O.get(n,r[0]),a=O.get(i,r[0]);if(o&&O.isString(o)&&(o=o.toUpperCase()),a&&O.isString(a)&&(a=a.toUpperCase()),void 0===n&&(n=null),void 0===i&&(i=null),"DESC"===r[1].toUpperCase()){var s=a;a=o,o=s}return o<a?-1:a<o?1:t<e.length-1?this.compare(e,t+1,n,i):0},evaluate:function evaluate(e,t,n){var i=this.constructor.ops;return i[t]?i[t](e,n):0===t.indexOf("like")?null!==this.like(n,t.substr(4)).exec(e):0===t.indexOf("notLike")?null===this.like(n,t.substr(7)).exec(e):void 0},filter:function filter(e,t){var n=this;if(e||(e={}),this.getData(),O.isObject(e)){var i={};(O.isObject(e.where)||O.isArray(e.where))&&(i=e.where),O.forOwn(e,function(e,t){t in p||t in i||(i[t]={"==":e})});var r=void 0;O.isObject(i)&&0!==Object.keys(i).length?r=this._applyWhereFromArray([i]):O.isArray(i)&&(r=this._applyWhereFromArray(i)),r&&(this.data=this.data.filter(function(e,t){return n._testArrayGroup(!0,!0,r,e).keep}));var o=e.orderBy||e.sort;if(O.isString(o)&&(o=[[o,"ASC"]]),O.isArray(o)||(o=null),o){o.forEach(function(e,t){O.isString(e)&&(o[t]=[e,"ASC"])}),this.data.sort(function(e,t){return n.compare(o,0,e,t)})}O.isNumber(e.skip)?this.skip(e.skip):O.isNumber(e.offset)&&this.skip(e.offset),O.isNumber(e.limit)&&this.limit(e.limit)}else O.isFunction(e)&&(this.data=this.data.filter(e,t));return this},forEach:function forEach(e,t){return this.getData().forEach(e,t),this},get:function get$$1(e,t){if(e||(e=[]),t||(t={}),this.data)throw O.err(d+"#get")(500,h);return e&&!O.isArray(e)&&(e=[e]),e.length?this.data=this.collection.getIndex(t.index).get(e):this.getData(),this},getAll:function getAll(){var t=this,e={};if(this.data)throw O.err(d+"#getAll")(500,h);for(var n=arguments.length,i=Array(n),r=0;r<n;r++)i[r]=arguments[r];if(!i.length||1===i.length&&O.isObject(i[0]))return this.getData(),this;i.length&&O.isObject(i[i.length-1])&&(e=i[i.length-1],i.pop());var o=this.collection.getIndex(e.index);return this.data=[],i.forEach(function(e){t.data=t.data.concat(o.get(e))}),this},getData:function getData(){return this.data||(this.data=this.collection.index.getAll()),this.data},like:function like(e,t){return new RegExp("^"+function escape(e){return e.replace(i,"\\$1")}(e).replace(v,".*").replace(g,".")+"$",t)},limit:function limit(e){if(!O.isNumber(e))throw O.err(d+"#limit","num")(400,"number",e);var t=this.getData();return this.data=t.slice(0,Math.min(t.length,e)),this},map:function map(e,t){return this.data=this.getData().map(e,t),this},mapCall:function mapCall(t){for(var e=arguments.length,n=Array(1<e?e-1:0),i=1;i<e;i++)n[i-1]=arguments[i];return this.data=this.getData().map(function(e){return e[t].apply(e,f(n))}),this},run:function run(){var e=this.data;return this.data=null,e},skip:function skip(e){if(!O.isNumber(e))throw O.err(d+"#skip","num")(400,"number",e);var t=this.getData();return e<t.length?this.data=t.slice(e):this.data=[],this}},{ops:{"=":function _(e,t){return e==t},"==":function _(e,t){return e==t},"===":function _(e,t){return e===t},"!=":function _(e,t){return e!=t},"!==":function _(e,t){return e!==t},">":function _(e,t){return t<e},">=":function _(e,t){return t<=e},"<":function _(e,t){return e<t},"<=":function _(e,t){return e<=t},isectEmpty:function isectEmpty(e,t){return!O.intersection(e||[],t||[]).length},isectNotEmpty:function isectNotEmpty(e,t){return O.intersection(e||[],t||[]).length},in:function _in(e,t){return-1!==t.indexOf(e)},notIn:function notIn(e,t){return-1===t.indexOf(e)},contains:function contains(e,t){return-1!==(e||[]).indexOf(t)},notContains:function notContains(e,t){return-1===(e||[]).indexOf(t)}}}),w="belongsTo",R="hasMany",k="hasOne";function Relation(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};O.classCallCheck(this,Relation),t.type=this.constructor.TYPE_NAME,this.validateOptions(e,t),"object"===(void 0===e?"undefined":o(e))&&Object.defineProperty(this,"relatedMapper",{value:e}),Object.defineProperty(this,"inverse",{writable:!0}),O.fillIn(this,t)}Relation.extend=O.extend,O.addHiddenPropsToTarget(Relation.prototype,{get canAutoAddLinks(){return void 0===this.add||!!this.add},get relatedCollection(){return this.mapper.datastore.getCollection(this.relation)},validateOptions:function validateOptions(e,t){var n="new Relation",i=t.localField;if(!i)throw O.err(n,"opts.localField")(400,"string",i);var r=t.foreignKey=t.foreignKey||t.localKey;if(!r&&(t.type===w||t.type===k))throw O.err(n,"opts.foreignKey")(400,"string",r);if(O.isString(e)){if(t.relation=e,!O.isFunction(t.getRelation))throw O.err(n,"opts.getRelation")(400,"function",t.getRelation)}else{if(!e)throw O.err(n,"related")(400,"Mapper or string",e);t.relation=e.name}},assignTo:function assignTo(e){this.name=e.name,Object.defineProperty(this,"mapper",{value:e}),e.relationList||Object.defineProperty(e,"relationList",{value:[]}),e.relationFields||Object.defineProperty(e,"relationFields",{value:[]}),e.relationList.push(this),e.relationFields.push(this.localField)},canFindLinkFor:function canFindLinkFor(){return!(!this.foreignKey&&!this.localKey)},getRelation:function getRelation(){return this.relatedMapper},getForeignKey:function getForeignKey(e){return O.get(e,this.mapper.idAttribute)},setForeignKey:function setForeignKey(e,t){e&&t&&this._setForeignKey(e,t)},_setForeignKey:function _setForeignKey(t,e){var n=this,i=this.mapper.idAttribute;O.isArray(e)||(e=[e]),e.forEach(function(e){O.set(e,n.foreignKey,O.get(t,i))})},getLocalField:function getLocalField(e){return O.get(e,this.localField)},setLocalField:function setLocalField(e,t){return O.set(e,this.localField,t)},getInverse:function getInverse(e){return this.inverse||this.findInverseRelation(e),this.inverse},findInverseRelation:function findInverseRelation(t){var n=this;this.getRelation().relationList.forEach(function(e){if(e.getRelation()===t&&n.isInversedTo(e)&&n!==e)return n.inverse=e,!0})},isInversedTo:function isInversedTo(e){return!e.foreignKey||e.foreignKey===this.foreignKey},addLinkedRecords:function addLinkedRecords(e){var n=this,i=this.mapper.datastore;e.forEach(function(e){var t=n.getLocalField(e);O.isFunction(n.add)?t=n.add(i,n,e):t&&(t=n.linkRecord(e,t)),(!t||O.isArray(t)&&!t.length)&&n.canFindLinkFor(e)&&(t=n.findExistingLinksFor(e)),t&&n.setLocalField(e,t)})},removeLinkedRecords:function removeLinkedRecords(e,t){var n=this.localField;t.forEach(function(e){O.set(e,n,void 0)})},linkRecord:function linkRecord(e,t){var n=O.get(t,this.mapper.idAttribute);void 0===n?-1===this.relatedCollection.unsaved().indexOf(t)&&this.canAutoAddLinks&&(t=this.relatedCollection.add(t)):t!==this.relatedCollection.get(n)&&(this.setForeignKey(e,t),this.canAutoAddLinks&&(t=this.relatedCollection.add(t)));return t},findExistingLinksByForeignKey:function findExistingLinksByForeignKey(e){if(null!=e)return this.relatedCollection.filter(b({},this.foreignKey,e))},ensureLinkedDataHasProperType:function ensureLinkedDataHasProperType(e,t){var n=this.getRelation(),i=this.getLocalField(e);(!O.isArray(i)||i.length&&!n.is(i[0]))&&i&&!n.is(i)&&O.set(e,this.localField,n.createRecord(i,t))},isRequiresParentId:function isRequiresParentId(){return!1},isRequiresChildId:function isRequiresChildId(){return!1},createChildRecord:function createChildRecord(t,e,n){var i=this;return this.setForeignKey(t,e),this.createLinked(e,n).then(function(e){i.setLocalField(t,e)})},createLinked:function createLinked(e,t){var n=O.isArray(e)?"createMany":"create";return this.getRelation()[n](e,t)}}),[Relation.extend({getForeignKey:function getForeignKey(e){return O.get(e,this.foreignKey)},_setForeignKey:function _setForeignKey(e,t){O.set(e,this.foreignKey,O.get(t,this.getRelation().idAttribute))},findExistingLinksFor:function findExistingLinksFor(e){if(e){var t=O.get(e,this.foreignKey);return null!=t?this.relatedCollection.get(t):void 0}},isRequiresParentId:function isRequiresParentId(){return!0},createParentRecord:function createParentRecord(t,e){var n=this,i=this.getLocalField(t);return this.createLinked(i,e).then(function(e){n.setForeignKey(t,e)})},createChildRecord:function createChildRecord(){throw new Error('"BelongsTo" relation does not support child creation as it cannot have children.')}},{TYPE_NAME:"belongsTo"}),Relation.extend({validateOptions:function validateOptions(e,t){Relation.prototype.validateOptions.call(this,e,t);var n=t.localKeys,i=t.foreignKeys,r=t.foreignKey;if(!r&&!n&&!i)throw O.err("new Relation","opts.<foreignKey|localKeys|foreignKeys>")(400,"string",r)},canFindLinkFor:function canFindLinkFor(e){return!!(this.foreignKey||this.foreignKeys||this.localKeys&&O.get(e,this.localKeys))},linkRecord:function linkRecord(n,e){var i=this,r=this.relatedCollection,o=this.canAutoAddLinks,a=this.foreignKey,s=this.relatedCollection.unsaved();return e.map(function(e){var t=r.recordId(e);return(void 0===t&&-1===s.indexOf(e)||e!==r.get(t))&&(a&&i.setForeignKey(n,e),o&&(e=r.add(e))),e})},findExistingLinksFor:function findExistingLinksFor(e){var t=O.get(e,this.mapper.idAttribute),n=this.localKeys?O.get(e,this.localKeys):null,i=void 0;if(void 0!==t&&this.foreignKey?i=this.findExistingLinksByForeignKey(t):this.localKeys&&n?i=this.findExistingLinksByLocalKeys(n):void 0!==t&&this.foreignKeys&&(i=this.findExistingLinksByForeignKeys(t)),i&&i.length)return i},findExistingLinksByLocalKeys:function findExistingLinksByLocalKeys(e){return this.relatedCollection.filter({where:b({},this.relatedCollection.mapper.idAttribute,{in:e})})},findExistingLinksByForeignKeys:function findExistingLinksByForeignKeys(e){return this.relatedCollection.filter({where:b({},this.foreignKeys,{contains:e})})},isRequiresParentId:function isRequiresParentId(){return!!this.localKeys&&0<this.localKeys.length},isRequiresChildId:function isRequiresChildId(){return!!this.foreignKey},createParentRecord:function createParentRecord(t,e){var n=this,i=this.getLocalField(t),r=this.getRelation().idAttribute;return this.createLinked(i,e).then(function(e){O.set(t,n.localKeys,e.map(function(e){return O.get(e,r)}))})},createLinked:function createLinked(e,t){return this.getRelation().createMany(e,t)}},{TYPE_NAME:"hasMany"}),Relation.extend({findExistingLinksFor:function findExistingLinksFor(e,t){var n=O.get(t,e.idAttribute),i=this.findExistingLinksByForeignKey(n);if(i&&i.length)return i[0]},isRequiresChildId:function isRequiresChildId(){return!0}},{TYPE_NAME:"hasOne"})].forEach(function(n){Relation[n.TYPE_NAME]=function(e,t){return new n(e,t)}});var m=function belongsTo(t,n){return function(e){Relation.belongsTo(t,n).assignTo(e)}},A=function hasMany(t,n){return function(e){Relation.hasMany(t,n).assignTo(e)}},I=function hasOne(t,n){return function(e){Relation.hasOne(t,n).assignTo(e)}},F=function superMethod(i,r){var o=i.datastore;return o&&o[r]?function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return o[r].apply(o,[i.name].concat(t))}:i[r].bind(i)},E="creating",j="noValidate",S="keepChangeHistory",P="previous";function Record(e,t){O.classCallCheck(this,Record),Settable.call(this),e||(e={}),t||(t={});var n=this._set,i=this.constructor.mapper;n(E,!0),n(j,!!t.noValidate),n(S,void 0===t.keepChangeHistory?!i||i.keepChangeHistory:t.keepChangeHistory);var r=i?O.get(e,i.idAttribute):void 0;void 0!==r&&O.set(this,i.idAttribute,r),O.fillIn(this,e),n(E,!1),void 0!==t.validateOnSet?n(j,!t.validateOnSet):i&&void 0!==i.validateOnSet?n(j,!i.validateOnSet):n(j,!1),n(P,i?i.toJSON(e):O.plainCopy(e))}var L=u.extend({constructor:Record,_mapper:function _mapper(){var e=this.constructor.mapper;if(!e)throw O.err("Record#_mapper","")(404,"mapper");return e},afterLoadRelations:function afterLoadRelations(){},beforeLoadRelations:function beforeLoadRelations(){},changeHistory:function changeHistory(){return(this._get("history")||[]).slice()},changes:function changes(e){return e||(e={}),O.diffObjects("function"==typeof this.toJSON?this.toJSON(e):this,this._get("previous"),e)},commit:function commit(e){this._set("changed"),this._set("changing",!1),this._set("history",[]),this._set("previous",this.toJSON(e))},destroy:function destroy(e){e||(e={});var t=this._mapper();return F(t,"destroy")(O.get(this,t.idAttribute),e)},get:function get$$1(e){return O.get(this,e)},hasChanges:function hasChanges(e){return!!(this._get("changed")||[]).length||O.areDifferent("function"==typeof this.toJSON?this.toJSON(e):this,this._get("previous"),e)},isNew:function isNew(e){return void 0===O.get(this,this._mapper().idAttribute)},isValid:function isValid(e){return!this._mapper().validate(this,e)},removeInverseRelation:function removeInverseRelation(e,t,n,i){var r=this;if(n.type===k)C(e,n.localField,void 0);else if(n.type===R){var o=O.get(e,n.localField);void 0===t?O.remove(o,function(e){return e===r}):O.remove(o,function(e){return e===r||t===O.get(e,i)})}},setupInverseRelation:function setupInverseRelation(e,t,n,i){var r=this;if(n.type===k)C(e,n.localField,this);else if(n.type===R){var o=O.get(e,n.localField);void 0===t?O.noDupeAdd(o,this,function(e){return e===r}):O.noDupeAdd(o,this,function(e){return e===r||t===O.get(e,i)})}},loadRelations:function loadRelations(e,a){var s=this,t=void 0,c=this._mapper();return e||(e=[]),O.isString(e)&&(e=[e]),a||(a={}),a.with=e,O._(a,c),a.adapter=c.getAdapterName(a),t=a.op="beforeLoadRelations",O.resolve(this[t](e,a)).then(function(){t=a.op="loadRelations",c.dbg(t,s,e,a);var r=[],o=void 0;return O.forEachRelation(c,a,function(t,e){var n=t.getRelation();if(e.raw=!1,O.isFunction(t.load))o=t.load(c,t,s,a);else if("hasMany"===t.type||"hasOne"===t.type)t.foreignKey?o=F(n,"findAll")(b({},t.foreignKey,O.get(s,c.idAttribute)),e).then(function(e){return"hasOne"===t.type?e.length?e[0]:void 0:e}):t.localKeys?o=F(n,"findAll")({where:b({},n.idAttribute,{in:O.get(s,t.localKeys)})}):t.foreignKeys&&(o=F(n,"findAll")({where:b({},t.foreignKeys,{contains:O.get(s,c.idAttribute)})},a));else if("belongsTo"===t.type){var i=O.get(s,t.foreignKey);O.isSorN(i)&&(o=F(n,"find")(i,e))}o&&(o=o.then(function(e){t.setLocalField(s,e)}),r.push(o))}),Promise.all(r)}).then(function(){return t=a.op="afterLoadRelations",O.resolve(s[t](e,a)).then(function(){return s})})},previous:function previous(e){return e?this._get("previous."+e):this._get("previous")},revert:function revert(n){var i=this,r=this._get("previous");n||(n={}),n.preserve||(n.preserve=[]),O.forOwn(this,function(e,t){t!==i._mapper().idAttribute&&!r.hasOwnProperty(t)&&i.hasOwnProperty(t)&&-1===n.preserve.indexOf(t)&&delete i[t]}),O.forOwn(r,function(e,t){-1===n.preserve.indexOf(t)&&(i[t]=e)}),this.commit()},save:function save(n){var i=this;n||(n={});var e=this._mapper(),t=O.get(this,e.idAttribute),r=this,o=function postProcess(e){var t=n.raw?e.data:e;return t&&(O.deepMixIn(i,t),i.commit()),e};if(void 0===t)return F(e,"create")(r,n).then(o);if(n.changesOnly){var a=this.changes(n);r={},O.fillIn(r,a.added),O.fillIn(r,a.changed)}return F(e,"update")(t,r,n).then(o)},set:function set$$1(e,t,n){O.isObject(e)&&(n=t),n||(n={}),n.silent&&this._set("silent",!0),O.set(this,e,t),this._get("eventId")||this._set("silent")},toJSON:function toJSON(e){var t=this.constructor.mapper;if(t)return t.toJSON(this,e);var n={};return O.forOwn(this,function(e,t){n[t]=O.plainCopy(e)}),n},unset:function unset(e,t){this.set(e,void 0,t)},validate:function validate(e){return this._mapper().validate(this,e)}},{creatingPath:E,noValidatePath:j,keepChangeHistoryPath:S,previousPath:P});function insertAt(e,t,n){return e.splice(t,0,n),e}function removeAt(e,t){return e.splice(t,1),e}function binarySearch(e,t,n){for(var i,r,o,a=0,s=e.length,c=void 0,l=void 0;a<s;){if(i=t,r=e[l=(a+s)/2|0],o=n,0===(c=i===r?0:(o&&(i=o(i),r=o(r)),null===i&&null===r||void 0===i&&void 0===r?-1:null==i?-1:null==r?1:i<r?-1:r<i?1:0)))return{found:!0,index:l};c<0?s=l:a=l+1}return{found:!1,index:s}}function Index(e,t){if(O.classCallCheck(this,Index),e||(e=[]),!O.isArray(e))throw new Error("fieldList must be an array.");t||(t={}),this.fieldList=e,this.fieldGetter=t.fieldGetter,this.hashCode=t.hashCode,this.isIndex=!0,this.keys=[],this.values=[]}O.eventify(Record.prototype,function(){return this._get("events")},function(e){this._set("events",e)}),O.addHiddenPropsToTarget(Index.prototype,{set:function set(e,t){O.isArray(e)||(e=[e]);var n=e.shift()||void 0,i=binarySearch(this.keys,n);if(0===e.length)if(i.found){var r=binarySearch(this.values[i.index],t,this.hashCode);r.found||insertAt(this.values[i.index],r.index,t)}else insertAt(this.keys,i.index,n),insertAt(this.values,i.index,[t]);else if(i.found)this.values[i.index].set(e,t);else{insertAt(this.keys,i.index,n);var o=new Index([],{hashCode:this.hashCode});o.set(e,t),insertAt(this.values,i.index,o)}},get:function get(e){O.isArray(e)||(e=[e]);var t=e.shift()||void 0,n=binarySearch(this.keys,t);return 0===e.length?n.found?this.values[n.index].isIndex?this.values[n.index].getAll():this.values[n.index].slice():[]:n.found?this.values[n.index].get(e):[]},getAll:function getAll(e){e||(e={});var t=[],n=this.values;if("desc"===e.order)for(var i=n.length-1;0<=i;i--){var r=n[i];t=r.isIndex?t.concat(r.getAll(e)):t.concat(r)}else for(var o=0;o<n.length;o++){var a=n[o];t=a.isIndex?t.concat(a.getAll(e)):t.concat(a)}return t},visitAll:function visitAll(t,n){this.values.forEach(function(e){e.isIndex?e.visitAll(t,n):e.forEach(t,n)})},between:function between(e,t,n){n||(n={}),O.isArray(e)||(e=[e]),O.isArray(t)||(t=[t]),O.fillIn(n,{leftInclusive:!0,rightInclusive:!1,limit:void 0,offset:0});var i=this._between(e,t,n);return n.limit?i.slice(n.offset,n.limit+n.offset):i.slice(n.offset)},_between:function _between(e,t,n){var i=[],r=e.shift(),o=t.shift(),a=void 0;if(a=void 0!==r?binarySearch(this.keys,r):{found:!1,index:0},0===e.length){a.found&&!1===n.leftInclusive&&(a.index+=1);for(var s=a.index;s<this.keys.length;s+=1){if(void 0!==o)if(n.rightInclusive){if(this.keys[s]>o)break}else if(this.keys[s]>=o)break;if(i=this.values[s].isIndex?i.concat(this.values[s].getAll()):i.concat(this.values[s]),n.limit&&i.length>=n.limit+n.offset)break}}else for(var c=a.index;c<this.keys.length;c+=1){var l=this.keys[c];if(o<l)break;if(i=this.values[c].isIndex?l===r?i.concat(this.values[c]._between(O.copy(e),t.map(function(){}),n)):l===o?i.concat(this.values[c]._between(e.map(function(){}),O.copy(t),n)):i.concat(this.values[c].getAll()):i.concat(this.values[c]),n.limit&&i.length>=n.limit+n.offset)break}return n.limit?i.slice(0,n.limit+n.offset):i},peek:function peek(){return this.values.length?this.values[0].isIndex?this.values[0].peek():this.values[0]:[]},clear:function clear(){this.keys=[],this.values=[]},insertRecord:function insertRecord(t){var e=this.fieldList.map(function(e){return O.isFunction(e)?e(t)||void 0:t[e]||void 0});this.set(e,t)},removeRecord:function removeRecord(r){var o=this,a=void 0,s=void 0!==this.hashCode(r);return this.values.forEach(function(e,t){if(e.isIndex){if(e.removeRecord(r))return 0===e.keys.length&&(removeAt(o.keys,t),removeAt(o.values,t)),!(a=!0)}else{var n={};if(void 0!==o.keys[t]&&s)s&&(n=binarySearch(e,r,o.hashCode));else for(var i=e.length-1;0<=i;i--)if(e[i]===r){n={found:!0,index:i};break}if(n.found)return removeAt(e,n.index),0===e.length&&(removeAt(o.keys,t),removeAt(o.values,t)),!(a=!0)}}),a?r:void 0},updateRecord:function updateRecord(e){void 0!==this.removeRecord(e)&&this.insertRecord(e)}});var M=L.noValidatePath,K="Collection",N={commitOnMerge:!0,emitRecordEvents:!0,idAttribute:"id",onConflict:"merge"};var D=u.extend({constructor:function Collection(e,t){O.classCallCheck(this,Collection),u.call(this,t),e&&!O.isArray(e)&&(t=e,e=[]),O.isString(t)&&(t={idAttribute:t}),e||(e=[]),t||(t={}),Object.defineProperties(this,{mapper:{value:void 0,writable:!0},queryClass:{value:void 0,writable:!0}}),O.fillIn(this,t),O.fillIn(this,O.copy(N)),this.queryClass||(this.queryClass=y);var n=this.recordId();Object.defineProperties(this,{index:{value:new Index([n],{hashCode:function hashCode(e){return O.get(e,n)}})},indexes:{value:{}}}),(O.isObject(e)||O.isArray(e)&&e.length)&&this.add(e)},_onRecordEvent:function _onRecordEvent(){this.emitRecordEvents&&this.emit.apply(this,arguments)},add:function add(e,o){var a=this;o||(o={}),O._(o,this),e=this.beforeAdd(e,o)||e;var t=!1,s=this.recordId();if(!O.isArray(e)){if(!O.isObject(e))throw O.err(K+"#add","records")(400,"object or array",e);e=[e],t=!0}e=e.map(function(n){var e=a.recordId(n),i=void 0===e?e:a.get(e);if(n===i)return i;if(i){var t=o.onConflict||a.onConflict;if("merge"!==t&&"replace"!==t&&"skip"!==t)throw O.err(K+"#add","opts.onConflict")(400,"one of (merge, replace, skip)",t,!0);var r=i._get(M);o.noValidate&&i._set(M,!0),"merge"===t?O.deepMixIn(i,n):"replace"===t&&(O.forOwn(i,function(e,t){t!==s&&void 0===n[t]&&(i[t]=void 0)}),i.set(n)),o.noValidate&&i._set(M,r),n=i,o.commitOnMerge&&O.isFunction(n.commit)&&n.commit(),a.updateIndexes(n)}else n=a.mapper?a.mapper.createRecord(n,o):n,a.index.insertRecord(n),O.forOwn(a.indexes,function(e,t){e.insertRecord(n)}),n&&O.isFunction(n.on)&&n.on("all",a._onRecordEvent,a);return n});var n=t?e[0]:e;return o.silent||this.emit("add",n),this.afterAdd(e,o,n)||n},afterAdd:function afterAdd(){},afterRemove:function afterRemove(){},afterRemoveAll:function afterRemoveAll(){},beforeAdd:function beforeAdd(){},beforeRemove:function beforeRemove(){},beforeRemoveAll:function beforeRemoveAll(){},between:function between(e,t,n){return this.query().between(e,t,n).run()},createIndex:function createIndex(e,t,n){var i=this;O.isString(e)&&void 0===t&&(t=[e]),n||(n={}),n.hashCode||(n.hashCode=function(e){return i.recordId(e)});var r=this.indexes[e]=new Index(t,n);this.index.visitAll(r.insertRecord,r)},filter:function filter(e,t){return this.query().filter(e,t).run()},forEach:function forEach(e,t){this.index.visitAll(e,t)},get:function get$$1(e){var t=void 0===e?[]:this.query().get(e).run();return t.length?t[0]:void 0},getAll:function getAll(){var e;return(e=this.query()).getAll.apply(e,arguments).run()},getIndex:function getIndex(e){var t=e?this.indexes[e]:this.index;if(!t)throw O.err(K+"#getIndex",e)(404,"index");return t},limit:function limit(e){return this.query().limit(e).run()},map:function map(t,n){var i=[];return this.index.visitAll(function(e){i.push(t.call(n,e))}),i},mapCall:function mapCall(t){for(var e=arguments.length,n=Array(1<e?e-1:0),i=1;i<e;i++)n[i-1]=arguments[i];var r=[];return this.index.visitAll(function(e){r.push(e[t].apply(e,f(n)))}),r},prune:function prune(e){return this.removeAll(this.unsaved(),e)},query:function query(){return new this.queryClass(this)},recordId:function recordId(e){return e?O.get(e,this.recordId()):this.mapper?this.mapper.idAttribute:this.idAttribute},reduce:function reduce(e,t){return this.getAll().reduce(e,t)},remove:function remove(e,t){t||(t={}),this.beforeRemove(e,t);var n=O.isSorN(e)?this.get(e):e;return O.isObject(n)&&(n=this.index.removeRecord(n))&&(O.forOwn(this.indexes,function(e,t){e.removeRecord(n)}),O.isFunction(n.off)&&n.off("all",this._onRecordEvent,this),t.silent||this.emit("remove",n)),this.afterRemove(e,t,n)||n},removeAll:function removeAll(e,t){var n=this;t||(t={}),this.beforeRemoveAll(e,t);var i=O.isArray(e)?e.slice():this.filter(e),r=O.plainCopy(t);return r.silent=!0,i=i.map(function(e){return n.remove(e,r)}).filter(function(e){return e}),t.silent||this.emit("remove",i),this.afterRemoveAll(e,t,i)||i},skip:function skip(e){return this.query().skip(e).run()},toJSON:function toJSON(e){return this.mapCall("toJSON",e)},unsaved:function unsaved(e){return this.index.get()},updateIndex:function updateIndex(e,t){t||(t={}),this.getIndex(t.index).updateRecord(e)},updateIndexes:function updateIndexes(n){this.index.updateRecord(n),O.forOwn(this.indexes,function(e,t){e.updateRecord(n)})}}),T={array:O.isArray,boolean:O.isBoolean,integer:O.isInteger,null:O.isNull,number:O.isNumber,object:O.isObject,string:O.isString},q=function segmentToString(e,t){var n="";return e&&(O.isNumber(e)?n+="["+e+"]":n+=t?"."+e:""+e),n},J=function makeError(e,t,n){return{expected:t,actual:""+e,path:function makePath(e){e||(e={});var t="";return(e.path||[]).forEach(function(e){t+=q(e,t)}),t+=q(e.prop,t)}(n)}},Q=function addError(e,t,n,i){i.push(J(e,t,n))},B=function maxLengthCommon(e,t,n,i){var r=n[e];if(t.length>r)return J(t.length,"length no more than "+r,i)},$=function minLengthCommon(e,t,n,i){var r=n[e];if(t.length<r)return J(t.length,"length no less than "+r,i)},H={allOf:function allOf(t,e,n){var i=[];return e.allOf.forEach(function(e){i=i.concat(X(t,e,n)||[])}),i.length?i:void 0},anyOf:function anyOf(n,e,i){var r=!1,o=[];return e.anyOf.forEach(function(e){var t=X(n,e,i);t?o=o.concat(t):r=!0}),r?void 0:o},dependencies:function dependencies(e,t,n){},enum:function _enum(t,e,n){var i=e.enum;if(-1===O.findIndex(i,function(e){return O.deepEqual(e,t)}))return J(t,"one of ("+i.join(", ")+")",n)},items:function items(e,t,n){n||(n={});for(var items=t.items,i=[],r=O.isArray(items),o=e.length,a=0;a<o;a++)r&&(items=t.items[a]),n.prop=a,i=i.concat(X(e[a],items,n)||[]);return i.length?i:void 0},maximum:function maximum(e,t,n){var maximum=t.maximum,i=t.exclusiveMaximum;if((void 0===e?"undefined":o(e))===(void 0===maximum?"undefined":o(maximum))&&!(i?e<maximum:e<=maximum))return J(e,i?"no more than nor equal to "+maximum:"no more than "+maximum,n)},maxItems:function maxItems(e,t,n){if(O.isArray(e))return B("maxItems",e,t,n)},maxLength:function maxLength(e,t,n){return B("maxLength",e,t,n)},maxProperties:function maxProperties(e,t,n){if(O.isObject(e)){var maxProperties=t.maxProperties,i=Object.keys(e).length;return maxProperties<i?J(i,"no more than "+maxProperties+" properties",n):void 0}},minimum:function minimum(e,t,n){var minimum=t.minimum,i=t.exclusiveMinimum;if((void 0===e?"undefined":o(e))===(void 0===minimum?"undefined":o(minimum))&&!(i?minimum<e:minimum<=e))return J(e,i?"no less than nor equal to "+minimum:"no less than "+minimum,n)},minItems:function minItems(e,t,n){if(O.isArray(e))return $("minItems",e,t,n)},minLength:function minLength(e,t,n){return $("minLength",e,t,n)},minProperties:function minProperties(e,t,n){if(O.isObject(e)){var minProperties=t.minProperties,i=Object.keys(e).length;return i<minProperties?J(i,"no more than "+minProperties+" properties",n):void 0}},multipleOf:function multipleOf(e,t,n){var multipleOf=t.multipleOf;if(O.isNumber(e)&&e/multipleOf%1!=0)return J(e,"multipleOf "+multipleOf,n)},not:function not(e,t,n){if(!X(e,t.not,n))return J("succeeded","should have failed",n)},oneOf:function oneOf(n,e,i){var r=!1,o=[];return e.oneOf.forEach(function(e){var t=X(n,e,i);if(t)o=o.concat(t);else{if(r)return o=[J("valid against more than one","valid against only one",i)],r=!1;r=!0}}),r?void 0:o},pattern:function pattern(e,t,n){var pattern=t.pattern;if(O.isString(e)&&!e.match(pattern))return J(e,pattern,n)},properties:function properties(r,e,o){if(o||(o={}),!O.isArray(r)){var t=void 0===e.additionalProperties||e.additionalProperties,a=[],properties=e.properties||{},n=e.patternProperties||{},s=[];O.forOwn(properties,function(e,t){o.prop=t,s=s.concat(X(r[t],e,o)||[]),a.push(t)});var c=O.omit(r,a);O.forOwn(n,function(n,i){O.forOwn(c,function(e,t){t.match(i)&&(o.prop=t,s=s.concat(X(r[t],n,o)||[]),a.push(t))})});var i=Object.keys(O.omit(r,a));if(!1===t){if(i.length){var l=o.prop;o.prop="",Q("extra fields: "+i.join(", "),"no extra fields",o,s),o.prop=l}}else O.isObject(t)&&i.forEach(function(e){o.prop=e,s=s.concat(X(r[e],t,o)||[])});return s.length?s:void 0}},required:function required(n,e,i){i||(i={});var required=e.required,r=[];return i.existingOnly||required.forEach(function(e){if(void 0===O.get(n,e)){var t=i.prop;i.prop=e,Q(void 0,"a value",i,r),i.prop=t}}),r.length?r:void 0},type:function type(t,n,i){var type=n.type,r=void 0;if(O.isString(type)&&(type=[type]),type.forEach(function(e){if(T[e](t,n,i))return r=e,!1}),!r)return J(null!=t?void 0===t?"undefined":o(t):""+t,"one of ("+type.join(", ")+")",i);var e=ie[r];return e?e(t,n,i):void 0},uniqueItems:function uniqueItems(e,t,n){if(e&&e.length&&t.uniqueItems){var i=void 0,r=void 0,o=void 0;for(r=e.length-1;0<r;r--)for(i=e[r],o=r-1;0<=o;o--)if(O.deepEqual(i,e[o]))return J(i,"no duplicates",n)}}},U=function runOps(e,t,n,i){var r=[];return e.forEach(function(e){void 0!==n[e]&&(r=r.concat(H[e](t,n,i)||[]))}),r.length?r:void 0},V=["enum","type","allOf","anyOf","oneOf","not"],G=["items","maxItems","minItems","uniqueItems"],W=["multipleOf","maximum","minimum"],Y=["maxProperties","minProperties","required","properties","dependencies"],z=["maxLength","minLength","pattern"],X=function _validate(e,t,n){var i=[];n||(n={}),n.ctx||(n.ctx={value:e,schema:t});var r=void 0,o=n.prop;if(void 0!==t){if(!O.isObject(t))throw O.err("Schema#validate")(500,'Invalid schema at path: "'+n.path+'"');return void 0===n.path&&(n.path=[]),void 0!==n.prop&&(r=!0,n.path.push(n.prop),n.prop=void 0),t.extends&&(i=O.isFunction(t.extends.validate)?i.concat(t.extends.validate(e,n)||[]):i.concat(_validate(e,t.extends,n)||[])),void 0===e?(!0!==t.required||n.existingOnly||Q(e,"a value",n,i),r&&(n.path.pop(),n.prop=o),i.length?i:void 0):(i=i.concat(function validateAny(e,t,n){return U(V,e,t,n)}(e,t,n)||[]),r&&(n.path.pop(),n.prop=o),i.length?i:void 0)}},Z="changing",ee="changed",te="history",ne="eventId",ie={array:function array(e,t,n){return U(G,e,t,n)},integer:function integer(e,t,n){return ie.numeric(e,t,n)},number:function number(e,t,n){return ie.numeric(e,t,n)},numeric:function numeric(e,t,n){return U(W,e,t,n)},object:function object(e,t,n){return U(Y,e,t,n)},string:function string(e,t,n){return U(z,e,t,n)}};var re=u.extend({constructor:function Schema(e){var i=this;e||(e={}),O.fillIn(this,e),"object"===this.type?(this.properties=this.properties||{},O.forOwn(this.properties,function(e,t){e instanceof Schema||(i.properties[t]=new Schema(e))})):"array"!==this.type||!this.items||this.items instanceof Schema||(this.items=new Schema(this.items)),!this.extends||this.extends instanceof Schema||(this.extends=new Schema(this.extends)),["allOf","anyOf","oneOf"].forEach(function(n){i[n]&&i[n].forEach(function(e,t){e instanceof Schema||(i[n][t]=new Schema(e))})})},apply:function apply(n,i){var r=this;i||(i={}),i.getter||(i.getter="_get"),i.setter||(i.setter="_set"),i.unsetter||(i.unsetter="_unset"),i.track||(i.track=this.track);var e=this.properties||{};O.forOwn(e,function(e,t){Object.defineProperty(n,t,r.makeDescriptor(t,e,i))})},applyDefaults:function applyDefaults(i){if(i){var e=this.properties||{},r=O.isFunction(i.set)||O.isFunction(i._set);O.forOwn(e,function(e,t){if(e.hasOwnProperty("default")&&void 0===O.get(i,t)&&(r?i.set(t,O.plainCopy(e.default),{silent:!0}):O.set(i,t,O.plainCopy(e.default))),"object"===e.type&&e.properties){if(r){var n=i._get("noValidate");i._set("noValidate",!0),O.set(i,t,O.get(i,t)||{},{silent:!0}),i._set("noValidate",n)}else O.set(i,t,O.get(i,t)||{});e.applyDefaults(O.get(i,t))}})}},makeDescriptor:function makeDescriptor(f,h,e){var t={configurable:!0,enumerable:void 0===h.enumerable||!!h.enumerable},p="props."+f,v="previous."+f,g=e.getter,y=e.setter,m=e.unsetter,A=O.isBoolean(e.track)?e.track:h.track;if(t.get=function(){return this._get(p)},O.isFunction(h.get)){var n=t.get;t.get=function(){return h.get.call(this,n)}}if(t.set=function(r){var o=this,a=this[g],s=this[y],c=this[m];if(!a("noValidate")){var e=h.validate(r,{path:[f]});if(e){var t=new Error("validation failed");throw t.errors=e,t}}if(A&&!a("creating")){var n=a(v),l=a(p),i=a(Z),u=a(ee);i||(u=[]);var d=u.indexOf(f);l!==r&&-1===d&&u.push(f),n===r&&0<=d&&u.splice(d,1),u.length||(i=!1,c(Z),c(ee),a(ne)&&(clearTimeout(a(ne)),c(ne))),!i&&u.length&&(s(ee,u),s(Z,!0),s(ne,setTimeout(function(){if(c(ee),c(ne),c(Z),!a("silent")){var e=void 0;for(e=0;e<u.length;e++)o.emit("change:"+u[e],o,O.get(o,u[e]));var t=O.diffObjects(b({},f,r),b({},f,l));if(a("keepChangeHistory")){var n=O.plainCopy(t);n.timestamp=(new Date).getTime();var i=a(te);!i&&s(te,i=[]),i.push(n)}o.emit("change",o,t)}c("silent")},0)))}return s(p,r),r},O.isFunction(h.set)){var i=t.set;t.set=function(e){return h.set.call(this,e,i)}}return t},pick:function pick(n){var i=this;if(void 0!==n){if("object"===this.type){var r={},e=this.properties;if(e&&O.forOwn(e,function(e,t){r[t]=e.pick(n[t])}),this.extends&&O.fillIn(r,this.extends.pick(n)),this.additionalProperties)for(var t in n)e[t]||(r[t]=O.plainCopy(n[t]));return r}return"array"===this.type?n.map(function(e){var t=i.items?i.items.pick(e):{};return i.extends&&O.fillIn(t,i.extends.pick(e)),t}):O.plainCopy(n)}},validate:function validate(e,t){return X(e,this,t)}},{ANY_OPS:V,ARRAY_OPS:G,NUMERIC_OPS:W,OBJECT_OPS:Y,STRING_OPS:z,typeGroupValidators:ie,types:T,validate:X,validationKeywords:H}),oe="Mapper",ae=["beforeCreate","beforeCreateMany"],se=["beforeCreate","beforeCreateMany","beforeUpdate","beforeUpdateAll","beforeUpdateMany"],ce=function makeNotify(d){return function(){for(var e=this,t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];var r=n[n.length-d],o=r.op;if(this.dbg.apply(this,[o].concat(n)),-1!==ae.indexOf(o)&&!1!==r.applyDefaults){var a=this.getSchema();if(a&&a.applyDefaults){var s=n[0];O.isArray(s)||(s=[s]),s.forEach(function(e){a.applyDefaults(e)})}}if(-1!==se.indexOf(o)&&!r.noValidate){var c=r.existingOnly;0===o.indexOf("beforeUpdate")&&void 0===r.existingOnly&&(r.existingOnly=!0);var l=this.validate(n["beforeUpdate"===o?1:0],O.pick(r,["existingOnly"]));if(r.existingOnly=c,l){var u=new Error("validation failed");return u.errors=l,O.reject(u)}}(r.notify||void 0===r.notify&&this.notify)&&setTimeout(function(){e.emit.apply(e,[o].concat(n))})}},le=ce(1),ue=ce(2),de={count:{defaults:[{},{}],skip:!0,types:[]},destroy:{defaults:[{},{}],skip:!0,types:[]},destroyAll:{defaults:[{},{}],skip:!0,types:[]},find:{defaults:[void 0,{}],types:[]},findAll:{defaults:[{},{}],types:[]},sum:{defaults:[void 0,{},{}],skip:!0,types:[]},update:{adapterArgs:function adapterArgs(e,t,n,i){return[t,e.toJSON(n,i),i]},beforeAssign:1,defaults:[void 0,{},{}],types:[]},updateAll:{adapterArgs:function adapterArgs(e,t,n,i){return[e.toJSON(t,i),n,i]},beforeAssign:0,defaults:[{},{},{}],types:[]},updateMany:{adapterArgs:function adapterArgs(t,e,n){return[e.map(function(e){return t.toJSON(e,n)}),n]},beforeAssign:0,defaults:[[],{}],types:[]}},fe={_adapters:{},applyDefaults:!0,applySchema:!0,defaultAdapter:"http",idAttribute:"id",keepChangeHistory:!0,notify:!0,noValidate:!1,raw:!1,validateOnSet:!0};var he=u.extend({constructor:function Mapper(e){if(O.classCallCheck(this,Mapper),u.call(this),e||(e={}),Object.defineProperties(this,{_adapters:{value:void 0,writable:!0},datastore:{value:void 0,writable:!0},lifecycleMethods:{value:de},recordClass:{value:void 0,writable:!0},schema:{value:void 0,writable:!0}}),O.fillIn(this,e),O.fillIn(this,O.copy(fe)),!this.name)throw O.err("new "+oe,"opts.name")(400,"string",this.name);if(this.schema&&(this.schema.type||(this.schema.type="object"),this.schema instanceof re||(this.schema=new re(this.schema||{type:"object"}))),void 0===this.recordClass){var i=L;this.recordClass=i.extend({constructor:function Record(){var n=function Record(e,t){O.classCallCheck(this,n),i.call(this,e,t)};return n}()})}this.recordClass&&(this.recordClass.mapper=this,O.isObject(this.methods)&&O.addHiddenPropsToTarget(this.recordClass.prototype,this.methods),L.prototype.isPrototypeOf(Object.create(this.recordClass.prototype))&&this.schema&&this.schema.apply&&this.applySchema&&this.schema.apply(this.recordClass.prototype))},afterCount:ue,afterCreate:ue,afterCreateMany:ue,afterDestroy:ue,afterDestroyAll:ue,afterFind:ue,afterFindAll:ue,afterSum:ue,afterUpdate:ue,afterUpdateAll:ue,afterUpdateMany:ue,beforeCreate:le,beforeCreateMany:le,beforeCount:le,beforeDestroy:le,beforeDestroyAll:le,beforeFind:le,beforeFindAll:le,beforeSum:le,beforeUpdate:le,beforeUpdateAll:le,beforeUpdateMany:le,_end:function _end(e,t,n){if(t.raw&&O._(e,t),n)return e;var i=t.raw?e.data:e;return i&&O.isFunction(this.wrap)&&(i=this.wrap(i,t),t.raw?e.data=i:e=i),e},belongsTo:function belongsTo$$1(e,t){return m(e,t)(this)},count:function count(e,t){return this.crud("count",e,t)},create:function create(n,i){var r=this;n||(n={}),i||(i={});var t=n,o={},a={};return O._(i,this),i.adapter=this.getAdapterName(i),i.op="beforeCreate",this._runHook(i.op,n,i).then(function(e){return n=void 0!==e?e:n,i.with||(i.with=[]),r._createParentRecordIfRequired(n,i)}).then(function(e){o=e}).then(function(){return i.op="create",r._invokeAdapterMethod(i.op,n,i)}).then(function(e){a=e}).then(function(){var e=i.raw?a.data:a;return r._createOrAssignChildRecordIfRequired(e,{opts:i,parentRelationMap:o,originalProps:n})}).then(function(e){return r._commitChanges(t,e)}).then(function(e){i.raw?a.data=e:a=e;var t=r._end(a,i);return i.op="afterCreate",r._runHook(i.op,n,i,t)})},_commitChanges:function _commitChanges(e,n){var i=this;return O.isArray(e)?e.map(function(e,t){return i._commitChanges(e,n[t])}):(O.set(e,n,{silent:!0}),O.isFunction(e.commit)&&e.commit(),e)},createInstance:function createInstance(e,t){return this.createRecord(e,t)},_createParentRecordIfRequired:function _createParentRecordIfRequired(n,e){var i=[],r=[];return O.forEachRelation(this,e,function(e,t){e.isRequiresParentId()&&e.getLocalField(n)&&(t.raw=!1,r.push(e),i.push(e.createParentRecord(n,t)))}),O.Promise.all(i).then(function(i){return r.reduce(function(e,t,n){return t.setLocalField(e,i[n]),e},{})})},_createOrAssignChildRecordIfRequired:function _createOrAssignChildRecordIfRequired(r,o){var a=[];return O.forEachRelation(this,o.opts,function(e,t){var n=e.getLocalField(o.originalProps);if(n)if(t.raw=!1,e.isRequiresChildId())a.push(e.createChildRecord(r,n,t));else if(e.isRequiresParentId()){var i=e.getLocalField(o.parentRelationMap);i&&e.setLocalField(r,i)}}),O.Promise.all(a).then(function(){return r})},createMany:function createMany(c,n){var l=this;c||(c=[]),n||(n={});var t=c,i=void 0;return O._(n,this),n.adapter=this.getAdapterName(n),n.op="beforeCreateMany",this._runHook(n.op,c,n).then(function(e){c=void 0!==e?e:c;var a={};n.with