Comparing version 3.0.0-beta.5 to 3.0.0-beta.6
@@ -0,1 +1,12 @@ | ||
##### 3.0.0-beta.6 - 16 May 2016 | ||
###### Breaking changes | ||
- beforeCreate, beforeCreateMany, beforeUpdate, beforeUpdateAll, and beforeUpdateMany | ||
now run `Mapper#validate` for you by default. | ||
###### Backwards compatible changes | ||
- Added `Container#as` and `DataStore#as` | ||
- Added some deprecation warnings | ||
- Added support for groupings in where clauses | ||
##### 3.0.0-beta.5 - 07 May 2016 | ||
@@ -2,0 +13,0 @@ |
@@ -55,3 +55,3 @@ export const version: { | ||
plainCopy(from: any): any | ||
possibleConstructorReturn(self: Object, call: Object | Function): Object | ||
possibleConstructorReturn(self: Object, call: Object|Function): Object | ||
reject(value: any): Promise<any> | ||
@@ -61,3 +61,3 @@ remove(array: any[], fn: Function): void | ||
set(object: Object, path: string, value?: any): void | ||
strictEqual(a: any, b: any): boolean | ||
deepEqual(a: any, b: any): boolean | ||
toJson(value: any, replacer?: Function, space?: number): string | ||
@@ -123,3 +123,3 @@ unset(object: Object, path: string): void | ||
afterFindAll(query: any, opts: any, result: any): any | ||
afterSum(field: string, query:any, opts: any, result: any): any | ||
afterSum(field: string, query: any, opts: any, result: any): any | ||
afterUpdate(id: string|number, opts: any, result: any): any | ||
@@ -164,2 +164,3 @@ afterUpdateAll(props: any, query: any, opts: any, result: any): any | ||
updateMany(records: any[], opts?: any): Promise<any> | ||
validate(records: any|any[], opts?: any): any | ||
} | ||
@@ -196,3 +197,3 @@ export class Collection extends Component { | ||
remove(id: string|number, opts?: any): any | ||
removeAll(query: any, opts?: any): void | any[] | ||
removeAll(query: any, opts?: any): void|any[] | ||
skip(num: number): any[] | ||
@@ -212,2 +213,3 @@ toJSON(opts?: any): any[] | ||
_mappers: Object | ||
as(name: string): Mapper | ||
constructor(opts?: any) | ||
@@ -229,2 +231,3 @@ createRecord(name: string, props?: any, opts?: any): Record | ||
linkRelations: boolean | ||
as(name: string): Mapper|LinkedCollection | ||
constructor(opts?: any) | ||
@@ -244,3 +247,3 @@ add(mapperName: string, records: any[]|any, opts?: any): any[]|any | ||
remove(mapperName: string, id: string|number, opts?: any): any | ||
removeAll(mapperName: string, query?: any, opts?: any): void | any[] | ||
removeAll(mapperName: string, query?: any, opts?: any): void|any[] | ||
toJson(mapperName: string, opts?: any): any[] | ||
@@ -247,0 +250,0 @@ _callSuper(method: string, ...args: any[]): any |
/*! | ||
* js-data | ||
* @version 3.0.0-beta.5 - Homepage <http://www.js-data.io/> | ||
* @version 3.0.0-beta.6 - Homepage <http://www.js-data.io/> | ||
* @author js-data project authors | ||
@@ -10,4 +10,4 @@ * @copyright (c) 2014-2016 js-data project authors | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("js-data",["exports"],t):t(e.JSData=e.JSData||{})}(this,function(e){"use strict";function t(){Object.defineProperty(this,"_listeners",{value:{}})}function n(e,t,n){return e===t?0:(n&&(e=n(e),t=n(t)),null===e&&null===t?0:null===e?-1:null===t?1:t>e?-1:e>t?1:0)}function r(e,t,n){return e.splice(t,0,n),e}function i(e,t){return e.splice(t,1),e}function o(e,t,r){for(var i=0,o=e.length,a=void 0,u=void 0;o>i;){if(u=(i+o)/2|0,a=n(t,e[u],r),0===a)return{found:!0,index:u};0>a?o=u:i=u+1}return{found:!1,index:o}}function a(e,t){if(j.classCallCheck(this,a),e||(e=[]),!j.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=[]}function u(e,t){var n=this,r="new "+q;t||(t={});var i=t.localField;if(!i)throw j.err(r,"opts.localField")(400,"string",i);var o=t.foreignKey=t.foreignKey||t.localKey;if(!o&&(t.type===D||t.type===J))throw j.err(r,"opts.foreignKey")(400,"string",o);var a=t.localKeys,u=t.foreignKeys;if(!o&&!a&&!u&&t.type===T)throw j.err(r,"opts.<foreignKey|localKeys|foreignKeys>")(400,"string",o);if(j.isString(e)){if(t.relation=e,!j.isFunction(t.getRelation))throw j.err(r,"opts.getRelation")(400,"function",t.getRelation)}else{if(!e)throw j.err(r,"related")(400,"Mapper or string",e);t.relation=e.name,Object.defineProperty(n,"relatedMapper",{value:e})}Object.defineProperty(n,"inverse",{value:void 0,writable:!0}),j.fillIn(n,t)}var s={};s.typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},s.defineProperty=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},s.toConsumableArray=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)};var c="utils",l=1/0,f=1.7976931348623157e308,d="[object Boolean]",p="[object Date]",h="[object Function]",v="[object Number]",g="[object Object]",y="[object RegExp]",m="[object String]",b=Object.prototype.toString,A=/^(.+)\.(.+)$/,_={400:function(){return"expected: "+arguments[0]+", found: "+(arguments[2]?arguments[1]:s.typeof(arguments[1]))},404:function(){return arguments[0]+" not found"}},w=function(e){if(!e)return 0;if(e=+e,e===l||e===-l){var t=0>e?-1:1;return t*f}var n=e%1;return e===e?n?e-n:e:0},O=function(e){return b.call(e)},x=function(e){return!!e&&"object"===("undefined"==typeof e?"undefined":s.typeof(e))&&e.constructor===Object},C=function(e,t){if(!t)return e;var n=t.split(".");return n.forEach(function(t){e[t]||(e[t]={}),e=e[t]}),e},I={Promise:Promise,_:function(e,t){I.forOwn(t,function(t,n){n&&I.isUndefined(e[n])&&!I.isFunction(t)&&0!==n.indexOf("_")&&(e[n]=t)})},_forRelation:function(e,t,n,r){var i=t.relation,o=null,a=void 0;if(e||(e={}),e.with||(e.with=[]),(a=I._getIndex(e.with,i))>=0?o=i:(a=I._getIndex(e.with,t.localField))>=0&&(o=t.localField),e.withAll)return void n.call(r,t,{});if(o){var u={};I.fillIn(u,t.getRelation()),I.fillIn(u,e),u.with=e.with.slice(),u._activeWith=u.with.splice(a,1)[0],u.with.forEach(function(e,t){e&&0===e.indexOf(o)&&e.length>=o.length&&"."===e[o.length]?u.with[t]=e.substr(o.length+1):u.with[t]=""}),n.call(r,t,u)}},_getIndex:function(e,t){var n=-1;return e.forEach(function(e,r){return e===t?(n=r,!1):I.isObject(e)&&e.relation===t?(n=r,!1):void 0}),n},addHiddenPropsToTarget:function(e,t){var n={};I.forOwn(t,function(e,t){n[t]={writable:!0,value:e}}),Object.defineProperties(e,n)},areDifferent:function(e,t,n){n||(n={});var r=I.diffObjects(e,t,n),i=Object.keys(r.added).length+Object.keys(r.removed).length+Object.keys(r.changed).length;return i>0},classCallCheck:function(e,t){if(!(e instanceof t))throw I.err(""+t.name)(500,"Cannot call a class as a function")},copy:function(e,t,n,r,i,o){if(t){if(e===t)throw I.err(c+".copy")(500,"Cannot copy! Source and destination are identical.");if(n=n||[],r=r||[],I.isObject(e)){var a=n.indexOf(e);if(-1!==a)return r[a];n.push(e),r.push(t)}var u=void 0;if(I.isArray(e)){var s=void 0;for(t.length=0,s=0;s<e.length;s++)u=I.copy(e[s],null,n,r,i,o),I.isObject(e[s])&&(n.push(e[s]),r.push(u)),t.push(u)}else{I.isArray(t)?t.length=0:I.forOwn(t,function(e,n){delete t[n]});for(var l in e)if(e.hasOwnProperty(l)){if(I.isBlacklisted(l,i))continue;u=I.copy(e[l],null,n,r,i,o),I.isObject(e[l])&&(n.push(e[l]),r.push(u)),t[l]=u}}}else t=e,e&&(I.isArray(e)?t=I.copy(e,[],n,r,i,o):I.isDate(e)?t=new Date(e.getTime()):I.isRegExp(e)?(t=new RegExp(e.source,e.toString().match(/[^\/]*$/)[0]),t.lastIndex=e.lastIndex):I.isObject(e)&&(t=o?I.copy(e,{},n,r,i,o):I.copy(e,Object.create(Object.getPrototypeOf(e)),n,r,i,o)));return t},deepFillIn:function(e,t){return t&&I.forOwn(t,function(t,n){var r=e[n];x(t)&&x(r)?I.deepFillIn(r,t):e.hasOwnProperty(n)&&void 0!==e[n]||(e[n]=t)}),e},deepMixIn:function(e,t){return t&&I.forOwn(t,function(t,n){var r=e[n];x(t)&&x(r)?I.deepMixIn(r,t):e[n]=t}),e},diffObjects:function(e,t,n){n||(n={});var r=n.equalsFn,i=n.ignore,o={added:{},changed:{},removed:{}};return I.isFunction(r)||(r=I.strictEqual),I.forOwn(t,function(t,n){var a=e[n];I.isBlacklisted(n,i)||r(a,t)||(I.isUndefined(a)?o.removed[n]=void 0:r(a,t)||(o.changed[n]=a))}),I.forOwn(e,function(e,n){I.isUndefined(t[n])&&!I.isBlacklisted(n,i)&&(o.added[n]=e)}),o},equal:function(e,t){return e==t},err:function(e,t){return function(n){var r="["+e+":"+t+"] ",i=_[n].apply(null,Array.prototype.slice.call(arguments,1));return i=""+r+i+"\nhttp://www.js-data.io/v3.0/docs/errors#"+n,new Error(i)}},eventify:function(e,t,n){e=e||this;var r={};t||n||(t=function(){return r},n=function(e){r=e}),Object.defineProperties(e,{emit:{value:function(){for(var e=t.call(this)||{},n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];var o=r.shift(),a=e[o]||[],u=void 0;for(u=0;u<a.length;u++)a[u].f.apply(a[u].c,r);for(a=e.all||[],r.unshift(o),u=0;u<a.length;u++)a[u].f.apply(a[u].c,r)}},off:{value:function(e,r){var i=t.call(this),o=i[e];if(o)if(r){for(var a=0;a<o.length;a++)if(o[a].f===r){o.splice(a,1);break}}else o.splice(0,o.length);else n.call(this,{})}},on:{value:function(e,r,i){t.call(this)||n.call(this,{});var o=t.call(this);o[e]=o[e]||[],o[e].push({c:i,f:r})}}})},extend:function(e,t){var n=this,r=void 0;e||(e={}),t||(t={}),e.hasOwnProperty("constructor")?(r=e.constructor,delete e.constructor):r=function(){I.classCallCheck(this,r);for(var e=arguments.length,t=Array(e),i=0;e>i;i++)t[i]=arguments[i];n.apply(this,t)},r.prototype=Object.create(n&&n.prototype,{constructor:{configurable:!0,enumerable:!1,value:r,writable:!0}});var i=Object;return i.setPrototypeOf?i.setPrototypeOf(r,n):t.strictEs6Class?r.__proto__=n:I.forOwn(n,function(e,t){r[t]=e}),Object.defineProperty(r,"__super__",{configurable:!0,value:n}),I.addHiddenPropsToTarget(r.prototype,e),I.fillIn(r,t),r},fillIn:function(e,t){return I.forOwn(t,function(t,n){e.hasOwnProperty(n)&&void 0!==e[n]||(e[n]=t)}),e},findIndex:function(e,t){var n=-1;return e?(e.forEach(function(e,r){return t(e)?(n=r,!1):void 0}),n):n},forEachRelation:function(e,t,n,r){var i=e.relationList||[];i.length&&i.forEach(function(e){I._forRelation(t,e,n,r)})},forOwn:function(e,t,n){var r=Object.keys(e),i=r.length,o=void 0;for(o=0;i>o;o++)t.call(n,e[r[o]],r[o],e)},fromJson:function(e){return I.isString(e)?JSON.parse(e):e},get:function(e,t){if(t){for(var n=t.split("."),r=n.pop();t=n.shift();)if(e=e[t],null==e)return;return e[r]}},getSuper:function(e,t){var n=t?e:e.constructor;return n.hasOwnProperty("__super__")?n.__super__:Object.getPrototypeOf(n)||n.__proto__},intersection:function(e,t){if(!e||!t)return[];var n=[],r=void 0,i=void 0,o=e.length;for(i=0;o>i;i++)r=e[i],-1===n.indexOf(r)&&-1!==t.indexOf(r)&&n.push(r);return n},isArray:Array.isArray,isBlacklisted:function(e,t){if(!t||!t.length)return!1;for(var n=void 0,r=0;r<t.length;r++)if(O(t[r])===y&&t[r].test(e)||t[r]===e)return n=e;return!!n},isBoolean:function(e){return O(e)===d},isBrowser:!1,isDate:function(e){return e&&"object"===("undefined"==typeof e?"undefined":s.typeof(e))&&O(e)===p},isFunction:function(e){return"function"==typeof e||e&&O(e)===h},isInteger:function(e){return O(e)===v&&e==w(e)},isNull:function(e){return null===e},isNumber:function(e){var t="undefined"==typeof e?"undefined":s.typeof(e);return"number"===t||e&&"object"===t&&O(e)===v},isObject:function(e){return O(e)===g},isRegExp:function(e){return O(e)===y},isSorN:function(e){return I.isString(e)||I.isNumber(e)},isString:function(e){return"string"==typeof e||e&&"object"===("undefined"==typeof e?"undefined":s.typeof(e))&&O(e)===m},isUndefined:function(e){return void 0===e},logify:function(e){I.addHiddenPropsToTarget(e,{dbg:function(){for(var e=arguments.length,t=Array(e),n=0;e>n;n++)t[n]=arguments[n];this.log.apply(this,["debug"].concat(t))},log:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];if(e&&!n.length&&(n.push(e),e="debug"),"debug"!==e||this.debug){var i=e.toUpperCase()+": ("+(this.name||this.constructor.name)+")";if(console[e]){var o;(o=console)[e].apply(o,[i].concat(n))}else{var a;(a=console).log.apply(a,[i].concat(n))}}}})},noDupeAdd:function(e,t,n){if(e){var r=this.findIndex(e,n);0>r&&e.push(t)}},omit:function(e,t){var n={};return I.forOwn(e,function(e,r){-1===t.indexOf(r)&&(n[r]=e)}),n},plainCopy:function(e){return I.copy(e,void 0,void 0,void 0,void 0,!0)},reject:function(e){return I.Promise.reject(e)},remove:function(e,t){if(e&&e.length){var n=this.findIndex(e,t);n>=0&&e.splice(n,1)}},resolve:function(e){return I.Promise.resolve(e)},set:function(e,t,n){if(I.isObject(t))I.forOwn(t,function(t,n){I.set(e,n,t)});else{var r=A.exec(t);r?C(e,r[1])[r[2]]=n:e[t]=n}},strictEqual:function(e,t){var n=e===t;return n||(I.isObject(e)&&I.isObject(t)?(I.forOwn(e,function(e,r){n=n&&I.strictEqual(e,t[r])}),I.forOwn(t,function(t,r){n=n&&I.strictEqual(t,e[r])})):I.isArray(e)&&I.isArray(t)&&e.forEach(function(e,r){n=n&&I.strictEqual(e,t[r])})),n},toJson:JSON.stringify,unset:function(e,t){for(var n=t.split("."),r=n.pop();t=n.shift();)if(e=e[t],null==e)return;e[r]=void 0}};try{I.isBrowser=!!window}catch(e){I.isBrowser=!1}var j=I;t.extend=j.extend,j.logify(t.prototype),j.eventify(t.prototype,function(){return this._listeners},function(e){this._listeners=e});var F="Query",R="Index inaccessible after first operation",S={limit:"",offset:"",orderBy:"",skip:"",sort:"",where:""},E=/([.*+?^=!:${}()|[\]\/\\])/g,k=/%/g,P=/_/g,M=function(e){return e.replace(E,"\\$1")},K=t.extend({constructor:function e(t){var n=this;j.classCallCheck(n,e),n.collection=t,n.data=null},between:function(e,t,n){var r=this;if(n||(n={}),r.data)throw j.err(F+"#between")(500,"Cannot access index");return r.data=r.collection.getIndex(n.index).between(e,t,n),r},compare:function(e,t,n,r){var i=e[t],o=j.get(n,i[0]),a=j.get(r,i[0]);if(o&&j.isString(o)&&(o=o.toUpperCase()),a&&j.isString(a)&&(a=a.toUpperCase()),void 0===n&&(n=null),void 0===r&&(r=null),"DESC"===i[1].toUpperCase()){var u=a;a=o,o=u}return a>o?-1:o>a?1:t<e.length-1?this.compare(e,t+1,n,r):0},evaluate:function(e,t,n){var r=this.constructor.ops;return r[t]?r[t](e,n):0===t.indexOf("like")?!j.isNull(this.like(n,t.substr(4)).exec(e)):0===t.indexOf("notLike")?j.isNull(this.like(n,t.substr(7)).exec(e)):void 0},filter:function(e,t){var n=this;return e||(e={}),n.getData(),j.isObject(e)?!function(){var t={};j.isObject(e.where)&&(t=e.where),j.forOwn(e,function(e,n){n in S||n in t||(t[n]={"==":e})});var r=[],i=[],o=[];j.forOwn(t,function(e,t){j.isObject(e)||(e={"==":e}),j.forOwn(e,function(e,n){r.push(t),i.push(n),o.push(e)})}),r.length&&!function(){var e=void 0,t=r.length;n.data=n.data.filter(function(a){var u=!0,s=!0;for(e=0;t>e;e++){var c=i[e],l="|"===c.charAt(0);c=l?c.substr(1):c;var f=n.evaluate(j.get(a,r[e]),c,o[e]);void 0!==f&&(s=u?f:l?s||f:s&&f),u=!1}return s})}();var a=e.orderBy||e.sort;j.isString(a)&&(a=[[a,"ASC"]]),j.isArray(a)||(a=null),a&&!function(){var e=0;a.forEach(function(e,t){j.isString(e)&&(a[t]=[e,"ASC"])}),n.data.sort(function(t,r){return n.compare(a,e,t,r)})}(),j.isNumber(e.skip)?n.skip(e.skip):j.isNumber(e.offset)&&n.skip(e.offset),j.isNumber(e.limit)&&n.limit(e.limit)}():j.isFunction(e)&&(n.data=n.data.filter(e,t)),n},forEach:function(e,t){return this.getData().forEach(e,t),this},get:function(e,t){var n=this;if(e||(e=[]),t||(t={}),n.data)throw j.err(F+"#get")(500,R);return e&&!j.isArray(e)&&(e=[e]),e.length?(n.data=n.collection.getIndex(t.index).get(e),n):(n.getData(),n)},getAll:function(){var e=this,t={};if(e.data)throw j.err(F+"#getAll")(500,R);for(var n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];if(!r.length||1===r.length&&j.isObject(r[0]))return e.getData(),e;r.length&&j.isObject(r[r.length-1])&&(t=r[r.length-1],r.pop());var o=e.collection,a=o.getIndex(t.index);return e.data=[],r.forEach(function(t){e.data=e.data.concat(a.get(t))}),e},getData:function(){var e=this;return e.data||(e.data=e.collection.index.getAll()),e.data},like:function(e,t){return new RegExp("^"+M(e).replace(k,".*").replace(P,".")+"$",t)},limit:function(e){if(!j.isNumber(e))throw j.err(F+"#limit","num")(400,"number",e);var t=this.getData();return this.data=t.slice(0,Math.min(t.length,e)),this},map:function(e,t){return this.data=this.getData().map(e,t),this},mapCall:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];return this.data=this.getData().map(function(t){return t[e].apply(t,n)}),this},run:function(){var e=this.data;return this.data=null,e},skip:function(e){if(!j.isNumber(e))throw j.err(F+"#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 e>=t},"<":function(e,t){return t>e},"<=":function(e,t){return t>=e},isectEmpty:function(e,t){return!j.intersection(e||[],t||[]).length},isectNotEmpty:function(e,t){return j.intersection(e||[],t||[]).length},in:function(e,t){return-1!==t.indexOf(e)},notIn:function(e,t){return-1===t.indexOf(e)},contains:function(e,t){return-1!==(e||[]).indexOf(t)},notContains:function(e,t){return-1===(e||[]).indexOf(t)}}});j.addHiddenPropsToTarget(a.prototype,{set:function(e,t){j.isArray(e)||(e=[e]);var n=e.shift()||null,i=o(this.keys,n);if(0===e.length)if(i.found){var u=o(this.values[i.index],t,this.hashCode);u.found||r(this.values[i.index],u.index,t)}else r(this.keys,i.index,n),r(this.values,i.index,[t]);else if(i.found)this.values[i.index].set(e,t);else{r(this.keys,i.index,n);var s=new a([],{hashCode:this.hashCode});s.set(e,t),r(this.values,i.index,s)}},get:function(e){j.isArray(e)||(e=[e]);var t=e.shift()||null,n=o(this.keys,t);return 0===e.length?n.found?this.values[n.index].isIndex?this.values[n.index].getAll():this.values[n.index]:[]:n.found?this.values[n.index].get(e):[]},getAll:function(e){e||(e={});var t=[],n=this.values;if("desc"===e.order)for(var r=n.length-1;r>=0;r--){var i=n[r];t=i.isIndex?t.concat(i.getAll(e)):t.concat(i)}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(e,t){this.values.forEach(function(n){n.isIndex?n.visitAll(e,t):n.forEach(e,t)})},between:function(e,t,n){n||(n={}),j.isArray(e)||(e=[e]),j.isArray(t)||(t=[t]),j.fillIn(n,{leftInclusive:!0,rightInclusive:!1,limit:void 0,offset:0});var r=this._between(e,t,n);return n.limit?r.slice(n.offset,n.limit+n.offset):r.slice(n.offset)},_between:function(e,t,n){var r=[],i=e.shift(),a=t.shift(),u=void 0;if(u=void 0!==i?o(this.keys,i):{found:!1,index:0},0===e.length){u.found&&n.leftInclusive===!1&&(u.index+=1);for(var s=u.index;s<this.keys.length;s+=1){if(void 0!==a)if(n.rightInclusive){if(this.keys[s]>a)break}else if(this.keys[s]>=a)break;if(r=this.values[s].isIndex?r.concat(this.values[s].getAll()):r.concat(this.values[s]),n.limit&&r.length>=n.limit+n.offset)break}}else for(var c=u.index;c<this.keys.length;c+=1){var l=this.keys[c];if(l>a)break;if(r=this.values[c].isIndex?l===i?r.concat(this.values[c]._between(j.copy(e),t.map(function(){}),n)):l===a?r.concat(this.values[c]._between(e.map(function(){}),j.copy(t),n)):r.concat(this.values[c].getAll()):r.concat(this.values[c]),n.limit&&r.length>=n.limit+n.offset)break}return n.limit?r.slice(0,n.limit+n.offset):r},peek:function(){return this.values.length?this.values[0].isIndex?this.values[0].peek():this.values[0]:[]},clear:function(){this.keys=[],this.values=[]},insertRecord:function(e){var t=this.fieldList.map(function(t){return j.isFunction(t)?t(e)||null:e[t]||null});this.set(t,e)},removeRecord:function(e){var t=this,n=void 0;return this.values.forEach(function(r,a){if(r.isIndex){if(r.removeRecord(e))return 0===r.keys.length&&(i(t.keys,a),i(t.values,a)),n=!0,!1}else{var u=o(r,e,t.hashCode);if(u.found)return i(r,u.index),0===r.length&&(i(t.keys,a),i(t.values,a)),n=!0,!1}}),n?e:void 0},updateRecord:function(e){this.removeRecord(e),this.insertRecord(e)}});var L="Collection",N={idAttribute:"id",onConflict:"merge"},U=t.extend({constructor:function e(t,n){var r=this;j.classCallCheck(r,e),e.__super__.call(r),t&&!j.isArray(t)&&(n=t,t=[]),j.isString(n)&&(n={idAttribute:n}),t||(t=[]),n||(n={}),Object.defineProperties(r,{mapper:{value:void 0,writable:!0},queryClass:{value:void 0,writable:!0}}),j.fillIn(r,n),j.fillIn(r,j.copy(N)),r.queryClass||(r.queryClass=K);var i=r.recordId();Object.defineProperties(r,{index:{value:new a([i],{hashCode:function(e){return j.get(e,i)}})},indexes:{value:{}}}),t&&r.add(t)},_onRecordEvent:function(){this.emit.apply(this,arguments)},add:function(e,t){var n=this;t||(t={}),j._(t,n),e=n.beforeAdd(e,t)||e;var r=!1,i=n.recordId();if(!j.isArray(e)){if(!j.isObject(e))throw j.err(L+"#add","records")(400,"object or array",e);e=[e],r=!0}e=e.map(function(e){var r=n.recordId(e);if(!j.isSorN(r))throw j.err(L+"#add","record."+i)(400,"string or number",r);var o=n.get(r);if(e===o)return o;if(o){var a=t.onConflict||n.onConflict;if("merge"===a)j.deepMixIn(o,e);else{if("replace"!==a)throw j.err(L+"#add","opts.onConflict")(400,"one of (merge, replace)",a,!0);j.forOwn(o,function(t,n){n===i||e.hasOwnProperty(n)||delete o[n]}),o.set(e)}e=o,n.updateIndexes(e)}else e=n.mapper?n.mapper.createRecord(e,t):e,n.index.insertRecord(e),j.forOwn(n.indexes,function(t,n){t.insertRecord(e)}),e&&j.isFunction(e.on)&&e.on("all",n._onRecordEvent,n);return e});var o=r?e[0]:e;return n.emit("add",o),n.afterAdd(e,t,o)||o},afterAdd:function(){},afterRemove:function(){},afterRemoveAll:function(){},beforeAdd:function(){},beforeRemove:function(){},beforeRemoveAll:function(){},between:function(e,t,n){return this.query().between(e,t,n).run()},createIndex:function(e,t,n){var r=this;j.isString(e)&&void 0===t&&(t=[e]),n||(n={}),n.hashCode=n.hashCode||function(e){return r.recordId(e)};var i=r.indexes[e]=new a(t,n);return r.index.visitAll(i.insertRecord,i),r},filter:function(e,t){return this.query().filter(e,t).run()},forEach:function(e,t){this.index.visitAll(e,t)},get:function(e){var t=this.query().get(e).run();return t.length?t[0]:void 0},getAll:function(){var e;return(e=this.query()).getAll.apply(e,arguments).run()},getIndex:function(e){var t=e?this.indexes[e]:this.index;if(!t)throw j.err(L+"#getIndex",e)(404,"index");return t},limit:function(e){return this.query().limit(e).run()},map:function(e,t){var n=[];return this.index.visitAll(function(r){n.push(e.call(t,r))}),n},mapCall:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=[];return this.index.visitAll(function(t){i.push(t[e].apply(t,n))}),i},recordId:function(e){var t=this;return e?j.get(e,t.recordId()):t.mapper?t.mapper.idAttribute:t.idAttribute},query:function(){var e=this.queryClass;return new e(this)},reduce:function(e,t){var n=this.getAll();return n.reduce(e,t)},remove:function(e,t){var n=this;t||(t={}),n.beforeRemove(e,t);var r=n.get(e);return r&&(n.index.removeRecord(r),j.forOwn(n.indexes,function(e,t){e.removeRecord(r)}),r&&j.isFunction(r.off)&&(r.off("all",n._onRecordEvent,n),n.emit("remove",r))),n.afterRemove(e,t,r)||r},removeAll:function(e,t){var n=this;t||(t={}),n.beforeRemoveAll(e,t);var r=n.filter(e);return r.forEach(function(e){n.remove(n.recordId(e),t)}),n.afterRemoveAll(e,t,r)||r},skip:function(e){return this.query().skip(e).run()},toJSON:function(e){return this.mapCall("toJSON",e)},updateIndex:function(e,t){t||(t={}),this.getIndex(t.index).updateRecord(e)},updateIndexes:function(e){var t=this;t.index.updateRecord(e),j.forOwn(t.indexes,function(t,n){t.updateRecord(e)})}}),D="belongsTo",T="hasMany",J="hasOne",q="Relation";j.addHiddenPropsToTarget(u.prototype,{getRelation:function(){return this.relatedMapper},getForeignKey:function(e){return this.type===D?j.get(e,this.foreignKey):j.get(e,this.mapper.idAttribute)},setForeignKey:function(e,t){var n=this;e&&t&&(n.type===D?j.set(e,n.foreignKey,j.get(t,n.getRelation().idAttribute)):!function(){var r=n.mapper.idAttribute;j.isArray(t)?t.forEach(function(t){j.set(t,n.foreignKey,j.get(e,r))}):j.set(t,n.foreignKey,j.get(e,r))}())},getLocalField:function(e){return j.get(e,this.localField)},setLocalField:function(e,t){return j.set(e,this.localField,t)},getInverse:function(e){var t=this;return t.inverse?t.inverse:(t.getRelation().relationList.forEach(function(n){if(n.getRelation()===e){if(n.foreignKey&&n.foreignKey!==t.foreignKey)return;return t.inverse=n,!1}}),t.inverse)}});var Q=function(e,t,n){n.name=e.name;var r=new u(t,n);Object.defineProperty(r,"mapper",{value:e}),e.relationList||Object.defineProperty(e,"relationList",{value:[]}),e.relationFields||Object.defineProperty(e,"relationFields",{value:[]}),e.relationList.push(r),e.relationFields.push(r.localField)},B=function(e,t){return t||(t={}),t.type=D,function(n){Q(n,e,t)}},$=function(e,t){return t||(t={}),t.type=T,function(n){Q(n,e,t)}},V=function(e,t){return t||(t={}),t.type=J,function(n){Q(n,e,t)}},H="Record",G=function(e,t){var n=e.datastore;return n&&n[t]?function(){for(var r=arguments.length,i=Array(r),o=0;r>o;o++)i[o]=arguments[o];return n[t].apply(n,[e.name].concat(i))}:e[t].bind(e)},W=t.extend({constructor:function e(t,n){var r=this;j.classCallCheck(r,e),t||(t={}),n||(n={});var i={};Object.defineProperties(r,{_get:{value:function(e){return j.get(i,e)}},_set:{value:function(e,t){return j.set(i,e,t)}},_unset:{value:function(e){return j.unset(i,e)}}});var o=r._set;o("creating",!0),n.noValidate&&o("noValidate",!0),j.fillIn(r,t),o("creating",!1),o("noValidate",!1),o("previous",j.copy(t))},_mapper:function(){var e=this,t=e.constructor.mapper;if(!t)throw j.err(H+"#_mapper","")(404,"mapper");return t},afterLoadRelations:function(){},beforeLoadRelations:function(){},changes:function(e){var t=this;return e||(e={}),j.diffObjects("function"==typeof t.toJSON?t.toJSON(e):t,t._get("previous"),e)},commit:function(){var e=this;return e._set("changed"),e._set("previous",j.copy(e)),e},destroy:function(e){var t=this;e||(e={});var n=t._mapper();return G(n,"destroy")(j.get(t,n.idAttribute),e)},get:function(e){return j.get(this,e)},hasChanges:function(e){var t=this,n=!!(t._get("changed")||[]).length;return n||j.areDifferent("function"==typeof t.toJSON?t.toJSON(e):t,t._get("previous"),e)},hashCode:function(){var e=this;return j.get(e,e._mapper().idAttribute)},isValid:function(e){var t=this;return!t._mapper().validate(t,e)},loadRelations:function(e,t){var n=void 0,r=this,i=r._mapper();return e||(e=[]),j.isString(e)&&(e=[e]),t||(t={}),t.with=e,j._(t,i),t.adapter=i.getAdapterName(t),n=t.op="beforeLoadRelations",j.resolve(r[n](e,t)).then(function(){n=t.op="loadRelations",i.dbg(n,r,e,t);var o=[],a=void 0;return j.forEachRelation(i,t,function(e,n){var u=e.getRelation();if(n.raw=!1,j.isFunction(e.load))a=e.load(i,e,r,t);else if("hasMany"===e.type||"hasOne"===e.type)e.foreignKey?a=G(u,"findAll")(s.defineProperty({},e.foreignKey,j.get(r,i.idAttribute)),n).then(function(t){return"hasOne"===e.type?t.length?t[0]:void 0:t}):e.localKeys?a=G(u,"findAll")({where:s.defineProperty({},u.idAttribute,{in:j.get(r,e.localKeys)})}):e.foreignKeys&&(a=G(u,"findAll")({where:s.defineProperty({},e.foreignKeys,{contains:j.get(r,i.idAttribute)})},t));else if("belongsTo"===e.type){var c=j.get(r,e.foreignKey);j.isSorN(c)&&(a=G(u,"find")(c,n))}a&&(a=a.then(function(t){e.setLocalField(r,t)}),o.push(a))}),Promise.all(o)}).then(function(){return n=t.op="afterLoadRelations",j.resolve(r[n](e,t)).then(function(){return r})})},previous:function(e){var t=this;return e?t._get("previous."+e):t._get("previous")},revert:function(e){var t=this,n=t._get("previous");return e||(e={}),e.preserve||(e.preserve=[]),j.forOwn(t,function(r,i){i!==t._mapper().idAttribute&&!n.hasOwnProperty(i)&&t.hasOwnProperty(i)&&-1===e.preserve.indexOf(i)&&delete t[i]}),j.forOwn(n,function(n,r){-1===e.preserve.indexOf(r)&&(t[r]=n)}),t.commit(),t},save:function(e){var t=this;e||(e={});var n=t._mapper(),r=j.get(t,n.idAttribute),i=t;if(j.isUndefined(r))return G(n,"create")(i,e);if(e.changesOnly){var o=t.changes(e);i={},j.fillIn(i,o.added),j.fillIn(i,o.changed)}return G(n,"update")(r,i,e)},set:function(e,t,n){var r=this;j.isObject(e)&&(n=t),n||(n={}),n.silent&&r._set("silent",!0),j.set(r,e,t),r._get("eventId")||r._set("silent")},toJSON:function(e){var t=this,n=this.constructor.mapper;if(n)return n.toJSON(this,e);var r=function(){var e={};return j.forOwn(t,function(t,n){e[n]=j.copy(t)}),{v:e}}();return"object"===("undefined"==typeof r?"undefined":s.typeof(r))?r.v:void 0},unset:function(e,t){this.set(e,void 0,t)},validate:function(e){return this._mapper().validate(this,e)}});j.eventify(W.prototype,function(){return this._get("events")},function(e){this._set("events",e)});var z="Schema",X={array:j.isArray,boolean:j.isBoolean,integer:j.isInteger,null:j.isNull,number:j.isNumber,object:j.isObject,string:j.isString},Y=function(e,t){var n="";return e&&(n+=j.isNumber(e)?"["+e+"]":t?"."+e:""+e),n},Z=function(e){e||(e={});var t="",n=e.path||[];return n.forEach(function(e){t+=Y(e,t)}),t+=Y(e.prop,t)},ee=function(e,t,n){return{expected:t,actual:""+e,path:Z(n)}},te=function(e,t,n,r){r.push(ee(e,t,n))},ne=function(e,t,n,r){var i=n[e];return t.length>i?ee(t.length,"length no more than "+i,r):void 0},re=function(e,t,n,r){var i=n[e];return t.length<i?ee(t.length,"length no less than "+i,r):void 0},ie={allOf:function(e,t,n){var r=[];return t.allOf.forEach(function(t){r=r.concat(pe(e,t,n)||[])}),r.length?void 0:r},anyOf:function(e,t,n){var r=!1,i=[];return t.anyOf.forEach(function(t){var o=pe(e,t,n);o?i=i.concat(o):r=!0}),r?void 0:i},dependencies:function(e,t,n){},enum:function(e,t,n){var r=t.enum;return-1===r.indexOf(e)?ee(e,"one of ("+r.join(", ")+")",n):void 0},items:function e(t,n,r){r||(r={});for(var e=n.items,i=[],o=j.isArray(e),a=t.length,u=0;a>u;u++)o&&(e=n.items[u]),r.prop=u,i=i.concat(pe(t[u],e,r)||[]);return i.length?i:void 0},maximum:function e(t,n,r){var e=n.maximum,i=n.exclusiveMaximum;return("undefined"==typeof t?"undefined":s.typeof(t))===("undefined"==typeof e?"undefined":s.typeof(e))&&(i?t>e:t>=e)?ee(t,"no more than "+e,r):void 0},maxItems:function(e,t,n){return ne("maxItems",e,t,n)},maxLength:function(e,t,n){return ne("maxLength",e,t,n)},maxProperties:function e(t,n,r){var e=n.maxProperties,i=Object.keys(t).length;return i>e?ee(i,"no more than "+e+" properties",r):void 0},minimum:function e(t,n,r){var e=n.minimum,i=n.exclusiveMinimum;return("undefined"==typeof t?"undefined":s.typeof(t))===("undefined"==typeof e?"undefined":s.typeof(e))&&(i?e>t:e>=t)?ee(t,"no less than "+e,r):void 0},minItems:function(e,t,n){return re("minItems",e,t,n)},minLength:function(e,t,n){return re("minLength",e,t,n)},minProperties:function e(t,n,r){var e=n.minProperties,i=Object.keys(t).length;return e>i?ee(i,"no more than "+e+" properties",r):void 0},multipleOf:function(e,t,n){},not:function(e,t,n){return pe(e,t.not,n)?void 0:ee("succeeded","should have failed",n)},oneOf:function(e,t,n){var r=!1,i=[];return t.oneOf.forEach(function(t){var o=pe(e,t,n);if(o)i=i.concat(o);else{if(r)return i=[ee("valid against more than one","valid against only one",n)],r=!1,!1;r=!0}}),r?void 0:i},pattern:function e(t,n,r){var e=n.pattern;return j.isString(t)&&!t.match(e)?ee(t,e,r):void 0},properties:function e(t,n,r){r||(r={});var i=j.isUndefined(n.additionalProperties)?!0:n.additionalProperties,o={},e=n.properties||{},a=n.patternProperties||{},u=[];j.forOwn(t,function(e,t){o[t]=void 0}),j.forOwn(e||{},function(e,n){j.isUndefined(t[n])&&!j.isUndefined(e.default)&&(t[n]=j.copy(e.default)),r.prop=n,u=u.concat(pe(t[n],e,r)||[]),delete o[n]}),j.forOwn(a,function(e,n){j.forOwn(o,function(i,a){a.match(n)&&(r.prop=a,u=u.concat(pe(t[a],e,r)||[]),delete o[a])})});var s=Object.keys(o);return i===!1?s.length&&te("extra fields: "+s.join(", "),"no extra fields",r,u):j.isObject(i)&&s.forEach(function(e){r.prop=e,u=u.concat(pe(t[e],i,r)||[])}),u.length?u:void 0},required:function e(t,n,r){var e=n.required,i=[];return r.existingOnly||e.forEach(function(e){if(j.isUndefined(j.get(t,e))){var n=r.prop;r.prop=e,te(void 0,"a value",r,i),r.prop=n}}),i.length?i:void 0},type:function e(t,n,r){var e=n.type,i=void 0;if(j.isString(e)&&(e=[e]),e.forEach(function(e){return X[e](t,n,r)?(i=e,!1):void 0}),!i)return ee(t?"undefined"==typeof t?"undefined":s.typeof(t):""+t,"one of ("+e.join(", ")+")",r);var o=we[i];return o?o(t,n,r):void 0},uniqueItems:function(e,t,n){if(e&&e.length&&t.uniqueItems){var r=e.length,i=void 0,o=void 0,a=void 0;for(o=r-1;o>0;o--)for(i=e[o],a=o-1;a>=0;a--)if(i===e[a])return ee(i,"no duplicates",n)}}},oe=function(e,t,n,r){return!j.isUndefined(n[e])&&ie[e](t,n,r)},ae=function(e,t,n,r){var i=[];return e.forEach(function(e){i=i.concat(oe(e,t,n,r)||[])}),i.length?i:void 0},ue=["enum","type","allOf","anyOf","oneOf","not"],se=["items","maxItems","minItems","uniqueItems"],ce=["multipleOf","maximum","minimum"],le=["maxProperties","minProperties","required","properties","dependencies"],fe=["maxLength","minLength","pattern"],de=function(e,t,n){return ae(ue,e,t,n)},pe=function e(t,n,r){var i=[];r||(r={});var o=void 0,a=r.prop;if(!j.isUndefined(n)){if(!j.isObject(n))throw j.err(z+"#validate")(500,'Invalid schema at path: "'+r.path+'"');return j.isUndefined(r.path)&&(r.path=[]),j.isUndefined(r.prop)||(o=!0,r.path.push(r.prop),r.prop=void 0),n.extends&&(i=j.isFunction(n.extends.validate)?i.concat(n.extends.validate(t,r)||[]):i.concat(e(t,n.extends,r)||[])),j.isUndefined(t)?(n.required===!0&&te(t,"a value",r,i),o&&(r.path.pop(),r.prop=a),i.length?i:void 0):(i=i.concat(de(t,n,r)||[]),o&&(r.path.pop(),r.prop=a),i.length?i:void 0)}},he="changing",ve="changed",ge="creating",ye="eventId",me="noValidate",be="silent",Ae="validation failed",_e=function(e,t,n){var r={enumerable:j.isUndefined(t.enumerable)?!0:!!t.enumerable},i="props."+e,o="previous."+e,a=n.getter,u=n.setter,s=n.unsetter;return r.get=function(){return this._get(i)},r.set=function(n){var r=this,c=r[a],l=r[u],f=r[s];if(!c(me)){var d=t.validate(n);if(d){var p=new Error(Ae);throw p.errors=d,p}}return t.track&&!c(ge)&&!function(){var t=c(o),a=c(i),u=c(he),s=c(ve);u||(s=[]);var d=s.indexOf(e);a!==n&&-1===d&&s.push(e),t===n&&d>=0&&s.splice(d,1),s.length||(u=!1,f(he),f(ve),c(ye)&&(clearTimeout(c(ye)),f(ye))),!u&&s.length&&(l(ve,s),l(he,!0),l(ye,setTimeout(function(){if(f(ve),f(ye),f(he),!c(be)){var e=void 0;for(e=0;e<s.length;e++)r.emit("change:"+s[e],r,j.get(r,s[e]));r.emit("change",r,r.changes())}f(be)},0)))}(),l(i,n),n},r},we={array:function(e,t,n){return ae(se,e,t,n)},integer:function(e,t,n){return we.numeric(e,t,n)},number:function(e,t,n){return we.numeric(e,t,n)},numeric:function(e,t,n){return ae(ce,e,t,n)},object:function(e,t,n){return ae(le,e,t,n)},string:function(e,t,n){return ae(fe,e,t,n)}},Oe=t.extend({constructor:function e(t){t||(t={}),j.fillIn(this,t),t.properties&&j.forOwn(t.properties,function(n,r){n instanceof e||(t.properties[r]=new e(n)); | ||
})},apply:function(e,t){t||(t={}),t.getter=t.getter||"_get",t.setter=t.setter||"_set",t.unsetter=t.unsetter||"_unset";var n=this.properties||{};j.forOwn(n,function(n,r){Object.defineProperty(e,r,_e(r,n,t))})},validate:function(e,t){return pe(e,this,t)}},{typeGroupValidators:we,types:X,validate:pe,validationKeywords:ie}),xe="Mapper",Ce=function(e){return function(){for(var t=arguments.length,n=Array(t),r=0;t>r;r++)n[r]=arguments[r];var i=this,o=n[n.length-e];i.dbg.apply(i,[o.op].concat(n)),(o.notify||void 0===o.notify&&i.notify)&&setTimeout(function(){i.emit.apply(i,[o.op].concat(n))})}},Ie=Ce(1),je=Ce(2),Fe={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(e,t,n,r){return[t,e.toJSON(n,r),r]},beforeAssign:1,defaults:[void 0,{},{}],types:[]},updateAll:{adapterArgs:function(e,t,n,r){return[e.toJSON(t,r),n,r]},beforeAssign:0,defaults:[{},{},{}],types:[]},updateMany:{adapterArgs:function(e,t,n){return[t.map(function(t){return e.toJSON(t,n)}),n]},beforeAssign:0,defaults:[[],{}],types:[]}},Re={_adapters:{},applySchema:!0,debug:!1,defaultAdapter:"http",idAttribute:"id",notify:!0,raw:!1},Se=t.extend({constructor:function e(t){var n=this;if(j.classCallCheck(n,e),e.__super__.call(n),t||(t={}),Object.defineProperties(n,{_adapters:{value:void 0,writable:!0},recordClass:{value:void 0,writable:!0},lifecycleMethods:{value:Fe},schema:{value:void 0,writable:!0}}),j.fillIn(n,t),j.fillIn(n,j.copy(Re)),!n.name)throw j.err("new "+xe,"opts.name")(400,"string",n.name);n.schema instanceof Oe||(n.schema=new Oe(n.schema||{})),j.isUndefined(n.recordClass)&&!function(){var e=W;n.recordClass=e.extend({constructor:function(){var t=function(n,r){j.classCallCheck(this,t),e.call(this,n,r)};return t}()})}(),n.recordClass&&(n.recordClass.mapper=n,j.getSuper(n.recordClass,!0)===W&&n.schema&&n.schema.apply&&n.applySchema&&n.schema.apply(n.recordClass.prototype))},afterCount:je,afterCreate:je,afterCreateMany:je,afterDestroy:je,afterDestroyAll:je,afterFind:je,afterFindAll:je,afterSum:je,afterUpdate:je,afterUpdateAll:je,afterUpdateMany:je,beforeCreate:Ie,beforeCreateMany:Ie,beforeCount:Ie,beforeDestroy:Ie,beforeDestroyAll:Ie,beforeFind:Ie,beforeFindAll:Ie,beforeSum:Ie,beforeUpdate:Ie,beforeUpdateAll:Ie,beforeUpdateMany:Ie,_end:function(e,t,n){var r=this;if(t.raw&&j._(e,t),n)return e;var i=t.raw?e.data:e;return i&&j.isFunction(r.wrap)&&(i=r.wrap(i,t),t.raw?e.data=i:e=i),e},belongsTo:function(e,t){return B(e,t)(this)},count:function(e,t){return this.crud("count",e,t)},create:function(e,t){var n=void 0,r=void 0,i=this;return e||(e={}),t||(t={}),j._(t,i),r=t.adapter=i.getAdapterName(t),n=t.op="beforeCreate",j.resolve(i[n](e,t)).then(function(o){e=j.isUndefined(o)?e:o;var a={};t.with||(t.with=[]);var u=[];return j.forEachRelation(i,t,function(t,n){var r=t.getLocalField(e),i=t.getRelation(),o=i.idAttribute;n.raw=!1,r&&(t.type===D?u.push(i.create(r,n).then(function(n){t.setLocalField(a,n),t.setForeignKey(e,n)})):t.type===T&&t.localKeys&&u.push(i.createMany(r,n).then(function(n){t.setLocalField(a,n),j.set(e,t.localKeys,n.map(function(e){return j.get(e,o)}))})))}),j.Promise.all(u).then(function(){return n=t.op="create",i.dbg(n,e,t),j.resolve(i.getAdapter(r)[n](i,i.toJSON(e,{with:t.pass||[]}),t))}).then(function(n){var r=t.raw?n.data:n;return u=[],j.forEachRelation(i,t,function(t,n){var i=t.getLocalField(e);if(i){n.raw=!1;var o=void 0;t.type===T&&t.foreignKey?(t.setForeignKey(r,i),o=t.getRelation().createMany(i,n).then(function(e){t.setLocalField(r,e)})):t.type===J?(t.setForeignKey(r,i),o=t.getRelation().create(i,n).then(function(e){t.setLocalField(r,e)})):t.type===D&&t.getLocalField(a)?t.setLocalField(r,t.getLocalField(a)):t.type===T&&t.localKeys&&t.getLocalField(a)&&t.setLocalField(r,t.getLocalField(a)),o&&u.push(o)}}),j.Promise.all(u).then(function(){return n})})}).then(function(r){return r=i._end(r,t),n=t.op="afterCreate",j.resolve(i[n](e,t,r)).then(function(e){return j.isUndefined(e)?r:e})})},createInstance:function(e,t){return this.createRecord(e,t)},createMany:function(e,t){var n=void 0,r=void 0,i=this;return e||(e=[]),t||(t={}),j._(t,i),r=t.adapter=i.getAdapterName(t),n=t.op="beforeCreateMany",j.resolve(i[n](e,t)).then(function(o){e=j.isUndefined(o)?e:o;var a={};t.with||(t.with=[]);var u=[];return j.forEachRelation(i,t,function(t,n){var r=e.map(function(e){return t.getLocalField(e)}).filter(function(e){return e});t.type===D&&r.length===e.length&&u.push(t.getRelation().createMany(r,n).then(function(r){var i=n.raw?r.data:r;t.setLocalField(a,i),e.forEach(function(e,n){t.setForeignKey(e,i[n])})}))}),j.Promise.all(u).then(function(){n=t.op="createMany";var o=e.map(function(e){return i.toJSON(e,{with:t.pass||[]})});return i.dbg(n,e,t),j.resolve(i.getAdapter(r)[n](i,o,t))}).then(function(n){var r=t.raw?n.data:n;return u=[],j.forEachRelation(i,t,function(n,o){var s=e.map(function(e){return n.getLocalField(e)}).filter(function(e){return e});if(s.length===e.length){var c=n.getLocalField(a),l=void 0;n.type===T?i.log("warn","deep createMany of hasMany type not supported!"):n.type===J?(r.forEach(function(e,t){n.setForeignKey(e,s[t])}),l=n.getRelation().createMany(s,o).then(function(e){var i=t.raw?e.data:e;r.forEach(function(e,t){n.setLocalField(e,i[t])})})):n.type===D&&c&&c.length===r.length&&r.forEach(function(e,t){n.setLocalField(e,c[t])}),l&&u.push(l)}}),j.Promise.all(u).then(function(){return n})})}).then(function(r){return r=i._end(r,t),n=t.op="afterCreateMany",j.resolve(i[n](e,t,r)).then(function(e){return j.isUndefined(e)?r:e})})},createRecord:function(e,t){e||(e={});var n=this;if(j.isArray(e))return e.map(function(e){return n.createRecord(e,t)});if(!j.isObject(e))throw j.err(xe+"#createRecord","props")(400,"array or object",e);var r=n.recordClass,i=n.relationList||[];return i.forEach(function(n){var r=n.getRelation(),i=n.getLocalField(e);if(i&&!r.is(i)){if(j.isArray(i)&&(!i.length||r.is(i[0])))return;j.set(e,n.localField,r.createRecord(i,t))}}),r?e instanceof r?e:new r(e,t):e},crud:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=this,o=i.lifecycleMethods[e];if(!o)throw j.err(xe+"#crud",e)(404,"method");var a=""+e.charAt(0).toUpperCase()+e.substr(1),u="before"+a,c="after"+a,l=void 0,f=void 0;o.defaults.forEach(function(e,t){j.isUndefined(n[t])&&(n[t]=j.copy(e))});var d=n[n.length-1];return j._(d,i),f=d.adapter=i.getAdapterName(d),l=d.op=u,j.resolve(i[l].apply(i,s.toConsumableArray(n))).then(function(t){var r;return j.isUndefined(o.beforeAssign)||(n[o.beforeAssign]=j.isUndefined(t)?n[o.beforeAssign]:t),l=d.op=e,n=o.adapterArgs?o.adapterArgs.apply(o,[i].concat(s.toConsumableArray(n))):n,i.dbg.apply(i,[l].concat(s.toConsumableArray(n))),j.resolve((r=i.getAdapter(f))[l].apply(r,[i].concat(s.toConsumableArray(n))))}).then(function(e){return e=i._end(e,d,!!o.skip),n.push(e),l=d.op=c,j.resolve(i[l].apply(i,s.toConsumableArray(n))).then(function(t){return j.isUndefined(t)?e:t})})},destroy:function(e,t){return this.crud("destroy",e,t)},destroyAll:function(e,t){return this.crud("destroyAll",e,t)},find:function(e,t){return this.crud("find",e,t)},findAll:function(e,t){return this.crud("findAll",e,t)},getAdapter:function(e){var t=this;t.dbg("getAdapter","name:",e);var n=t.getAdapterName(e);if(!n)throw j.err(xe+"#getAdapter","name")(400,"string",e);return t.getAdapters()[n]},getAdapterName:function(e){return e||(e={}),j.isString(e)&&(e={adapter:e}),e.adapter||e.defaultAdapter},getAdapters:function(){return this._adapters},getSchema:function(){return this.schema},hasMany:function(e,t){return $(e,t)(this)},hasOne:function(e,t){return V(e,t)(this)},is:function(e){var t=this.recordClass;return t?e instanceof t:!1},registerAdapter:function(e,t,n){var r=this;n||(n={}),r.getAdapters()[e]=t,(n===!0||n.default)&&(r.defaultAdapter=e)},sum:function(e,t,n){return this.crud("sum",e,t,n)},toJSON:function(e,t){var n=this,r=void 0;if(t||(t={}),j.isArray(e))return e.map(function(e){return n.toJSON(e,t)});r=e;var i=(n?n.relationFields:[])||[],o={},a=void 0;if(n&&n.schema&&(a=n.schema.properties||{},j.forOwn(a,function(e,t){o[t]=j.plainCopy(r[t])})),a||(a={}),!t.strict)for(var u in r)a[u]||-1!==i.indexOf(u)||(o[u]=j.plainCopy(r[u]));return n&&t.withAll&&(t.with=i.slice()),n&&t.with&&(j.isString(t.with)&&(t.with=[t.with]),j.forEachRelation(n,t,function(e,t){var n=e.getLocalField(r);n&&(j.isArray(n)?e.setLocalField(o,n.map(function(n){return e.getRelation().toJSON(n,t)})):e.setLocalField(o,e.getRelation().toJSON(n,t)))})),o},update:function(e,t,n){return this.crud("update",e,t,n)},updateAll:function(e,t,n){return this.crud("updateAll",e,t,n)},updateMany:function(e,t){return this.crud("updateMany",e,t)},validate:function(e,t){var n=this,r=n.getSchema();if(!j.isArray(e))return r.validate(e,t);var i=e.map(function(e){return r.validate(e,t)}),o=!1;return i.forEach(function(e){e&&(o=!0)}),o?i:void 0},wrap:function(e,t){return this.createRecord(e,t)}}),Ee="Container",ke=["count","create","createMany","createRecord","dbg","destroy","destroyAll","find","findAll","getSchema","is","log","sum","toJSON","update","updateAll","updateMany","validate"],Pe={constructor:function e(t){var n=this;j.classCallCheck(n,e),e.__super__.call(n),t||(t={}),j.fillIn(n,t),n.mapperDefaults=n.mapperDefaults||{},n.mapperClass=n.mapperClass||Se,n._adapters={},n._mappers={}},_onMapperEvent:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=n.shift();this.emit.apply(this,[i,e].concat(n))},defineMapper:function(e,t){var n=this;if(j.isObject(e)&&(t=e,e=t.name),!j.isString(e))throw j.err(Ee+"#defineMapper","name")(400,"string",e);t||(t={}),t.name=e,t.relations||(t.relations={});var r=t.mapperClass||n.mapperClass;delete t.mapperClass,j.fillIn(t,n.mapperDefaults);var i=n._mappers[e]=new r(t);return i.relations||(i.relations={}),i.name=e,i._adapters=n.getAdapters(),i.datastore=n,i.on("all",function(){for(var t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];n._onMapperEvent.apply(n,[e].concat(r))}),j.forOwn(i.relations,function(e,t){j.forOwn(e,function(e,r){j.isObject(e)&&(e=[e]),e.forEach(function(e){e.getRelation=function(){return n.getMapper(r)};var o=n._mappers[r]||r;t===D?i.belongsTo(o,e):t===J?i.hasOne(o,e):t===T&&i.hasMany(o,e)})})}),i},defineResource:function(e,t){return this.defineMapper(e,t)},getAdapter:function(e){var t=this,n=t.getAdapterName(e);if(!n)throw j.err(Ee+"#getAdapter","name")(400,"string",e);return t.getAdapters()[n]},getAdapterName:function(e){return e||(e={}),j.isString(e)&&(e={adapter:e}),e.adapter||this.mapperDefaults.defaultAdapter},getAdapters:function(){return this._adapters},getMapper:function(e){var t=this._mappers[e];if(!t)throw j.err(Ee+"#getMapper",e)(404,"mapper");return t},registerAdapter:function(e,t,n){var r=this;n||(n={}),r.getAdapters()[e]=t,(n===!0||n.default)&&(r.mapperDefaults.defaultAdapter=e,j.forOwn(r._mappers,function(t){t.defaultAdapter=e}))}};ke.forEach(function(e){Pe[e]=function(t){for(var n,r=arguments.length,i=Array(r>1?r-1:0),o=1;r>o;o++)i[o-1]=arguments[o];return(n=this.getMapper(t))[e].apply(n,i)}});var Me=t.extend(Pe),Ke="LinkedCollection",Le=U.extend({constructor:function e(t,n){var r=this;if(j.classCallCheck(r,e),e.__super__.call(r,t,n),Object.defineProperty(r,"_added",{value:{}}),!r.datastore)throw j.err("new "+Ke,"opts.datastore")(400,"DataStore",r.datastore);return r},_onRecordEvent:function(){for(var e=this,t=arguments.length,n=Array(t),r=0;t>r;r++)n[r]=arguments[r];j.getSuper(e).prototype._onRecordEvent.apply(e,n);var i=n[0];j.isString(i)&&0===i.indexOf("change")&&e.updateIndexes(n[1])},add:function(e,t){var n=this,r=n.datastore,i=n.mapper,o=i.relationList,a=(new Date).getTime(),u=!!i.recordClass,c=i.idAttribute,l=void 0;return j.isObject(e)&&!j.isArray(e)&&(l=!0,e=[e]),e=j.getSuper(n).prototype.add.call(n,e,t),o.length&&e.length&&i.relationList.forEach(function(t){var n=t.relation,i=r.getMapper(n),o=i.idAttribute,a=t.foreignKey,u=r.getCollection(n),l=t.type,f=l===T,d=j.isUndefined(t.add)?!0:!!t.add,p=void 0;e.forEach(function(e){p=t.getLocalField(e);var n=j.get(e,c);if(j.isFunction(t.add))p=t.add(r,t,e);else if(p)if(f)p=p.map(function(n){return n!==u.get(u.recordId(n))&&(a&&t.setForeignKey(e,n),d&&(n=u.add(n))),n});else{var i=j.get(p,o);p!==u.get(i)&&(t.setForeignKey(e,p),d&&(p=u.add(p)))}if(!p||j.isArray(p)&&!p.length)if(l===D){var h=j.get(e,a);j.isUndefined(h)||(p=u.get(h))}else if(l===J){var v=u.filter(s.defineProperty({},a,n));p=v.length?v[0]:void 0}else if(l===T)if(a){var g=u.filter(s.defineProperty({},a,n));p=g.length?g:void 0}else if(t.localKeys&&j.get(e,t.localKeys)){var y=u.filter({where:s.defineProperty({},o,{in:j.get(e,t.localKeys)})});p=y.length?y:void 0}else if(t.foreignKeys){var m=u.filter({where:s.defineProperty({},t.foreignKeys,{contains:n})});p=m.length?m:void 0}p&&t.setLocalField(e,p)})}),e.forEach(function(e){n._added[n.recordId(e)]=a,u&&e._set("$",a)}),l?e[0]:e},remove:function(e,t){var n=this,r=n.mapper,i=j.getSuper(n).prototype.remove.call(n,e,t);return i&&(delete n._added[e],r.recordClass&&i._set("$")),i},removeAll:function(e,t){var n=this,r=n.mapper,i=j.getSuper(n).prototype.removeAll.call(n,e,t);return i.forEach(function(e){delete n._added[n.recordId(e)],r.recordClass&&e._set("$")}),i}}),Ne="DataStore",Ue=function(e,t,n){e&&e._set?e._set(t,n):j.set(e,t,n)},De=function(e,t,n){var r=this,i=r._completedQueries[e][t];return j.isFunction(i)?i(e,t,n):i},Te={constructor:function e(t){var n=this;return j.classCallCheck(n,e),e.__super__.call(n,t),n.collectionClass=n.collectionClass||Le,n._collections={},n._pendingQueries={},n._completedQueries={},n},_callSuper:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];return this.constructor.__super__.prototype[e].apply(this,n)},_end:function(e,t,n){var r=n.raw?t.data:t;return r&&j.isFunction(this.addToCache)&&(r=this.addToCache(e,r,n),n.raw?t.data=r:t=r),t},_onCollectionEvent:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=n.shift();this.emit.apply(this,[i,e].concat(n))},addToCache:function(e,t,n){return this.getCollection(e).add(t,n)},cachedFind:De,cachedFindAll:De,cacheFind:function(e,t,n,r){var i=this;i._completedQueries[e][n]=function(e,t,n){return i.get(e,t)}},cacheFindAll:function(e,t,n,r){var i=this;i._completedQueries[e][n]=function(e,t,n){return i.filter(e,j.fromJson(t))}},create:function(e,t,n){var r=this;return n||(n={}),r._callSuper("create",e,t,n).then(function(t){return r._end(e,t,n)})},createMany:function(e,t,n){var r=this;return n||(n={}),r._callSuper("createMany",e,t,n).then(function(t){return r._end(e,t,n)})},defineMapper:function(e,t){var n=this,r=j.getSuper(n).prototype.defineMapper.call(n,e,t);n._pendingQueries[e]={},n._completedQueries[e]={},r.relationList||Object.defineProperty(r,"relationList",{value:[]});var i=n._collections[e]=new n.collectionClass(null,{_added:{},datastore:n,mapper:r}),o=r.schema||{},a=o.properties||{};j.forOwn(a,function(e,t){e.indexed&&i.createIndex(t)}),i.createIndex("addedTimestamps",["$"],{fieldGetter:function(e){return i._added[i.recordId(e)]}}),i.on("all",function(){for(var t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];n._onCollectionEvent.apply(n,[e].concat(r))});var u=r.idAttribute;return r.relationList.forEach(function(e){var t=e.relation,o=e.localField,a="links."+o,s=e.foreignKey,c=e.type,l={index:s},f=void 0,d=function(){return this._get(a)};c===D?(i.indexes[s]||i.createIndex(s),f={get:d,set:function(o){var c=this,f=this._get(a);if(o===f)return f;var d=j.get(c,u),p=e.getInverse(r);if(o){var h=e.getRelation().idAttribute,v=j.get(o,h);if(j.isUndefined(v)||(o=n.get(t,v)||o),c._set(a,o),Ue(c,s,v),i.updateIndex(c,l),p.type===J)j.set(o,p.localField,c);else if(p.type===T){var g=j.get(o,p.localField);j.noDupeAdd(g,c,function(e){return d===j.get(e,u)})}}else c._set(a,void 0),Ue(c,s,void 0),i.updateIndex(c,l);if(f)if(p.type===J)j.set(f,p.localField,void 0);else if(p.type===T){var y=j.get(f,p.localField);j.remove(y,function(e){return d===j.get(e,u)})}return o}},r.recordClass.prototype.hasOwnProperty(s)&&!function(){var e=r.recordClass;r.recordClass=e.extend({constructor:function(){var t=function(n,r){j.classCallCheck(this,t),e.call(this,n,r)};return t}()})}(),Object.defineProperty(r.recordClass.prototype,s,{enumerable:!0,get:function(){return this._get(s)},set:function(e){var r=this;if(j.isUndefined(e))j.set(r,o,void 0);else{Ue(r,s,e);var i=n.get(t,e);i&&j.set(r,o,i)}}})):c===T?!function(){var i=e.localKeys,o=e.foreignKeys;n._collections[t]&&s&&!n.getCollection(t).indexes[s]&&n.getCollection(t).createIndex(s),f={get:function(){var e=this,t=d.call(e);return t||e._set(a,[]),d.call(e)},set:function(c){var f=this;c||(c=[]),c&&!j.isArray(c)&&(c=[c]);var d=j.get(f,u),p=e.getRelation().idAttribute,h=e.getInverse(r),v=h.localField,g=f._get(a);g||(g=[]);var y=g;g=[];var m={};return c.forEach(function(e){var r=j.get(e,p);j.isUndefined(r)||(e=n.get(t,r)||e,m[r]=e),g.push(e)}),s?(c.forEach(function(e){Ue(e,s,d),n.getCollection(t).updateIndex(e,l),j.set(e,v,f)}),y.forEach(function(e){var r=j.get(e,p);j.isUndefined(r)||m.hasOwnProperty(r)||(Ue(e,s,void 0),n.getCollection(t).updateIndex(e,l),j.set(e,v,void 0))})):i?!function(){var e=[];c.forEach(function(t){j.set(t,v,f),e.push(j.get(t,p))}),j.set(f,i,e),y.forEach(function(e){var t=j.get(e,p);j.isUndefined(t)||m.hasOwnProperty(t)||j.set(e,v,void 0)})}():o&&(y.forEach(function(e){var t=j.get(e,o)||[];j.remove(t,function(e){return d===e});var n=j.get(e,v)||[];j.remove(n,function(e){return d===j.get(e,u)})}),c.forEach(function(e){var t=j.get(e,o)||[];j.noDupeAdd(t,d,function(e){return d===e});var n=j.get(e,v)||[];j.noDupeAdd(n,f,function(e){return d===j.get(e,u)})})),f._set(a,g),g}}}():c===J&&(n._collections[t]&&s&&!n.getCollection(t).indexes[s]&&n.getCollection(t).createIndex(s),f={get:d,set:function(i){var o=this,c=this._get(a);if(i===c)return c;var f=j.get(i,e.getRelation().idAttribute),d=e.getInverse(r).localField;return c&&(Ue(c,s,void 0),n.getCollection(t).updateIndex(c,l),j.set(c,d,void 0)),i?(j.isUndefined(f)||(i=n.get(t,f)||i),o._set(a,i),Ue(i,s,j.get(o,u)),n.getCollection(t).updateIndex(i,l),j.set(i,d,o)):o._set(a,void 0),i}}),f&&(f.enumerable=j.isUndefined(e.enumerable)?!1:e.enumerable,e.get&&!function(){var t=f.get;f.get=function(){var n=this;return e.get(e,this,function(){for(var e=arguments.length,r=Array(e),i=0;e>i;i++)r[i]=arguments[i];return t.apply(n,r)})}}(),e.set&&!function(){var t=f.set;f.set=function(n){var r=this;return e.set(e,this,n,function(e){return t.call(r,void 0===e?n:e)})}}(),Object.defineProperty(r.recordClass.prototype,o,f))}),r},destroy:function(e,t,n){var r=this;return n||(n={}),r._callSuper("destroy",e,t,n).then(function(i){return n.raw?i.data=r.getCollection(e).remove(t,n):i=r.getCollection(e).remove(t,n),delete r._pendingQueries[e][t],delete r._completedQueries[e][t],i})},destroyAll:function(e,t,n){var r=this;return n||(n={}),r._callSuper("destroyAll",e,t,n).then(function(i){n.raw?i.data=r.getCollection(e).removeAll(t,n):i=r.getCollection(e).removeAll(t,n);var o=r.hashQuery(e,t,n);return delete r._pendingQueries[e][o],delete r._completedQueries[e][o],i})},eject:function(e,t){return this.remove(e,t)},ejectAll:function(e,t){return this.removeAll(e,t)},find:function(e,t,n){var r=this;n||(n={});var i=r._pendingQueries[e][t];if(j.fillIn(n,r.getMapper(e)),i)return i;var o=r.cachedFind(e,t,n),a=void 0;return a=n.force||!o?r._pendingQueries[e][t]=r._callSuper("find",e,t,n).then(function(i){delete r._pendingQueries[e][t];var o=r._end(e,i,n);return r.cacheFind(e,o,t,n),o},function(n){return delete r._pendingQueries[e][t],j.reject(n)}):j.resolve(o)},findAll:function(e,t,n){var r=this;n||(n={});var i=r.hashQuery(e,t,n),o=r._pendingQueries[e][i];if(j.fillIn(n,r.getMapper(e)),o)return o;var a=r.cachedFindAll(e,i,n),u=void 0;return u=n.force||!a?r._pendingQueries[e][i]=r._callSuper("findAll",e,t,n).then(function(t){delete r._pendingQueries[e][i];var o=r._end(e,t,n);return r.cacheFindAll(e,o,i,n),o},function(t){return delete r._pendingQueries[e][i],j.reject(t)}):j.resolve(a)},getCollection:function(e){var t=this._collections[e];if(!t)throw j.err(Ne+"#getCollection",e)(404,"collection");return t},hashQuery:function(e,t,n){return j.toJson(t)},inject:function(e,t){return this.add(e,t)},remove:function(e,t,n){var r=this,i=r.getCollection(e).remove(t,n);return i&&r.removeRelated(e,[i],n),i},removeAll:function(e,t,n){var r=this,i=r.getCollection(e).removeAll(t,n);return i.length&&r.removeRelated(e,i,n),i},removeRelated:function(e,t,n){var r=this;j.forEachRelation(r.getMapper(e),n,function(e,n){t.forEach(function(t){var i=void 0,o=void 0;if(!e.foreignKey||e.type!==J&&e.type!==T?e.type===T&&e.localKeys?o={where:s.defineProperty({},e.getRelation().idAttribute,{in:j.get(t,e.localKeys)})}:e.type===T&&e.foreignKeys?o={where:s.defineProperty({},e.foreignKeys,{contains:e.getForeignKey(t)})}:e.type===D&&(i=r.remove(e.relation,e.getForeignKey(t),n)):o=s.defineProperty({},e.foreignKey,e.getForeignKey(t)),o&&(i=r.removeAll(e.relation,o,n)),i){if(j.isArray(i)&&!i.length)return;e.type===J&&(i=i[0]),e.setLocalField(t,i)}})})},update:function(e,t,n,r){var i=this;return r||(r={}),i._callSuper("update",e,t,n,r).then(function(t){return i._end(e,t,r)})},updateAll:function(e,t,n,r){var i=this;return r||(r={}),i._callSuper("updateAll",e,n,t,r).then(function(t){return i._end(e,t,r)})},updateMany:function(e,t,n){var r=this;return n||(n={}),r._callSuper("updateMany",e,t,n).then(function(t){return r._end(e,t,n)})}},Je=["add","between","createIndex","filter","get","getAll","query","toJson"];Je.forEach(function(e){Te[e]=function(t){for(var n,r=arguments.length,i=Array(r>1?r-1:0),o=1;r>o;o++)i[o-1]=arguments[o];return(n=this.getCollection(t))[e].apply(n,i)}});var qe=Me.extend(Te),Qe={beta:5,full:"3.0.0-beta.5",major:3,minor:0,patch:0};e.version=Qe,e.Collection=U,e.Component=t,e.Container=Me,e.DataStore=qe,e.Index=a,e.LinkedCollection=Le,e.Mapper=Se,e.Query=K,e.Record=W,e.Schema=Oe,e.utils=j,e.belongsToType=D,e.hasManyType=T,e.hasOneType=J,e.belongsTo=B,e.hasMany=$,e.hasOne=V}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("js-data",["exports"],t):t(e.JSData=e.JSData||{})}(this,function(e){"use strict";function t(){Object.defineProperty(this,"_listeners",{value:{}})}function n(e,t,n){return e===t?0:(n&&(e=n(e),t=n(t)),null===e&&null===t?0:null===e?-1:null===t?1:t>e?-1:e>t?1:0)}function r(e,t,n){return e.splice(t,0,n),e}function i(e,t){return e.splice(t,1),e}function o(e,t,r){for(var i=0,o=e.length,a=void 0,s=void 0;o>i;){if(s=(i+o)/2|0,a=n(t,e[s],r),0===a)return{found:!0,index:s};0>a?o=s:i=s+1}return{found:!1,index:o}}function a(e,t){if(j.classCallCheck(this,a),e||(e=[]),!j.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=[]}function s(e,t){var n="new "+J;t||(t={});var r=t.localField;if(!r)throw j.err(n,"opts.localField")(400,"string",r);var i=t.foreignKey=t.foreignKey||t.localKey;if(!i&&(t.type===N||t.type===T))throw j.err(n,"opts.foreignKey")(400,"string",i);var o=t.localKeys,a=t.foreignKeys;if(!i&&!o&&!a&&t.type===D)throw j.err(n,"opts.<foreignKey|localKeys|foreignKeys>")(400,"string",i);if(j.isString(e)){if(t.relation=e,!j.isFunction(t.getRelation))throw j.err(n,"opts.getRelation")(400,"function",t.getRelation)}else{if(!e)throw j.err(n,"related")(400,"Mapper or string",e);t.relation=e.name,Object.defineProperty(this,"relatedMapper",{value:e})}Object.defineProperty(this,"inverse",{value:void 0,writable:!0}),j.fillIn(this,t)}var u={};u.typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},u.defineProperty=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},u.toConsumableArray=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)};var c="utils",l=1/0,f=1.7976931348623157e308,d="[object Boolean]",h="[object Date]",p="[object Function]",v="[object Number]",g="[object Object]",y="[object RegExp]",m="[object String]",b=Object.prototype.toString,A=/^(.+)\.(.+)$/,_={400:function(){return"expected: "+arguments[0]+", found: "+(arguments[2]?arguments[1]:u.typeof(arguments[1]))},404:function(){return arguments[0]+" not found"}},O=function(e){if(!e)return 0;if(e=+e,e===l||e===-l){var t=0>e?-1:1;return t*f}var n=e%1;return e===e?n?e-n:e:0},w=function(e){return b.call(e)},x=function(e){return!!e&&"object"===("undefined"==typeof e?"undefined":u.typeof(e))&&e.constructor===Object},C=function(e,t){if(!t)return e;var n=t.split(".");return n.forEach(function(t){e[t]||(e[t]={}),e=e[t]}),e},j={Promise:Promise,_:function(e,t){j.forOwn(t,function(t,n){n&&j.isUndefined(e[n])&&!j.isFunction(t)&&0!==n.indexOf("_")&&(e[n]=t)})},_forRelation:function(e,t,n,r){var i=t.relation,o=null,a=void 0;if(e||(e={}),e.with||(e.with=[]),(a=j._getIndex(e.with,i))>=0?o=i:(a=j._getIndex(e.with,t.localField))>=0&&(o=t.localField),e.withAll)return void n.call(r,t,{});if(o){var s={};j.fillIn(s,t.getRelation()),j.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(r,t,s)}},_getIndex:function(e,t){var n=-1;return e.forEach(function(e,r){return e===t?(n=r,!1):j.isObject(e)&&e.relation===t?(n=r,!1):void 0}),n},addHiddenPropsToTarget:function(e,t){var n={};j.forOwn(t,function(e,t){n[t]={writable:!0,value:e}}),Object.defineProperties(e,n)},areDifferent:function(e,t,n){n||(n={});var r=j.diffObjects(e,t,n),i=Object.keys(r.added).length+Object.keys(r.removed).length+Object.keys(r.changed).length;return i>0},classCallCheck:function(e,t){if(!(e instanceof t))throw j.err(""+t.name)(500,"Cannot call a class as a function")},copy:function(e,t,n,r,i,o){if(t){if(e===t)throw j.err(c+".copy")(500,"Cannot copy! Source and destination are identical.");if(n=n||[],r=r||[],j.isObject(e)){var a=n.indexOf(e);if(-1!==a)return r[a];n.push(e),r.push(t)}var s=void 0;if(j.isArray(e)){var u=void 0;for(t.length=0,u=0;u<e.length;u++)s=j.copy(e[u],null,n,r,i,o),j.isObject(e[u])&&(n.push(e[u]),r.push(s)),t.push(s)}else{j.isArray(t)?t.length=0:j.forOwn(t,function(e,n){delete t[n]});for(var l in e)if(e.hasOwnProperty(l)){if(j.isBlacklisted(l,i))continue;s=j.copy(e[l],null,n,r,i,o),j.isObject(e[l])&&(n.push(e[l]),r.push(s)),t[l]=s}}}else t=e,e&&(j.isArray(e)?t=j.copy(e,[],n,r,i,o):j.isDate(e)?t=new Date(e.getTime()):j.isRegExp(e)?(t=new RegExp(e.source,e.toString().match(/[^\/]*$/)[0]),t.lastIndex=e.lastIndex):j.isObject(e)&&(t=o?j.copy(e,{},n,r,i,o):j.copy(e,Object.create(Object.getPrototypeOf(e)),n,r,i,o)));return t},deepFillIn:function(e,t){return t&&j.forOwn(t,function(t,n){var r=e[n];x(t)&&x(r)?j.deepFillIn(r,t):e.hasOwnProperty(n)&&void 0!==e[n]||(e[n]=t)}),e},deepMixIn:function(e,t){return t&&j.forOwn(t,function(t,n){var r=e[n];x(t)&&x(r)?j.deepMixIn(r,t):e[n]=t}),e},diffObjects:function(e,t,n){n||(n={});var r=n.equalsFn,i=n.ignore,o={added:{},changed:{},removed:{}};return j.isFunction(r)||(r=j.deepEqual),j.forOwn(t,function(t,n){var a=e[n];j.isBlacklisted(n,i)||r(a,t)||(j.isUndefined(a)?o.removed[n]=void 0:r(a,t)||(o.changed[n]=a))}),j.forOwn(e,function(e,n){j.isUndefined(t[n])&&!j.isBlacklisted(n,i)&&(o.added[n]=e)}),o},equal:function(e,t){return e==t},err:function(e,t){return function(n){var r="["+e+":"+t+"] ",i=_[n].apply(null,Array.prototype.slice.call(arguments,1));return i=""+r+i+"\nhttp://www.js-data.io/v3.0/docs/errors#"+n,new Error(i)}},eventify:function(e,t,n){e=e||this;var r={};t||n||(t=function(){return r},n=function(e){r=e}),Object.defineProperties(e,{emit:{value:function(){for(var e=t.call(this)||{},n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];var o=r.shift(),a=e[o]||[],s=void 0;for(s=0;s<a.length;s++)a[s].f.apply(a[s].c,r);for(a=e.all||[],r.unshift(o),s=0;s<a.length;s++)a[s].f.apply(a[s].c,r)}},off:{value:function(e,r){var i=t.call(this),o=i[e];if(o)if(r){for(var a=0;a<o.length;a++)if(o[a].f===r){o.splice(a,1);break}}else o.splice(0,o.length);else n.call(this,{})}},on:{value:function(e,r,i){t.call(this)||n.call(this,{});var o=t.call(this);o[e]=o[e]||[],o[e].push({c:i,f:r})}}})},extend:function(e,t){var n=this,r=void 0;e||(e={}),t||(t={}),e.hasOwnProperty("constructor")?(r=e.constructor,delete e.constructor):r=function(){j.classCallCheck(this,r);for(var e=arguments.length,t=Array(e),i=0;e>i;i++)t[i]=arguments[i];n.apply(this,t)},r.prototype=Object.create(n&&n.prototype,{constructor:{configurable:!0,enumerable:!1,value:r,writable:!0}});var i=Object;return i.setPrototypeOf?i.setPrototypeOf(r,n):t.strictEs6Class?r.__proto__=n:j.forOwn(n,function(e,t){r[t]=e}),r.hasOwnProperty("__super__")||Object.defineProperty(r,"__super__",{configurable:!0,value:n}),j.addHiddenPropsToTarget(r.prototype,e),j.fillIn(r,t),r},fillIn:function(e,t){return j.forOwn(t,function(t,n){e.hasOwnProperty(n)&&void 0!==e[n]||(e[n]=t)}),e},findIndex:function(e,t){var n=-1;return e?(e.forEach(function(e,r){return t(e)?(n=r,!1):void 0}),n):n},forEachRelation:function(e,t,n,r){var i=e.relationList||[];i.length&&i.forEach(function(e){j._forRelation(t,e,n,r)})},forOwn:function(e,t,n){var r=Object.keys(e),i=r.length,o=void 0;for(o=0;i>o;o++)t.call(n,e[r[o]],r[o],e)},fromJson:function(e){return j.isString(e)?JSON.parse(e):e},get:function(e,t){if(t){for(var n=t.split("."),r=n.pop();t=n.shift();)if(e=e[t],null==e)return;return e[r]}},getSuper:function(e,t){var n=t?e:e.constructor;return n.hasOwnProperty("__super__")?n.__super__:Object.getPrototypeOf(n)||n.__proto__},intersection:function(e,t){if(!e||!t)return[];var n=[],r=void 0,i=void 0,o=e.length;for(i=0;o>i;i++)r=e[i],-1===n.indexOf(r)&&-1!==t.indexOf(r)&&n.push(r);return n},isArray:Array.isArray,isBlacklisted:function(e,t){if(!t||!t.length)return!1;for(var n=void 0,r=0;r<t.length;r++)if(w(t[r])===y&&t[r].test(e)||t[r]===e)return n=e;return!!n},isBoolean:function(e){return w(e)===d},isBrowser:!1,isDate:function(e){return e&&"object"===("undefined"==typeof e?"undefined":u.typeof(e))&&w(e)===h},isFunction:function(e){return"function"==typeof e||e&&w(e)===p},isInteger:function(e){return w(e)===v&&e==O(e)},isNull:function(e){return null===e},isNumber:function(e){var t="undefined"==typeof e?"undefined":u.typeof(e);return"number"===t||e&&"object"===t&&w(e)===v},isObject:function(e){return w(e)===g},isRegExp:function(e){return w(e)===y},isSorN:function(e){return j.isString(e)||j.isNumber(e)},isString:function(e){return"string"==typeof e||e&&"object"===("undefined"==typeof e?"undefined":u.typeof(e))&&w(e)===m},isUndefined:function(e){return void 0===e},logify:function(e){j.addHiddenPropsToTarget(e,{dbg:function(){for(var e=arguments.length,t=Array(e),n=0;e>n;n++)t[n]=arguments[n];this.log.apply(this,["debug"].concat(t))},log:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];if(e&&!n.length&&(n.push(e),e="debug"),"debug"!==e||this.debug){var i=e.toUpperCase()+": ("+(this.name||this.constructor.name)+")";if(console[e]){var o;(o=console)[e].apply(o,[i].concat(n))}else{var a;(a=console).log.apply(a,[i].concat(n))}}}})},noDupeAdd:function(e,t,n){if(e){var r=this.findIndex(e,n);0>r&&e.push(t)}},omit:function(e,t){var n={};return j.forOwn(e,function(e,r){-1===t.indexOf(r)&&(n[r]=e)}),n},pick:function(e,t){var n={};return j.forOwn(e,function(e,r){-1!==t.indexOf(r)&&(n[r]=e)}),n},plainCopy:function(e){return j.copy(e,void 0,void 0,void 0,void 0,!0)},reject:function(e){return j.Promise.reject(e)},remove:function(e,t){if(e&&e.length){var n=this.findIndex(e,t);n>=0&&e.splice(n,1)}},resolve:function(e){return j.Promise.resolve(e)},set:function(e,t,n){if(j.isObject(t))j.forOwn(t,function(t,n){j.set(e,n,t)});else{var r=A.exec(t);r?C(e,r[1])[r[2]]=n:e[t]=n}},deepEqual:function(e,t){if(e===t)return!0;var n=!0;if(j.isObject(e)&&j.isObject(t)){if(j.forOwn(e,function(e,r){n=n&&j.deepEqual(e,t[r])}),!n)return n;j.forOwn(t,function(t,r){n=n&&j.deepEqual(t,e[r])})}else{if(!j.isArray(e)||!j.isArray(t))return!1;e.forEach(function(e,r){return n=n&&j.deepEqual(e,t[r]),n?void 0:!1})}return n},toJson:JSON.stringify,unset:function(e,t){for(var n=t.split("."),r=n.pop();t=n.shift();)if(e=e[t],null==e)return;e[r]=void 0}};try{j.isBrowser=!!window}catch(e){j.isBrowser=!1}t.extend=j.extend,j.logify(t.prototype),j.eventify(t.prototype,function(){return this._listeners},function(e){this._listeners=e});var I="Query",E="Index inaccessible after first operation",F={limit:"",offset:"",orderBy:"",skip:"",sort:"",where:""},R=/([.*+?^=!:${}()|[\]\/\\])/g,S=/%/g,k=/_/g,P=function(e){return e.replace(R,"\\$1")},M=t.extend({constructor:function e(t){j.classCallCheck(this,e),this.collection=t,this.data=null},_applyWhereFromObject:function(e){var t=[],n=[],r=[];return j.forOwn(e,function(e,i){j.isObject(e)||(e={"==":e}),j.forOwn(e,function(e,o){t.push(i),n.push(o),r.push(e)})}),{fields:t,ops:n,predicates:r}},_applyWhereFromArray:function(e){var t=this,n=[];return e.forEach(function(r,i){if(!j.isString(r)){var o=e[i-1],a=j.isArray(r)?t._applyWhereFromArray:t._applyWhereFromObject,s=a.call(t,r);"or"===o&&(s.isOr=!0),n.push(s)}}),n.isArray=!0,n},_testObjectGroup:function(e,t,n,r){var i=void 0,o=n.fields,a=n.ops,s=n.predicates,u=a.length;for(i=0;u>i;i++){var c=a[i],l="|"===c.charAt(0);c=l?c.substr(1):c;var f=this.evaluate(j.get(r,o[i]),c,s[i]);void 0!==f&&(e=t?f:l?e||f:e&&f),t=!1}return{keep:e,first:t}},_testArrayGroup:function(e,t,n,r){var i=void 0,o=n.length;for(i=0;o>i;i++){var a=n[i],s=a.isArray?this._testArrayGroup:this._testObjectGroup,u=s.call(this,!0,!0,a,r);e=n[i-1]?a.isOr?e||u.keep:e&&u.keep:u.keep,t=u.first}return{keep:e,first:t}},between:function(e,t,n){if(n||(n={}),this.data)throw j.err(I+"#between")(500,"Cannot access index");return this.data=this.collection.getIndex(n.index).between(e,t,n),this},compare:function(e,t,n,r){var i=e[t],o=j.get(n,i[0]),a=j.get(r,i[0]);if(o&&j.isString(o)&&(o=o.toUpperCase()),a&&j.isString(a)&&(a=a.toUpperCase()),void 0===n&&(n=null),void 0===r&&(r=null),"DESC"===i[1].toUpperCase()){var s=a;a=o,o=s}return a>o?-1:o>a?1:t<e.length-1?this.compare(e,t+1,n,r):0},evaluate:function(e,t,n){var r=this.constructor.ops;return r[t]?r[t](e,n):0===t.indexOf("like")?!j.isNull(this.like(n,t.substr(4)).exec(e)):0===t.indexOf("notLike")?j.isNull(this.like(n,t.substr(7)).exec(e)):void 0},filter:function(e,t){var n=this;return e||(e={}),this.getData(),j.isObject(e)?!function(){var t={};(j.isObject(e.where)||j.isArray(e.where))&&(t=e.where),j.forOwn(e,function(e,n){n in F||n in t||(t[n]={"==":e})});var r=void 0;j.isObject(t)&&0!==Object.keys(t).length?r=n._applyWhereFromArray([t]):j.isArray(t)&&(r=n._applyWhereFromArray(t)),r&&(n.data=n.data.filter(function(e,t){return n._testArrayGroup(!0,!0,r,e).keep}));var i=e.orderBy||e.sort;j.isString(i)&&(i=[[i,"ASC"]]),j.isArray(i)||(i=null),i&&!function(){var e=0;i.forEach(function(e,t){j.isString(e)&&(i[t]=[e,"ASC"])}),n.data.sort(function(t,r){return n.compare(i,e,t,r)})}(),j.isNumber(e.skip)?n.skip(e.skip):j.isNumber(e.offset)&&n.skip(e.offset),j.isNumber(e.limit)&&n.limit(e.limit)}():j.isFunction(e)&&(this.data=this.data.filter(e,t)),this},forEach:function(e,t){return this.getData().forEach(e,t),this},get:function(e,t){if(e||(e=[]),t||(t={}),this.data)throw j.err(I+"#get")(500,E);return e&&!j.isArray(e)&&(e=[e]),e.length?(this.data=this.collection.getIndex(t.index).get(e),this):(this.getData(),this)},getAll:function(){var e=this,t={};if(this.data)throw j.err(I+"#getAll")(500,E);for(var n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];if(!r.length||1===r.length&&j.isObject(r[0]))return this.getData(),this;r.length&&j.isObject(r[r.length-1])&&(t=r[r.length-1],r.pop());var o=this.collection,a=o.getIndex(t.index);return this.data=[],r.forEach(function(t){e.data=e.data.concat(a.get(t))}),this},getData:function(){return this.data||(this.data=this.collection.index.getAll()),this.data},like:function(e,t){return new RegExp("^"+P(e).replace(S,".*").replace(k,".")+"$",t)},limit:function(e){if(!j.isNumber(e))throw j.err(I+"#limit","num")(400,"number",e);var t=this.getData();return this.data=t.slice(0,Math.min(t.length,e)),this},map:function(e,t){return this.data=this.getData().map(e,t),this},mapCall:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];return this.data=this.getData().map(function(t){return t[e].apply(t,n)}),this},run:function(){var e=this.data;return this.data=null,e},skip:function(e){if(!j.isNumber(e))throw j.err(I+"#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 e>t},">=":function(e,t){return e>=t},"<":function(e,t){return t>e},"<=":function(e,t){return t>=e},isectEmpty:function(e,t){return!j.intersection(e||[],t||[]).length},isectNotEmpty:function(e,t){return j.intersection(e||[],t||[]).length},in:function(e,t){return-1!==t.indexOf(e)},notIn:function(e,t){return-1===t.indexOf(e)},contains:function(e,t){return-1!==(e||[]).indexOf(t)},notContains:function(e,t){return-1===(e||[]).indexOf(t)}}});j.addHiddenPropsToTarget(a.prototype,{set:function(e,t){j.isArray(e)||(e=[e]);var n=e.shift()||null,i=o(this.keys,n);if(0===e.length)if(i.found){var s=o(this.values[i.index],t,this.hashCode);s.found||r(this.values[i.index],s.index,t)}else r(this.keys,i.index,n),r(this.values,i.index,[t]);else if(i.found)this.values[i.index].set(e,t);else{r(this.keys,i.index,n);var u=new a([],{hashCode:this.hashCode});u.set(e,t),r(this.values,i.index,u)}},get:function(e){j.isArray(e)||(e=[e]);var t=e.shift()||null,n=o(this.keys,t);return 0===e.length?n.found?this.values[n.index].isIndex?this.values[n.index].getAll():this.values[n.index]:[]:n.found?this.values[n.index].get(e):[]},getAll:function(e){e||(e={});var t=[],n=this.values;if("desc"===e.order)for(var r=n.length-1;r>=0;r--){var i=n[r];t=i.isIndex?t.concat(i.getAll(e)):t.concat(i)}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(e,t){this.values.forEach(function(n){n.isIndex?n.visitAll(e,t):n.forEach(e,t)})},between:function(e,t,n){n||(n={}),j.isArray(e)||(e=[e]),j.isArray(t)||(t=[t]),j.fillIn(n,{leftInclusive:!0,rightInclusive:!1,limit:void 0,offset:0});var r=this._between(e,t,n);return n.limit?r.slice(n.offset,n.limit+n.offset):r.slice(n.offset)},_between:function(e,t,n){var r=[],i=e.shift(),a=t.shift(),s=void 0;if(s=void 0!==i?o(this.keys,i):{found:!1,index:0},0===e.length){s.found&&n.leftInclusive===!1&&(s.index+=1);for(var u=s.index;u<this.keys.length;u+=1){if(void 0!==a)if(n.rightInclusive){if(this.keys[u]>a)break}else if(this.keys[u]>=a)break;if(r=this.values[u].isIndex?r.concat(this.values[u].getAll()):r.concat(this.values[u]),n.limit&&r.length>=n.limit+n.offset)break}}else for(var c=s.index;c<this.keys.length;c+=1){var l=this.keys[c];if(l>a)break;if(r=this.values[c].isIndex?l===i?r.concat(this.values[c]._between(j.copy(e),t.map(function(){}),n)):l===a?r.concat(this.values[c]._between(e.map(function(){}),j.copy(t),n)):r.concat(this.values[c].getAll()):r.concat(this.values[c]),n.limit&&r.length>=n.limit+n.offset)break}return n.limit?r.slice(0,n.limit+n.offset):r},peek:function(){return this.values.length?this.values[0].isIndex?this.values[0].peek():this.values[0]:[]},clear:function(){this.keys=[],this.values=[]},insertRecord:function(e){var t=this.fieldList.map(function(t){return j.isFunction(t)?t(e)||null:e[t]||null});this.set(t,e)},removeRecord:function(e){var t=this,n=void 0;return this.values.forEach(function(r,a){if(r.isIndex){if(r.removeRecord(e))return 0===r.keys.length&&(i(t.keys,a),i(t.values,a)),n=!0,!1}else{var s=o(r,e,t.hashCode);if(s.found)return i(r,s.index),0===r.length&&(i(t.keys,a),i(t.values,a)),n=!0,!1}}),n?e:void 0},updateRecord:function(e){this.removeRecord(e),this.insertRecord(e)}});var K="Collection",L={idAttribute:"id",onConflict:"merge"},U=t.extend({constructor:function e(t,n){j.classCallCheck(this,e),e.__super__.call(this),t&&!j.isArray(t)&&(n=t,t=[]),j.isString(n)&&(n={idAttribute:n}),t||(t=[]),n||(n={}),Object.defineProperties(this,{mapper:{value:void 0,writable:!0},queryClass:{value:void 0,writable:!0}}),j.fillIn(this,n),j.fillIn(this,j.copy(L)),this.queryClass||(this.queryClass=M);var r=this.recordId();Object.defineProperties(this,{index:{value:new a([r],{hashCode:function(e){return j.get(e,r)}})},indexes:{value:{}}}),t&&this.add(t)},_onRecordEvent:function(){this.emit.apply(this,arguments)},add:function(e,t){var n=this;t||(t={}),j._(t,this),e=this.beforeAdd(e,t)||e;var r=!1,i=this.recordId();if(!j.isArray(e)){if(!j.isObject(e))throw j.err(K+"#add","records")(400,"object or array",e);e=[e],r=!0}e=e.map(function(e){var r=n.recordId(e);if(!j.isSorN(r))throw j.err(K+"#add","record."+i)(400,"string or number",r);var o=n.get(r);if(e===o)return o;if(o){var a=t.onConflict||n.onConflict;if("merge"===a)j.deepMixIn(o,e);else{if("replace"!==a)throw j.err(K+"#add","opts.onConflict")(400,"one of (merge, replace)",a,!0);j.forOwn(o,function(t,n){n===i||e.hasOwnProperty(n)||delete o[n]}),o.set(e)}e=o,n.updateIndexes(e)}else e=n.mapper?n.mapper.createRecord(e,t):e,n.index.insertRecord(e),j.forOwn(n.indexes,function(t,n){t.insertRecord(e)}),e&&j.isFunction(e.on)&&e.on("all",n._onRecordEvent,n);return e});var o=r?e[0]:e;return this.emit("add",o),this.afterAdd(e,t,o)||o},afterAdd:function(){},afterRemove:function(){},afterRemoveAll:function(){},beforeAdd:function(){},beforeRemove:function(){},beforeRemoveAll:function(){},between:function(e,t,n){return this.query().between(e,t,n).run()},createIndex:function(e,t,n){var r=this;j.isString(e)&&void 0===t&&(t=[e]),n||(n={}),n.hashCode||(n.hashCode=function(e){return r.recordId(e)});var i=this.indexes[e]=new a(t,n);return this.index.visitAll(i.insertRecord,i),this},filter:function(e,t){return this.query().filter(e,t).run()},forEach:function(e,t){this.index.visitAll(e,t)},get:function(e){var t=this.query().get(e).run();return t.length?t[0]:void 0},getAll:function(){var e;return(e=this.query()).getAll.apply(e,arguments).run()},getIndex:function(e){var t=e?this.indexes[e]:this.index;if(!t)throw j.err(K+"#getIndex",e)(404,"index");return t},limit:function(e){return this.query().limit(e).run()},map:function(e,t){var n=[];return this.index.visitAll(function(r){n.push(e.call(t,r))}),n},mapCall:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=[];return this.index.visitAll(function(t){i.push(t[e].apply(t,n))}),i},recordId:function(e){return e?j.get(e,this.recordId()):this.mapper?this.mapper.idAttribute:this.idAttribute},query:function(){var e=this.queryClass;return new e(this)},reduce:function(e,t){var n=this.getAll();return n.reduce(e,t)},remove:function(e,t){t||(t={}),this.beforeRemove(e,t);var n=this.get(e);return n&&(this.index.removeRecord(n),j.forOwn(this.indexes,function(e,t){e.removeRecord(n)}),n&&j.isFunction(n.off)&&(n.off("all",this._onRecordEvent,this),this.emit("remove",n))),this.afterRemove(e,t,n)||n},removeAll:function(e,t){var n=this;t||(t={}),this.beforeRemoveAll(e,t);var r=this.filter(e);return r.forEach(function(e){n.remove(n.recordId(e),t)}),this.afterRemoveAll(e,t,r)||r},skip:function(e){return this.query().skip(e).run()},toJSON:function(e){return this.mapCall("toJSON",e)},updateIndex:function(e,t){t||(t={}),this.getIndex(t.index).updateRecord(e)},updateIndexes:function(e){this.index.updateRecord(e),j.forOwn(this.indexes,function(t,n){t.updateRecord(e)})}}),N="belongsTo",D="hasMany",T="hasOne",J="Relation";j.addHiddenPropsToTarget(s.prototype,{getRelation:function(){return this.relatedMapper},getForeignKey:function(e){return this.type===N?j.get(e,this.foreignKey):j.get(e,this.mapper.idAttribute)},setForeignKey:function(e,t){var n=this;e&&t&&(this.type===N?j.set(e,this.foreignKey,j.get(t,this.getRelation().idAttribute)):!function(){var r=n.mapper.idAttribute;j.isArray(t)?t.forEach(function(t){j.set(t,n.foreignKey,j.get(e,r))}):j.set(t,n.foreignKey,j.get(e,r))}())},getLocalField:function(e){return j.get(e,this.localField)},setLocalField:function(e,t){return j.set(e,this.localField,t)},getInverse:function(e){var t=this;return this.inverse?this.inverse:(this.getRelation().relationList.forEach(function(n){if(n.getRelation()===e){if(n.foreignKey&&n.foreignKey!==t.foreignKey)return;return t.inverse=n,!1}}),this.inverse)}});var q=function(e,t,n){n.name=e.name;var r=new s(t,n);Object.defineProperty(r,"mapper",{value:e}),e.relationList||Object.defineProperty(e,"relationList",{value:[]}),e.relationFields||Object.defineProperty(e,"relationFields",{value:[]}),e.relationList.push(r),e.relationFields.push(r.localField)},Q=function(e,t){return t||(t={}),t.type=N,function(n){q(n,e,t)}},B=function(e,t){return t||(t={}),t.type=D,function(n){q(n,e,t)}},G=function(e,t){return t||(t={}),t.type=T,function(n){q(n,e,t)}},$="Record",W=function(e,t){var n=e.datastore;return n&&n[t]?function(){for(var r=arguments.length,i=Array(r),o=0;r>o;o++)i[o]=arguments[o];return n[t].apply(n,[e.name].concat(i))}:e[t].bind(e)},V=t.extend({constructor:function e(t,n){j.classCallCheck(this,e),t||(t={}),n||(n={});var r={};Object.defineProperties(this,{_get:{value:function(e){return j.get(r,e)}},_set:{value:function(e,t){return j.set(r,e,t)}},_unset:{value:function(e){return j.unset(r,e)}}});var i=this._set;i("creating",!0),n.noValidate&&i("noValidate",!0),j.fillIn(this,t),i("creating",!1),i("noValidate",!1),i("previous",j.copy(t))},_mapper:function(){var e=this.constructor.mapper;if(!e)throw j.err($+"#_mapper","")(404,"mapper");return e},afterLoadRelations:function(){},beforeLoadRelations:function(){},changes:function(e){return e||(e={}),j.diffObjects("function"==typeof this.toJSON?this.toJSON(e):this,this._get("previous"),e)},commit:function(){return this._set("changed"),this._set("previous",j.copy(this)),this},destroy:function(e){e||(e={});var t=this._mapper();return W(t,"destroy")(j.get(this,t.idAttribute),e)},get:function(e){return j.get(this,e)},hasChanges:function(e){var t=!!(this._get("changed")||[]).length;return t||j.areDifferent("function"==typeof this.toJSON?this.toJSON(e):this,this._get("previous"),e)},hashCode:function(){return j.get(this,this._mapper().idAttribute)},isValid:function(e){return!this._mapper().validate(this,e)},loadRelations:function(e,t){var n=this,r=void 0,i=this._mapper();return e||(e=[]),j.isString(e)&&(e=[e]),t||(t={}),t.with=e,j._(t,i),t.adapter=i.getAdapterName(t),r=t.op="beforeLoadRelations",j.resolve(this[r](e,t)).then(function(){r=t.op="loadRelations",i.dbg(r,n,e,t);var o=[],a=void 0;return j.forEachRelation(i,t,function(e,r){var s=e.getRelation();if(r.raw=!1,j.isFunction(e.load))a=e.load(i,e,n,t);else if("hasMany"===e.type||"hasOne"===e.type)e.foreignKey?a=W(s,"findAll")(u.defineProperty({},e.foreignKey,j.get(n,i.idAttribute)),r).then(function(t){return"hasOne"===e.type?t.length?t[0]:void 0:t}):e.localKeys?a=W(s,"findAll")({where:u.defineProperty({},s.idAttribute,{in:j.get(n,e.localKeys)})}):e.foreignKeys&&(a=W(s,"findAll")({where:u.defineProperty({},e.foreignKeys,{contains:j.get(n,i.idAttribute)})},t));else if("belongsTo"===e.type){var c=j.get(n,e.foreignKey);j.isSorN(c)&&(a=W(s,"find")(c,r))}a&&(a=a.then(function(t){e.setLocalField(n,t)}),o.push(a))}),Promise.all(o)}).then(function(){return r=t.op="afterLoadRelations",j.resolve(n[r](e,t)).then(function(){return n})})},previous:function(e){return e?this._get("previous."+e):this._get("previous")},revert:function(e){var t=this,n=this._get("previous");return e||(e={}),e.preserve||(e.preserve=[]),j.forOwn(this,function(r,i){i!==t._mapper().idAttribute&&!n.hasOwnProperty(i)&&t.hasOwnProperty(i)&&-1===e.preserve.indexOf(i)&&delete t[i]}),j.forOwn(n,function(n,r){-1===e.preserve.indexOf(r)&&(t[r]=n)}),this.commit(),this},save:function(e){e||(e={});var t=this._mapper(),n=j.get(this,t.idAttribute),r=this;if(j.isUndefined(n))return W(t,"create")(r,e);if(e.changesOnly){var i=this.changes(e);r={},j.fillIn(r,i.added),j.fillIn(r,i.changed)}return W(t,"update")(n,r,e)},set:function(e,t,n){j.isObject(e)&&(n=t),n||(n={}),n.silent&&this._set("silent",!0),j.set(this,e,t),this._get("eventId")||this._set("silent")},toJSON:function(e){var t=this,n=this.constructor.mapper;if(n)return n.toJSON(this,e);var r=function(){var e={};return j.forOwn(t,function(t,n){e[n]=j.copy(t)}),{v:e}}();return"object"===("undefined"==typeof r?"undefined":u.typeof(r))?r.v:void 0},unset:function(e,t){this.set(e,void 0,t)},validate:function(e){return this._mapper().validate(this,e)}});j.eventify(V.prototype,function(){return this._get("events")},function(e){this._set("events",e)});var H="Schema",z={array:j.isArray,boolean:j.isBoolean,integer:j.isInteger,null:j.isNull,number:j.isNumber,object:j.isObject,string:j.isString},X=function(e,t){var n="";return e&&(n+=j.isNumber(e)?"["+e+"]":t?"."+e:""+e),n},Y=function(e){e||(e={});var t="",n=e.path||[];return n.forEach(function(e){t+=X(e,t)}),t+=X(e.prop,t)},Z=function(e,t,n){return{expected:t,actual:""+e,path:Y(n)}},ee=function(e,t,n,r){r.push(Z(e,t,n))},te=function(e,t,n,r){var i=n[e];return t.length>i?Z(t.length,"length no more than "+i,r):void 0},ne=function(e,t,n,r){var i=n[e];return t.length<i?Z(t.length,"length no less than "+i,r):void 0},re={allOf:function(e,t,n){var r=[];return t.allOf.forEach(function(t){r=r.concat(de(e,t,n)||[])}),r.length?void 0:r},anyOf:function(e,t,n){var r=!1,i=[];return t.anyOf.forEach(function(t){var o=de(e,t,n);o?i=i.concat(o):r=!0}),r?void 0:i},dependencies:function(e,t,n){},enum:function(e,t,n){var r=t.enum;return-1===r.indexOf(e)?Z(e,"one of ("+r.join(", ")+")",n):void 0},items:function e(t,n,r){r||(r={});for(var e=n.items,i=[],o=j.isArray(e),a=t.length,s=0;a>s;s++)o&&(e=n.items[s]),r.prop=s,i=i.concat(de(t[s],e,r)||[]);return i.length?i:void 0},maximum:function e(t,n,r){var e=n.maximum,i=n.exclusiveMaximum;return("undefined"==typeof t?"undefined":u.typeof(t))===("undefined"==typeof e?"undefined":u.typeof(e))&&(i?t>e:t>=e)?Z(t,"no more than "+e,r):void 0},maxItems:function(e,t,n){return te("maxItems",e,t,n)},maxLength:function(e,t,n){return te("maxLength",e,t,n)},maxProperties:function e(t,n,r){var e=n.maxProperties,i=Object.keys(t).length;return i>e?Z(i,"no more than "+e+" properties",r):void 0},minimum:function e(t,n,r){var e=n.minimum,i=n.exclusiveMinimum;return("undefined"==typeof t?"undefined":u.typeof(t))===("undefined"==typeof e?"undefined":u.typeof(e))&&(i?e>t:e>=t)?Z(t,"no less than "+e,r):void 0},minItems:function(e,t,n){return ne("minItems",e,t,n)},minLength:function(e,t,n){return ne("minLength",e,t,n)},minProperties:function e(t,n,r){var e=n.minProperties,i=Object.keys(t).length;return e>i?Z(i,"no more than "+e+" properties",r):void 0},multipleOf:function(e,t,n){},not:function(e,t,n){return de(e,t.not,n)?void 0:Z("succeeded","should have failed",n)},oneOf:function(e,t,n){var r=!1,i=[];return t.oneOf.forEach(function(t){var o=de(e,t,n);if(o)i=i.concat(o);else{if(r)return i=[Z("valid against more than one","valid against only one",n)],r=!1,!1;r=!0}}),r?void 0:i},pattern:function e(t,n,r){var e=n.pattern;return j.isString(t)&&!t.match(e)?Z(t,e,r):void 0},properties:function e(t,n,r){r||(r={});var i=j.isUndefined(n.additionalProperties)?!0:n.additionalProperties,o={},e=n.properties||{},a=n.patternProperties||{},s=[];j.forOwn(t,function(e,t){o[t]=void 0}),j.forOwn(e||{},function(e,n){j.isUndefined(t[n])&&!j.isUndefined(e.default)&&(t[n]=j.copy(e.default)),r.prop=n,s=s.concat(de(t[n],e,r)||[]),delete o[n]}),j.forOwn(a,function(e,n){j.forOwn(o,function(i,a){a.match(n)&&(r.prop=a,s=s.concat(de(t[a],e,r)||[]),delete o[a])})});var u=Object.keys(o);return i===!1?u.length&&ee("extra fields: "+u.join(", "),"no extra fields",r,s):j.isObject(i)&&u.forEach(function(e){r.prop=e,s=s.concat(de(t[e],i,r)||[])}),s.length?s:void 0},required:function e(t,n,r){var e=n.required,i=[];return r.existingOnly||e.forEach(function(e){if(j.isUndefined(j.get(t,e))){var n=r.prop;r.prop=e,ee(void 0,"a value",r,i),r.prop=n}}),i.length?i:void 0},type:function e(t,n,r){var e=n.type,i=void 0;if(j.isString(e)&&(e=[e]),e.forEach(function(e){return z[e](t,n,r)?(i=e,!1):void 0}),!i)return Z(j.isUndefined(t)||null===t?""+t:"undefined"==typeof t?"undefined":u.typeof(t),"one of ("+e.join(", ")+")",r);var o=_e[i];return o?o(t,n,r):void 0},uniqueItems:function(e,t,n){if(e&&e.length&&t.uniqueItems){var r=e.length,i=void 0,o=void 0,a=void 0;for(o=r-1;o>0;o--)for(i=e[o],a=o-1;a>=0;a--)if(i===e[a])return Z(i,"no duplicates",n)}}},ie=function(e,t,n,r){return!j.isUndefined(n[e])&&re[e](t,n,r)},oe=function(e,t,n,r){var i=[];return e.forEach(function(e){i=i.concat(ie(e,t,n,r)||[])}),i.length?i:void 0},ae=["enum","type","allOf","anyOf","oneOf","not"],se=["items","maxItems","minItems","uniqueItems"],ue=["multipleOf","maximum","minimum"],ce=["maxProperties","minProperties","required","properties","dependencies"],le=["maxLength","minLength","pattern"],fe=function(e,t,n){return oe(ae,e,t,n)},de=function e(t,n,r){var i=[];r||(r={});var o=void 0,a=r.prop;if(!j.isUndefined(n)){if(!j.isObject(n))throw j.err(H+"#validate")(500,'Invalid schema at path: "'+r.path+'"');return j.isUndefined(r.path)&&(r.path=[]),j.isUndefined(r.prop)||(o=!0,r.path.push(r.prop),r.prop=void 0),n.extends&&(i=j.isFunction(n.extends.validate)?i.concat(n.extends.validate(t,r)||[]):i.concat(e(t,n.extends,r)||[])),j.isUndefined(t)?(n.required!==!0||r.existingOnly||ee(t,"a value",r,i),o&&(r.path.pop(),r.prop=a),i.length?i:void 0):(i=i.concat(fe(t,n,r)||[]),o&&(r.path.pop(),r.prop=a),i.length?i:void 0)}},he="changing",pe="changed",ve="creating",ge="eventId",ye="noValidate",me="silent",be="validation failed",Ae=function(e,t,n){ | ||
var r={enumerable:j.isUndefined(t.enumerable)?!0:!!t.enumerable},i="props."+e,o="previous."+e,a=n.getter,s=n.setter,u=n.unsetter;return r.get=function(){return this._get(i)},r.set=function(n){var r=this,c=this[a],l=this[s],f=this[u];if(!c(ye)){var d=t.validate(n);if(d){var h=new Error(be);throw h.errors=d,h}}return t.track&&!c(ve)&&!function(){var t=c(o),a=c(i),s=c(he),u=c(pe);s||(u=[]);var d=u.indexOf(e);a!==n&&-1===d&&u.push(e),t===n&&d>=0&&u.splice(d,1),u.length||(s=!1,f(he),f(pe),c(ge)&&(clearTimeout(c(ge)),f(ge))),!s&&u.length&&(l(pe,u),l(he,!0),l(ge,setTimeout(function(){if(f(pe),f(ge),f(he),!c(me)){var e=void 0;for(e=0;e<u.length;e++)r.emit("change:"+u[e],r,j.get(r,u[e]));r.emit("change",r,r.changes())}f(me)},0)))}(),l(i,n),n},r},_e={array:function(e,t,n){return oe(se,e,t,n)},integer:function(e,t,n){return _e.numeric(e,t,n)},number:function(e,t,n){return _e.numeric(e,t,n)},numeric:function(e,t,n){return oe(ue,e,t,n)},object:function(e,t,n){return oe(ce,e,t,n)},string:function(e,t,n){return oe(le,e,t,n)}},Oe=t.extend({constructor:function e(t){t||(t={}),j.fillIn(this,t),t.properties&&j.forOwn(t.properties,function(n,r){n instanceof e||(t.properties[r]=new e(n))})},apply:function(e,t){t||(t={}),t.getter=t.getter||"_get",t.setter=t.setter||"_set",t.unsetter=t.unsetter||"_unset";var n=this.properties||{};j.forOwn(n,function(n,r){Object.defineProperty(e,r,Ae(r,n,t))})},validate:function(e,t){return de(e,this,t)}},{typeGroupValidators:_e,types:z,validate:de,validationKeywords:re}),we="Mapper",xe=["beforeCreate","beforeCreateMany","beforeUpdate","beforeUpdateAll","beforeUpdateMany"],Ce=function(e){return function(){for(var t=this,n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];var o=r[r.length-e],a=o.op;if(this.dbg.apply(this,[a].concat(r)),-1!==xe.indexOf(a)&&o.validate!==!1){var s=o.existingOnly;0===a.indexOf("beforeUpdate")&&j.isUndefined(o.existingOnly)&&(o.existingOnly=!0);var u=this.validate(r["beforeUpdate"===a?1:0],j.pick(o,["existingOnly"]));if(o.existingOnly=s,u)return j.reject(u)}(o.notify||void 0===o.notify&&this.notify)&&setTimeout(function(){t.emit.apply(t,[a].concat(r))})}},je=Ce(1),Ie=Ce(2),Ee={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(e,t,n,r){return[t,e.toJSON(n,r),r]},beforeAssign:1,defaults:[void 0,{},{}],types:[]},updateAll:{adapterArgs:function(e,t,n,r){return[e.toJSON(t,r),n,r]},beforeAssign:0,defaults:[{},{},{}],types:[]},updateMany:{adapterArgs:function(e,t,n){return[t.map(function(t){return e.toJSON(t,n)}),n]},beforeAssign:0,defaults:[[],{}],types:[]}},Fe={_adapters:{},applySchema:!0,debug:!1,defaultAdapter:"http",idAttribute:"id",notify:!0,raw:!1},Re=t.extend({constructor:function e(t){var n=this;if(j.classCallCheck(this,e),e.__super__.call(this),t||(t={}),Object.defineProperties(this,{_adapters:{value:void 0,writable:!0},recordClass:{value:void 0,writable:!0},lifecycleMethods:{value:Ee},schema:{value:void 0,writable:!0}}),j.fillIn(this,t),j.fillIn(this,j.copy(Fe)),!this.name)throw j.err("new "+we,"opts.name")(400,"string",this.name);this.schema instanceof Oe||(this.schema=new Oe(this.schema||{})),this.schema instanceof Oe&&(this.schema.type||(this.schema.type="object")),j.isUndefined(this.recordClass)&&!function(){var e=V;n.recordClass=e.extend({constructor:function(){var t=function(n,r){j.classCallCheck(this,t),e.call(this,n,r)};return t}()})}(),this.recordClass&&(this.recordClass.mapper=this,j.getSuper(this.recordClass,!0)===V&&this.schema&&this.schema.apply&&this.applySchema&&this.schema.apply(this.recordClass.prototype))},afterCount:Ie,afterCreate:Ie,afterCreateMany:Ie,afterDestroy:Ie,afterDestroyAll:Ie,afterFind:Ie,afterFindAll:Ie,afterSum:Ie,afterUpdate:Ie,afterUpdateAll:Ie,afterUpdateMany:Ie,beforeCreate:je,beforeCreateMany:je,beforeCount:je,beforeDestroy:je,beforeDestroyAll:je,beforeFind:je,beforeFindAll:je,beforeSum:je,beforeUpdate:je,beforeUpdateAll:je,beforeUpdateMany:je,_end:function(e,t,n){if(t.raw&&j._(e,t),n)return e;var r=t.raw?e.data:e;return r&&j.isFunction(this.wrap)&&(r=this.wrap(r,t),t.raw?e.data=r:e=r),e},belongsTo:function(e,t){return Q(e,t)(this)},count:function(e,t){return this.crud("count",e,t)},create:function(e,t){var n=this,r=void 0,i=void 0;return e||(e={}),t||(t={}),j._(t,this),i=t.adapter=this.getAdapterName(t),r=t.op="beforeCreate",j.resolve(this[r](e,t)).then(function(o){e=j.isUndefined(o)?e:o;var a={};t.with||(t.with=[]);var s=[];return j.forEachRelation(n,t,function(t,n){var r=t.getLocalField(e),i=t.getRelation(),o=i.idAttribute;n.raw=!1,r&&(t.type===N?s.push(i.create(r,n).then(function(n){t.setLocalField(a,n),t.setForeignKey(e,n)})):t.type===D&&t.localKeys&&s.push(i.createMany(r,n).then(function(n){t.setLocalField(a,n),j.set(e,t.localKeys,n.map(function(e){return j.get(e,o)}))})))}),j.Promise.all(s).then(function(){return r=t.op="create",n.dbg(r,e,t),j.resolve(n.getAdapter(i)[r](n,n.toJSON(e,{with:t.pass||[]}),t))}).then(function(r){var i=t.raw?r.data:r;return s=[],j.forEachRelation(n,t,function(t,n){var r=t.getLocalField(e);if(r){n.raw=!1;var o=void 0;t.type===D&&t.foreignKey?(t.setForeignKey(i,r),o=t.getRelation().createMany(r,n).then(function(e){t.setLocalField(i,e)})):t.type===T?(t.setForeignKey(i,r),o=t.getRelation().create(r,n).then(function(e){t.setLocalField(i,e)})):t.type===N&&t.getLocalField(a)?t.setLocalField(i,t.getLocalField(a)):t.type===D&&t.localKeys&&t.getLocalField(a)&&t.setLocalField(i,t.getLocalField(a)),o&&s.push(o)}}),j.Promise.all(s).then(function(){return r})})}).then(function(i){return i=n._end(i,t),r=t.op="afterCreate",j.resolve(n[r](e,t,i)).then(function(e){return j.isUndefined(e)?i:e})})},createInstance:function(e,t){return this.createRecord(e,t)},createMany:function(e,t){var n=this,r=void 0,i=void 0;return e||(e=[]),t||(t={}),j._(t,this),i=t.adapter=this.getAdapterName(t),r=t.op="beforeCreateMany",j.resolve(this[r](e,t)).then(function(o){e=j.isUndefined(o)?e:o;var a={};t.with||(t.with=[]);var s=[];return j.forEachRelation(n,t,function(t,n){var r=e.map(function(e){return t.getLocalField(e)}).filter(function(e){return e});t.type===N&&r.length===e.length&&s.push(t.getRelation().createMany(r,n).then(function(r){var i=n.raw?r.data:r;t.setLocalField(a,i),e.forEach(function(e,n){t.setForeignKey(e,i[n])})}))}),j.Promise.all(s).then(function(){r=t.op="createMany";var o=e.map(function(e){return n.toJSON(e,{with:t.pass||[]})});return n.dbg(r,e,t),j.resolve(n.getAdapter(i)[r](n,o,t))}).then(function(r){var i=t.raw?r.data:r;return s=[],j.forEachRelation(n,t,function(r,o){var u=e.map(function(e){return r.getLocalField(e)}).filter(function(e){return e});if(u.length===e.length){var c=r.getLocalField(a),l=void 0;r.type===D?n.log("warn","deep createMany of hasMany type not supported!"):r.type===T?(i.forEach(function(e,t){r.setForeignKey(e,u[t])}),l=r.getRelation().createMany(u,o).then(function(e){var n=t.raw?e.data:e;i.forEach(function(e,t){r.setLocalField(e,n[t])})})):r.type===N&&c&&c.length===i.length&&i.forEach(function(e,t){r.setLocalField(e,c[t])}),l&&s.push(l)}}),j.Promise.all(s).then(function(){return r})})}).then(function(i){return i=n._end(i,t),r=t.op="afterCreateMany",j.resolve(n[r](e,t,i)).then(function(e){return j.isUndefined(e)?i:e})})},createRecord:function(e,t){var n=this;if(e||(e={}),j.isArray(e))return e.map(function(e){return n.createRecord(e,t)});if(!j.isObject(e))throw j.err(we+"#createRecord","props")(400,"array or object",e);var r=this.recordClass,i=this.relationList||[];return i.forEach(function(n){var r=n.getRelation(),i=n.getLocalField(e);if(i&&!r.is(i)){if(j.isArray(i)&&(!i.length||r.is(i[0])))return;j.set(e,n.localField,r.createRecord(i,t))}}),r?e instanceof r?e:new r(e,t):e},crud:function(e){for(var t=this,n=arguments.length,r=Array(n>1?n-1:0),i=1;n>i;i++)r[i-1]=arguments[i];var o=this.lifecycleMethods[e];if(!o)throw j.err(we+"#crud",e)(404,"method");var a=""+e.charAt(0).toUpperCase()+e.substr(1),s="before"+a,c="after"+a,l=void 0,f=void 0;o.defaults.forEach(function(e,t){j.isUndefined(r[t])&&(r[t]=j.copy(e))});var d=r[r.length-1];return j._(d,this),f=d.adapter=this.getAdapterName(d),l=d.op=s,j.resolve(this[l].apply(this,u.toConsumableArray(r))).then(function(n){var i;return j.isUndefined(r[o.beforeAssign])||(r[o.beforeAssign]=j.isUndefined(n)?r[o.beforeAssign]:n),l=d.op=e,r=o.adapterArgs?o.adapterArgs.apply(o,[t].concat(u.toConsumableArray(r))):r,t.dbg.apply(t,[l].concat(u.toConsumableArray(r))),j.resolve((i=t.getAdapter(f))[l].apply(i,[t].concat(u.toConsumableArray(r))))}).then(function(e){return e=t._end(e,d,!!o.skip),r.push(e),l=d.op=c,j.resolve(t[l].apply(t,u.toConsumableArray(r))).then(function(t){return j.isUndefined(t)?e:t})})},destroy:function(e,t){return this.crud("destroy",e,t)},destroyAll:function(e,t){return this.crud("destroyAll",e,t)},find:function(e,t){return this.crud("find",e,t)},findAll:function(e,t){return this.crud("findAll",e,t)},getAdapter:function(e){this.dbg("getAdapter","name:",e);var t=this.getAdapterName(e);if(!t)throw j.err(we+"#getAdapter","name")(400,"string",e);return this.getAdapters()[t]},getAdapterName:function(e){return e||(e={}),j.isString(e)&&(e={adapter:e}),e.adapter||e.defaultAdapter},getAdapters:function(){return this._adapters},getSchema:function(){return this.schema},hasMany:function(e,t){return B(e,t)(this)},hasOne:function(e,t){return G(e,t)(this)},is:function(e){var t=this.recordClass;return t?e instanceof t:!1},registerAdapter:function(e,t,n){n||(n={}),this.getAdapters()[e]=t,(n===!0||n.default)&&(this.defaultAdapter=e)},sum:function(e,t,n){return this.crud("sum",e,t,n)},toJSON:function(e,t){var n=this,r=void 0;if(t||(t={}),j.isArray(e))return e.map(function(e){return n.toJSON(e,t)});r=e;var i=(this?this.relationFields:[])||[],o={},a=void 0;if(this&&this.schema&&(a=this.schema.properties||{},j.forOwn(a,function(e,t){o[t]=j.plainCopy(r[t])})),a||(a={}),!t.strict)for(var s in r)a[s]||-1!==i.indexOf(s)||(o[s]=j.plainCopy(r[s]));return this&&t.withAll&&(t.with=i.slice()),this&&t.with&&(j.isString(t.with)&&(t.with=[t.with]),j.forEachRelation(this,t,function(e,t){var n=e.getLocalField(r);n&&(j.isArray(n)?e.setLocalField(o,n.map(function(n){return e.getRelation().toJSON(n,t)})):e.setLocalField(o,e.getRelation().toJSON(n,t)))})),o},update:function(e,t,n){return this.crud("update",e,t,n)},updateAll:function(e,t,n){return this.crud("updateAll",e,t,n)},updateMany:function(e,t){return this.crud("updateMany",e,t)},validate:function(e,t){t||(t={});var n=this.getSchema(),r=j.pick(t,["existingOnly"]);if(!j.isArray(e))return n.validate(e,r);var i=e.map(function(e){return n.validate(e,j.pick(r,["existingOnly"]))}),o=!1;return i.forEach(function(e){e&&(o=!0)}),o?i:void 0},wrap:function(e,t){return this.createRecord(e,t)}}),Se="Container",ke=["count","create","createMany","createRecord","dbg","destroy","destroyAll","find","findAll","getSchema","is","log","sum","toJSON","update","updateAll","updateMany","validate"],Pe={constructor:function e(t){j.classCallCheck(this,e),e.__super__.call(this),t||(t={}),Object.defineProperties(this,{_adapters:{value:{}},_mappers:{value:{}}}),j.fillIn(this,t),this.mapperDefaults=this.mapperDefaults||{},this.mapperClass=this.mapperClass||Re},_onMapperEvent:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=n.shift();this.emit.apply(this,[i,e].concat(n))},as:function(e){var t=this,n={};return ke.forEach(function(t){n[t]={writable:!0,value:function(){var n;return(n=this.getMapper(e))[t].apply(n,arguments)}}}),n.getMapper={writable:!0,value:function(){return t.getMapper(e)}},Object.create(this,n)},defineMapper:function(e,t){var n=this;if(j.isObject(e)&&(t=e,e=t.name),!j.isString(e))throw j.err(Se+"#defineMapper","name")(400,"string",e);t||(t={}),t.name=e,t.relations||(t.relations={});var r=t.mapperClass||this.mapperClass;delete t.mapperClass,j.fillIn(t,this.mapperDefaults);var i=this._mappers[e]=new r(t);return i.relations||(i.relations={}),i.name=e,i._adapters=this.getAdapters(),i.datastore=this,i.on("all",function(){for(var t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];return n._onMapperEvent.apply(n,[e].concat(r))}),j.forOwn(i.relations,function(e,t){j.forOwn(e,function(e,r){j.isObject(e)&&(e=[e]),e.forEach(function(e){e.getRelation=function(){return n.getMapper(r)};var o=n._mappers[r]||r;t===N?i.belongsTo(o,e):t===T?i.hasOne(o,e):t===D&&i.hasMany(o,e)})})}),i},defineResource:function(e,t){return console.warn("DEPRECATED: defineResource is deprecated, use defineMapper instead"),this.defineMapper(e,t)},getAdapter:function(e){var t=this.getAdapterName(e);if(!t)throw j.err(Se+"#getAdapter","name")(400,"string",e);return this.getAdapters()[t]},getAdapterName:function(e){return e||(e={}),j.isString(e)&&(e={adapter:e}),e.adapter||this.mapperDefaults.defaultAdapter},getAdapters:function(){return this._adapters},getMapper:function(e){var t=this._mappers[e];if(!t)throw j.err(Se+"#getMapper",e)(404,"mapper");return t},registerAdapter:function(e,t,n){n||(n={}),this.getAdapters()[e]=t,(n===!0||n.default)&&(this.mapperDefaults.defaultAdapter=e,j.forOwn(this._mappers,function(t){t.defaultAdapter=e}))}};ke.forEach(function(e){Pe[e]=function(t){for(var n,r=arguments.length,i=Array(r>1?r-1:0),o=1;r>o;o++)i[o-1]=arguments[o];return(n=this.getMapper(t))[e].apply(n,i)}});var Me=t.extend(Pe),Ke="LinkedCollection",Le=U.extend({constructor:function e(t,n){if(j.classCallCheck(this,e),Object.defineProperties(this,{_added:{value:{}},datastore:{writable:!0,value:void 0}}),e.__super__.call(this,t,n),!this.datastore)throw j.err("new "+Ke,"opts.datastore")(400,"DataStore",this.datastore);return this},_onRecordEvent:function(){for(var e=arguments.length,t=Array(e),n=0;e>n;n++)t[n]=arguments[n];j.getSuper(this).prototype._onRecordEvent.apply(this,t);var r=t[0];j.isString(r)&&0===r.indexOf("change")&&this.updateIndexes(t[1])},add:function(e,t){var n=this,r=this.datastore,i=this.mapper,o=i.relationList,a=(new Date).getTime(),s=!!i.recordClass,c=i.idAttribute,l=void 0;return j.isObject(e)&&!j.isArray(e)&&(l=!0,e=[e]),e=j.getSuper(this).prototype.add.call(this,e,t),o.length&&e.length&&i.relationList.forEach(function(t){var n=t.relation,i=r.getMapper(n),o=i.idAttribute,a=t.foreignKey,s=r.getCollection(n),l=t.type,f=l===D,d=j.isUndefined(t.add)?!0:!!t.add,h=void 0;e.forEach(function(e){h=t.getLocalField(e);var n=j.get(e,c);if(j.isFunction(t.add))h=t.add(r,t,e);else if(h)if(f)h=h.map(function(n){return n!==s.get(s.recordId(n))&&(a&&t.setForeignKey(e,n),d&&(n=s.add(n))),n});else{var i=j.get(h,o);h!==s.get(i)&&(t.setForeignKey(e,h),d&&(h=s.add(h)))}if(!h||j.isArray(h)&&!h.length)if(l===N){var p=j.get(e,a);j.isUndefined(p)||(h=s.get(p))}else if(l===T){var v=s.filter(u.defineProperty({},a,n));h=v.length?v[0]:void 0}else if(l===D)if(a){var g=s.filter(u.defineProperty({},a,n));h=g.length?g:void 0}else if(t.localKeys&&j.get(e,t.localKeys)){var y=s.filter({where:u.defineProperty({},o,{in:j.get(e,t.localKeys)})});h=y.length?y:void 0}else if(t.foreignKeys){var m=s.filter({where:u.defineProperty({},t.foreignKeys,{contains:n})});h=m.length?m:void 0}h&&t.setLocalField(e,h)})}),e.forEach(function(e){n._added[n.recordId(e)]=a,s&&e._set("$",a)}),l?e[0]:e},remove:function(e,t){var n=this.mapper,r=j.getSuper(this).prototype.remove.call(this,e,t);return r&&(delete this._added[e],n.recordClass&&r._set("$")),r},removeAll:function(e,t){var n=this,r=this.mapper,i=j.getSuper(this).prototype.removeAll.call(this,e,t);return i.forEach(function(e){delete n._added[n.recordId(e)],r.recordClass&&e._set("$")}),i}}),Ue="DataStore",Ne=["add","between","createIndex","filter","get","getAll","query","toJson"],De=["addToCache","cachedFind","cachedFindAll","cacheFind","cacheFindAll","hashQuery"],Te=function(e,t,n){e&&e._set?e._set(t,n):j.set(e,t,n)},Je=function(e,t,n){var r=this._completedQueries[e][t];return j.isFunction(r)?r(e,t,n):r},qe={constructor:function e(t){return j.classCallCheck(this,e),e.__super__.call(this,t),this.collectionClass=this.collectionClass||Le,this._collections={},this._pendingQueries={},this._completedQueries={},this},_callSuper:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];return this.constructor.__super__.prototype[e].apply(this,n)},_end:function(e,t,n){var r=n.raw?t.data:t;return r&&j.isFunction(this.addToCache)&&(r=this.addToCache(e,r,n),n.raw?t.data=r:t=r),t},_onCollectionEvent:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;t>r;r++)n[r-1]=arguments[r];var i=n.shift();this.emit.apply(this,[i,e].concat(n))},addToCache:function(e,t,n){return this.getCollection(e).add(t,n)},as:function(e){var t=this,n={};return De.forEach(function(t){n[t]={writable:!0,value:function(){for(var n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];return this[t].apply(this,[e].concat(r))}}}),ke.forEach(function(t){n[t]={writable:!0,value:function(){var n;return(n=this.getMapper(e))[t].apply(n,arguments)}}}),n.getMapper={writable:!0,value:function(){return t.getMapper(e)}},Ne.forEach(function(t){n[t]={writable:!0,value:function(){var n;return(n=this.getCollection(e))[t].apply(n,arguments)}}}),n.getCollection={writable:!0,value:function(){return t.getCollection(e)}},Object.create(this,n)},cachedFind:Je,cachedFindAll:Je,cacheFind:function(e,t,n,r){var i=this;this._completedQueries[e][n]=function(e,t,n){return i.get(e,t)}},cacheFindAll:function(e,t,n,r){var i=this;this._completedQueries[e][n]=function(e,t,n){return i.filter(e,j.fromJson(t))}},clear:function(){var e={};return j.forOwn(this._collections,function(t,n){e[n]=t.removeAll()}),e},create:function(e,t,n){var r=this;return n||(n={}),this._callSuper("create",e,t,n).then(function(t){return r._end(e,t,n)})},createMany:function(e,t,n){var r=this;return n||(n={}),this._callSuper("createMany",e,t,n).then(function(t){return r._end(e,t,n)})},defineMapper:function(e,t){var n=this,r=j.getSuper(n).prototype.defineMapper.call(n,e,t);n._pendingQueries[e]={},n._completedQueries[e]={},r.relationList||Object.defineProperty(r,"relationList",{value:[]});var i=n._collections[e]=new n.collectionClass(null,{_added:{},datastore:n,mapper:r}),o=r.schema||{},a=o.properties||{};j.forOwn(a,function(e,t){e.indexed&&i.createIndex(t)}),i.createIndex("addedTimestamps",["$"],{fieldGetter:function(e){return i._added[i.recordId(e)]}}),i.on("all",function(){for(var t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];n._onCollectionEvent.apply(n,[e].concat(r))});var s=r.idAttribute;return r.relationList.forEach(function(e){var t=e.relation,o=e.localField,a="links."+o,u=e.foreignKey,c=e.type,l={index:u},f=void 0,d=function(){return this._get(a)};c===N?(i.indexes[u]||i.createIndex(u),f={get:d,set:function(o){var c=this,f=this._get(a);if(o===f)return f;var d=j.get(c,s),h=e.getInverse(r);if(o){var p=e.getRelation().idAttribute,v=j.get(o,p);if(j.isUndefined(v)||(o=n.get(t,v)||o),c._set(a,o),Te(c,u,v),i.updateIndex(c,l),h.type===T)j.set(o,h.localField,c);else if(h.type===D){var g=j.get(o,h.localField);j.noDupeAdd(g,c,function(e){return d===j.get(e,s)})}}else c._set(a,void 0),Te(c,u,void 0),i.updateIndex(c,l);if(f)if(h.type===T)j.set(f,h.localField,void 0);else if(h.type===D){var y=j.get(f,h.localField);j.remove(y,function(e){return d===j.get(e,s)})}return o}},r.recordClass.prototype.hasOwnProperty(u)&&!function(){var e=r.recordClass;r.recordClass=e.extend({constructor:function(){var t=function(n,r){j.classCallCheck(this,t),e.call(this,n,r)};return t}()})}(),Object.defineProperty(r.recordClass.prototype,u,{enumerable:!0,get:function(){return this._get(u)},set:function(e){var r=this;if(j.isUndefined(e))j.set(r,o,void 0);else{Te(r,u,e);var i=n.get(t,e);i&&j.set(r,o,i)}}})):c===D?!function(){var i=e.localKeys,o=e.foreignKeys;n._collections[t]&&u&&!n.getCollection(t).indexes[u]&&n.getCollection(t).createIndex(u),f={get:function(){var e=this,t=d.call(e);return t||e._set(a,[]),d.call(e)},set:function(c){var f=this;c||(c=[]),c&&!j.isArray(c)&&(c=[c]);var d=j.get(f,s),h=e.getRelation().idAttribute,p=e.getInverse(r),v=p.localField,g=f._get(a);g||(g=[]);var y=g;g=[];var m={};return c.forEach(function(e){var r=j.get(e,h);j.isUndefined(r)||(e=n.get(t,r)||e,m[r]=e),g.push(e)}),u?(c.forEach(function(e){Te(e,u,d),n.getCollection(t).updateIndex(e,l),j.set(e,v,f)}),y.forEach(function(e){var r=j.get(e,h);j.isUndefined(r)||m.hasOwnProperty(r)||(Te(e,u,void 0),n.getCollection(t).updateIndex(e,l),j.set(e,v,void 0))})):i?!function(){var e=[];c.forEach(function(t){j.set(t,v,f),e.push(j.get(t,h))}),j.set(f,i,e),y.forEach(function(e){var t=j.get(e,h);j.isUndefined(t)||m.hasOwnProperty(t)||j.set(e,v,void 0)})}():o&&(y.forEach(function(e){var t=j.get(e,o)||[];j.remove(t,function(e){return d===e});var n=j.get(e,v)||[];j.remove(n,function(e){return d===j.get(e,s)})}),c.forEach(function(e){var t=j.get(e,o)||[];j.noDupeAdd(t,d,function(e){return d===e});var n=j.get(e,v)||[];j.noDupeAdd(n,f,function(e){return d===j.get(e,s)})})),f._set(a,g),g}}}():c===T&&(n._collections[t]&&u&&!n.getCollection(t).indexes[u]&&n.getCollection(t).createIndex(u),f={get:d,set:function(i){var o=this,c=this._get(a);if(i===c)return c;var f=j.get(i,e.getRelation().idAttribute),d=e.getInverse(r).localField;return c&&(Te(c,u,void 0),n.getCollection(t).updateIndex(c,l),j.set(c,d,void 0)),i?(j.isUndefined(f)||(i=n.get(t,f)||i),o._set(a,i),Te(i,u,j.get(o,s)),n.getCollection(t).updateIndex(i,l),j.set(i,d,o)):o._set(a,void 0),i}}),f&&(f.enumerable=j.isUndefined(e.enumerable)?!1:e.enumerable,e.get&&!function(){var t=f.get;f.get=function(){var n=this;return e.get(e,this,function(){for(var e=arguments.length,r=Array(e),i=0;e>i;i++)r[i]=arguments[i];return t.apply(n,r)})}}(),e.set&&!function(){var t=f.set;f.set=function(n){var r=this;return e.set(e,this,n,function(e){return t.call(r,void 0===e?n:e)})}}(),Object.defineProperty(r.recordClass.prototype,o,f))}),r},destroy:function(e,t,n){var r=this;return n||(n={}),this._callSuper("destroy",e,t,n).then(function(i){return n.raw?i.data=r.getCollection(e).remove(t,n):i=r.getCollection(e).remove(t,n),delete r._pendingQueries[e][t],delete r._completedQueries[e][t],i})},destroyAll:function(e,t,n){var r=this;return n||(n={}),this._callSuper("destroyAll",e,t,n).then(function(i){n.raw?i.data=r.getCollection(e).removeAll(t,n):i=r.getCollection(e).removeAll(t,n);var o=r.hashQuery(e,t,n);return delete r._pendingQueries[e][o],delete r._completedQueries[e][o],i})},eject:function(e,t,n){return console.warn('DEPRECATED: "eject" is deprecated, use "remove" instead'),this.remove(e,t,n)},ejectAll:function(e,t,n){return console.warn('DEPRECATED: "ejectAll" is deprecated, use "removeAll" instead'),this.removeAll(e,t,n)},find:function(e,t,n){var r=this;n||(n={});var i=this._pendingQueries[e][t];if(j.fillIn(n,this.getMapper(e)),i)return i;var o=this.cachedFind(e,t,n),a=void 0;return a=n.force||!o?this._pendingQueries[e][t]=this._callSuper("find",e,t,n).then(function(i){return delete r._pendingQueries[e][t],i=r._end(e,i,n),r.cacheFind(e,i,t,n),i},function(n){return delete r._pendingQueries[e][t],j.reject(n)}):j.resolve(o)},findAll:function(e,t,n){var r=this;n||(n={});var i=this.hashQuery(e,t,n),o=this._pendingQueries[e][i];if(j.fillIn(n,this.getMapper(e)),o)return o;var a=this.cachedFindAll(e,i,n),s=void 0;return s=n.force||!a?this._pendingQueries[e][i]=this._callSuper("findAll",e,t,n).then(function(t){return delete r._pendingQueries[e][i],t=r._end(e,t,n),r.cacheFindAll(e,t,i,n),t},function(t){return delete r._pendingQueries[e][i],j.reject(t)}):j.resolve(a)},getCollection:function(e){var t=this._collections[e];if(!t)throw j.err(Ue+"#getCollection",e)(404,"collection");return t},hashQuery:function(e,t,n){return j.toJson(t)},inject:function(e,t,n){return console.warn('DEPRECATED: "inject" is deprecated, use "add" instead'),this.add(e,t,n)},remove:function(e,t,n){var r=this.getCollection(e).remove(t,n);return r&&this.removeRelated(e,[r],n),r},removeAll:function(e,t,n){var r=this.getCollection(e).removeAll(t,n);return r.length&&this.removeRelated(e,r,n),r},removeRelated:function(e,t,n){var r=this;j.forEachRelation(this.getMapper(e),n,function(e,n){t.forEach(function(t){var i=void 0,o=void 0;if(!e.foreignKey||e.type!==T&&e.type!==D?e.type===D&&e.localKeys?o={where:u.defineProperty({},e.getRelation().idAttribute,{in:j.get(t,e.localKeys)})}:e.type===D&&e.foreignKeys?o={where:u.defineProperty({},e.foreignKeys,{contains:e.getForeignKey(t)})}:e.type===N&&(i=r.remove(e.relation,e.getForeignKey(t),n)):o=u.defineProperty({},e.foreignKey,e.getForeignKey(t)),o&&(i=r.removeAll(e.relation,o,n)),i){if(j.isArray(i)&&!i.length)return;e.type===T&&(i=i[0]),e.setLocalField(t,i)}})})},update:function(e,t,n,r){var i=this;return r||(r={}),this._callSuper("update",e,t,n,r).then(function(t){return i._end(e,t,r)})},updateAll:function(e,t,n,r){var i=this;return r||(r={}),this._callSuper("updateAll",e,n,t,r).then(function(t){return i._end(e,t,r)})},updateMany:function(e,t,n){var r=this;return n||(n={}),this._callSuper("updateMany",e,t,n).then(function(t){return r._end(e,t,n)})}};Ne.forEach(function(e){qe[e]=function(t){for(var n,r=arguments.length,i=Array(r>1?r-1:0),o=1;r>o;o++)i[o-1]=arguments[o];return(n=this.getCollection(t))[e].apply(n,i)}});var Qe=Me.extend(qe),Be={beta:6,full:"3.0.0-beta.6",major:3,minor:0,patch:0};e.version=Be,e.Collection=U,e.Component=t,e.Container=Me,e.DataStore=Qe,e.Index=a,e.LinkedCollection=Le,e.Mapper=Re,e.Query=M,e.Record=V,e.Schema=Oe,e.utils=j,e.belongsToType=N,e.hasManyType=D,e.hasOneType=T,e.belongsTo=Q,e.hasMany=B,e.hasOne=G}); | ||
//# sourceMappingURL=js-data.min.map |
{ | ||
"name": "js-data", | ||
"description": "Robust, framework-agnostic in-memory data store.", | ||
"version": "3.0.0-beta.5", | ||
"version": "3.0.0-beta.6", | ||
"homepage": "http://www.js-data.io", | ||
@@ -40,2 +40,3 @@ "repository": { | ||
"beforeEach", | ||
"after", | ||
"describe", | ||
@@ -68,4 +69,3 @@ "it" | ||
"test": "npm run build && npm run cover && npm run karma", | ||
"release": "npm test && npm run doc && repo-tools updates && repo-tools changelog && repo-tools authors", | ||
"ci": "npm test && nyc report --reporter=lcov | codecov" | ||
"release": "npm test && npm run doc && repo-tools updates && repo-tools changelog && repo-tools authors" | ||
}, | ||
@@ -78,3 +78,3 @@ "devDependencies": { | ||
"babel-preset-es2015-rollup": "1.1.1", | ||
"js-data-repo-tools": "0.5.0", | ||
"js-data-repo-tools": "0.5.1", | ||
"karma": "0.13.22", | ||
@@ -88,8 +88,8 @@ "karma-babel-preprocessor": "6.0.1", | ||
"karma-sinon": "1.0.5", | ||
"nyc": "6.4.3", | ||
"nyc": "6.4.4", | ||
"phantomjs-prebuilt": "2.1.7", | ||
"rollup": "0.26.2", | ||
"rollup": "0.26.3", | ||
"rollup-plugin-babel": "2.4.0", | ||
"uglify-js": "2.6.2" | ||
} | ||
} | ||
} |
<img src="https://raw.githubusercontent.com/js-data/js-data/master/js-data.png" alt="js-data logo" title="js-data" align="right" width="96" height="96" /> | ||
# [js-data v3 beta](http://www.js-data.io/) [![Slack Status][b1]][b2] [![npm version][b3]][b4] [![npm downloads][b5]][b6] [![coverage][b15]][b16] | ||
# [js-data v3 beta](http://www.js-data.io/) [![Slack Status][b1]][b2] [![npm version][b3]][b4] [![npm downloads][b5]][b6] [![coverage][bcov]][lcov] | ||
| __Browser tests__ | __Node.js tests__ | | ||
| ---------------------------------|----| | ||
| [![Sauce Test Status][b7]][b8] | [![Circle CI][b9]][b10] [![Circle CI][b11]][b10] [![Circle CI][b12]][b10] [![Circle CI][b13]][b10] [![Circle CI][b14]][b10] | | ||
| [![Sauce Test Status][b7]][b8] | [![Circle CI][b9]][b10] [![Circle CI][b11]][b10] [![Circle CI][b12]][b10] [![Circle CI][b13]][b10] [![Circle CI][b14]][b10] [![Circle CI][b15]][b10] | | ||
@@ -19,6 +19,9 @@ [b1]: http://slack.js-data.io/badge.svg | ||
[b10]: https://circleci.com/gh/js-data/js-data | ||
[b11]: https://img.shields.io/badge/Node.js-v5.x-brightgreen.svg | ||
[b12]: https://img.shields.io/badge/Node.js-v4.x-brightgreen.svg | ||
[b13]: https://img.shields.io/badge/Node.js-v0.12.x-brightgreen.svg | ||
[b14]: https://img.shields.io/badge/Node.js-v0.10.x-brightgreen.svg | ||
[b11]: https://img.shields.io/badge/Node.js-v6.x-brightgreen.svg | ||
[b12]: https://img.shields.io/badge/Node.js-v5.x-brightgreen.svg | ||
[b13]: https://img.shields.io/badge/Node.js-v4.x-brightgreen.svg | ||
[b14]: https://img.shields.io/badge/Node.js-v0.12.x-brightgreen.svg | ||
[b15]: https://img.shields.io/badge/Node.js-v0.10.x-brightgreen.svg | ||
[bcov]: https://img.shields.io/codecov/c/github/js-data/js-data/master.svg | ||
[lcov]: https://codecov.io/github/js-data/js-data | ||
@@ -25,0 +28,0 @@ __JSData v3 beta is stable. More features may be added before 3.0.0. Some adapters |
@@ -68,5 +68,4 @@ import utils from './utils' | ||
constructor: function Collection (records, opts) { | ||
const self = this | ||
utils.classCallCheck(self, Collection) | ||
Collection.__super__.call(self) | ||
utils.classCallCheck(this, Collection) | ||
Collection.__super__.call(this) | ||
@@ -103,3 +102,3 @@ if (records && !utils.isArray(records)) { | ||
*/ | ||
Object.defineProperties(self, { | ||
Object.defineProperties(this, { | ||
mapper: { | ||
@@ -109,2 +108,3 @@ value: undefined, | ||
}, | ||
// Query class used by this collection | ||
queryClass: { | ||
@@ -117,13 +117,13 @@ value: undefined, | ||
// Apply user-provided configuration | ||
utils.fillIn(self, opts) | ||
utils.fillIn(this, opts) | ||
// Fill in any missing options with the defaults | ||
utils.fillIn(self, utils.copy(COLLECTION_DEFAULTS)) | ||
utils.fillIn(this, utils.copy(COLLECTION_DEFAULTS)) | ||
if (!self.queryClass) { | ||
self.queryClass = Query | ||
if (!this.queryClass) { | ||
this.queryClass = Query | ||
} | ||
const idAttribute = self.recordId() | ||
const idAttribute = this.recordId() | ||
Object.defineProperties(self, { | ||
Object.defineProperties(this, { | ||
/** | ||
@@ -156,3 +156,3 @@ * The main index, which uses @{link Collection#recordId} as the key. | ||
if (records) { | ||
self.add(records) | ||
this.add(records) | ||
} | ||
@@ -192,4 +192,2 @@ }, | ||
add (records, opts) { | ||
const self = this | ||
// Default values for arguments | ||
@@ -199,8 +197,8 @@ opts || (opts = {}) | ||
// Fill in "opts" with the Collection's configuration | ||
utils._(opts, self) | ||
records = self.beforeAdd(records, opts) || records | ||
utils._(opts, this) | ||
records = this.beforeAdd(records, opts) || records | ||
// Track whether just one record or an array of records is being inserted | ||
let singular = false | ||
const idAttribute = self.recordId() | ||
const idAttribute = this.recordId() | ||
if (!utils.isArray(records)) { | ||
@@ -219,4 +217,4 @@ if (utils.isObject(records)) { | ||
// option. | ||
records = records.map(function (record) { | ||
let id = self.recordId(record) | ||
records = records.map((record) => { | ||
let id = this.recordId(record) | ||
if (!utils.isSorN(id)) { | ||
@@ -226,3 +224,3 @@ throw utils.err(`${DOMAIN}#add`, `record.${idAttribute}`)(400, 'string or number', id) | ||
// Grab existing record if there is one | ||
const existing = self.get(id) | ||
const existing = this.get(id) | ||
// If the currently visited record is just a reference to an existing | ||
@@ -237,3 +235,3 @@ // record, then there is nothing to be done. Exit early. | ||
// in the collection, so we need to merge them | ||
const onConflict = opts.onConflict || self.onConflict | ||
const onConflict = opts.onConflict || this.onConflict | ||
if (onConflict === 'merge') { | ||
@@ -253,3 +251,3 @@ utils.deepMixIn(existing, record) | ||
// Update all indexes in the collection | ||
self.updateIndexes(record) | ||
this.updateIndexes(record) | ||
} else { | ||
@@ -259,9 +257,9 @@ // Here, the currently visted record does not correspond to any record | ||
// it into the collection | ||
record = self.mapper ? self.mapper.createRecord(record, opts) : record | ||
self.index.insertRecord(record) | ||
utils.forOwn(self.indexes, function (index, name) { | ||
record = this.mapper ? this.mapper.createRecord(record, opts) : record | ||
this.index.insertRecord(record) | ||
utils.forOwn(this.indexes, function (index, name) { | ||
index.insertRecord(record) | ||
}) | ||
if (record && utils.isFunction(record.on)) { | ||
record.on('all', self._onRecordEvent, self) | ||
record.on('all', this._onRecordEvent, this) | ||
} | ||
@@ -274,4 +272,4 @@ } | ||
// TODO: Make this more performant (batch events?) | ||
self.emit('add', result) | ||
return self.afterAdd(records, opts, result) || result | ||
this.emit('add', result) | ||
return this.afterAdd(records, opts, result) || result | ||
}, | ||
@@ -396,3 +394,2 @@ | ||
createIndex (name, fieldList, opts) { | ||
const self = this | ||
if (utils.isString(name) && fieldList === undefined) { | ||
@@ -402,8 +399,6 @@ fieldList = [name] | ||
opts || (opts = {}) | ||
opts.hashCode = opts.hashCode || function (obj) { | ||
return self.recordId(obj) | ||
} | ||
const index = self.indexes[name] = new Index(fieldList, opts) | ||
self.index.visitAll(index.insertRecord, index) | ||
return self | ||
opts.hashCode || (opts.hashCode = (obj) => this.recordId(obj)) | ||
const index = this.indexes[name] = new Index(fieldList, opts) | ||
this.index.visitAll(index.insertRecord, index) | ||
return this | ||
}, | ||
@@ -587,7 +582,6 @@ | ||
recordId (record) { | ||
const self = this | ||
if (record) { | ||
return utils.get(record, self.recordId()) | ||
return utils.get(record, this.recordId()) | ||
} | ||
return self.mapper ? self.mapper.idAttribute : self.idAttribute | ||
return this.mapper ? this.mapper.idAttribute : this.idAttribute | ||
}, | ||
@@ -644,21 +638,19 @@ | ||
remove (id, opts) { | ||
const self = this | ||
// Default values for arguments | ||
opts || (opts = {}) | ||
self.beforeRemove(id, opts) | ||
const record = self.get(id) | ||
this.beforeRemove(id, opts) | ||
const record = this.get(id) | ||
// The record is in the collection, remove it | ||
if (record) { | ||
self.index.removeRecord(record) | ||
utils.forOwn(self.indexes, function (index, name) { | ||
this.index.removeRecord(record) | ||
utils.forOwn(this.indexes, function (index, name) { | ||
index.removeRecord(record) | ||
}) | ||
if (record && utils.isFunction(record.off)) { | ||
record.off('all', self._onRecordEvent, self) | ||
self.emit('remove', record) | ||
record.off('all', this._onRecordEvent, this) | ||
this.emit('remove', record) | ||
} | ||
} | ||
return self.afterRemove(id, opts, record) || record | ||
return this.afterRemove(id, opts, record) || record | ||
}, | ||
@@ -680,13 +672,12 @@ | ||
removeAll (query, opts) { | ||
const self = this | ||
// Default values for arguments | ||
opts || (opts = {}) | ||
self.beforeRemoveAll(query, opts) | ||
const records = self.filter(query) | ||
this.beforeRemoveAll(query, opts) | ||
const records = this.filter(query) | ||
// Remove each selected record from the collection | ||
records.forEach(function (item) { | ||
self.remove(self.recordId(item), opts) | ||
records.forEach((item) => { | ||
this.remove(this.recordId(item), opts) | ||
}) | ||
return self.afterRemoveAll(query, opts, records) || records | ||
return this.afterRemoveAll(query, opts, records) || records | ||
}, | ||
@@ -753,5 +744,4 @@ | ||
updateIndexes (record) { | ||
const self = this | ||
self.index.updateRecord(record) | ||
utils.forOwn(self.indexes, function (index, name) { | ||
this.index.updateRecord(record) | ||
utils.forOwn(this.indexes, function (index, name) { | ||
index.updateRecord(record) | ||
@@ -758,0 +748,0 @@ }) |
@@ -12,3 +12,3 @@ import utils from './utils' | ||
const toProxy = [ | ||
export const proxiedMapperMethods = [ | ||
/** | ||
@@ -439,9 +439,19 @@ * Wrapper for {@link Mapper#count}. | ||
constructor: function Container (opts) { | ||
const self = this | ||
utils.classCallCheck(self, Container) | ||
Container.__super__.call(self) | ||
utils.classCallCheck(this, Container) | ||
Container.__super__.call(this) | ||
opts || (opts = {}) | ||
Object.defineProperties(this, { | ||
// Holds the adapters, shared by all mappers in this container | ||
_adapters: { | ||
value: {} | ||
}, | ||
// The the mappers in this container | ||
_mappers: { | ||
value: {} | ||
} | ||
}) | ||
// Apply options provided by the user | ||
utils.fillIn(self, opts) | ||
utils.fillIn(this, opts) | ||
@@ -457,3 +467,3 @@ /** | ||
*/ | ||
self.mapperDefaults = self.mapperDefaults || {} | ||
this.mapperDefaults = this.mapperDefaults || {} | ||
@@ -469,9 +479,3 @@ /** | ||
*/ | ||
self.mapperClass = self.mapperClass || Mapper | ||
// Holds the adapters, shared by all mappers in this container | ||
self._adapters = {} | ||
// The the mappers in this container | ||
self._mappers = {} | ||
this.mapperClass = this.mapperClass || Mapper | ||
}, | ||
@@ -508,2 +512,40 @@ | ||
/** | ||
* Return a container scoped to a particular mapper. | ||
* | ||
* @example | ||
* import {Container} from 'js-data' | ||
* const store = new Container() | ||
* const UserMapper = store.defineMapper('user') | ||
* const UserStore = store.as('user') | ||
* | ||
* const user1 = store.createRecord('user', { name: 'John' }) | ||
* const user2 = UserStore.createRecord({ name: 'John' }) | ||
* const user3 = UserMapper.createRecord({ name: 'John' }) | ||
* assert.deepEqual(user1, user2) | ||
* assert.deepEqual(user2, user3) | ||
* assert.deepEqual(user1, user3) | ||
* | ||
* @method Container#as | ||
* @param {string} name Name of the {@link Mapper}. | ||
* @returns {Object} A container scoped to a particular mapper. | ||
* @since 3.0.0 | ||
*/ | ||
as (name) { | ||
const props = {} | ||
proxiedMapperMethods.forEach(function (method) { | ||
props[method] = { | ||
writable: true, | ||
value: function (...args) { | ||
return this.getMapper(name)[method](...args) | ||
} | ||
} | ||
}) | ||
props.getMapper = { | ||
writable: true, | ||
value: () => this.getMapper(name) | ||
} | ||
return Object.create(this, props) | ||
}, | ||
/** | ||
* Create a new mapper and register it in this container. | ||
@@ -516,4 +558,8 @@ * | ||
* }) | ||
* const userMapper = store.defineMapper('user') | ||
* userMapper.foo // "bar" | ||
* // Container#defineMapper returns a direct reference to the newly created | ||
* // Mapper. | ||
* const UserMapper = store.defineMapper('user') | ||
* UserMapper === store.getMapper('user') // true | ||
* UserMapper === store.as('user').getMapper() // true | ||
* UserMapper.foo // "bar" | ||
* | ||
@@ -525,8 +571,7 @@ * @method Container#defineMapper | ||
* {@link Container#mapperClass} when creating the new {@link Mapper}. | ||
* @returns {Mapper} | ||
* @returns {Mapper} The newly created instance of {@link Mapper}. | ||
* @see Container#as | ||
* @since 3.0.0 | ||
*/ | ||
defineMapper (name, opts) { | ||
const self = this | ||
// For backwards compatibility with defineResource | ||
@@ -548,10 +593,10 @@ if (utils.isObject(name)) { | ||
// Check if the user is overriding the datastore's default mapperClass | ||
const mapperClass = opts.mapperClass || self.mapperClass | ||
const mapperClass = opts.mapperClass || this.mapperClass | ||
delete opts.mapperClass | ||
// Apply the datastore's defaults to the options going into the mapper | ||
utils.fillIn(opts, self.mapperDefaults) | ||
utils.fillIn(opts, this.mapperDefaults) | ||
// Instantiate a mapper | ||
const mapper = self._mappers[name] = new mapperClass(opts) // eslint-disable-line | ||
const mapper = this._mappers[name] = new mapperClass(opts) // eslint-disable-line | ||
mapper.relations || (mapper.relations = {}) | ||
@@ -561,22 +606,18 @@ // Make sure the mapper's name is set | ||
// All mappers in this datastore will share adapters | ||
mapper._adapters = self.getAdapters() | ||
mapper._adapters = this.getAdapters() | ||
mapper.datastore = self | ||
mapper.datastore = this | ||
mapper.on('all', function (...args) { | ||
self._onMapperEvent(name, ...args) | ||
}) | ||
mapper.on('all', (...args) => this._onMapperEvent(name, ...args)) | ||
// Setup the mapper's relations, including generating Mapper#relationList | ||
// and Mapper#relationFields | ||
utils.forOwn(mapper.relations, function (group, type) { | ||
utils.forOwn(group, function (relations, _name) { | ||
utils.forOwn(mapper.relations, (group, type) => { | ||
utils.forOwn(group, (relations, _name) => { | ||
if (utils.isObject(relations)) { | ||
relations = [relations] | ||
} | ||
relations.forEach(function (def) { | ||
def.getRelation = function () { | ||
return self.getMapper(_name) | ||
} | ||
const relatedMapper = self._mappers[_name] || _name | ||
relations.forEach((def) => { | ||
def.getRelation = () => this.getMapper(_name) | ||
const relatedMapper = this._mappers[_name] || _name | ||
if (type === belongsToType) { | ||
@@ -597,2 +638,3 @@ mapper.belongsTo(relatedMapper, def) | ||
defineResource (name, opts) { | ||
console.warn('DEPRECATED: defineResource is deprecated, use defineMapper instead') | ||
return this.defineMapper(name, opts) | ||
@@ -611,8 +653,7 @@ }, | ||
getAdapter (name) { | ||
const self = this | ||
const adapter = self.getAdapterName(name) | ||
const adapter = this.getAdapterName(name) | ||
if (!adapter) { | ||
throw utils.err(`${DOMAIN}#getAdapter`, 'name')(400, 'string', name) | ||
} | ||
return self.getAdapters()[adapter] | ||
return this.getAdapters()[adapter] | ||
}, | ||
@@ -654,4 +695,7 @@ | ||
* const container = new Container() | ||
* const userMapper = container.defineMapper('user') | ||
* userMapper === container.getMapper('user') // true | ||
* // Container#defineMapper returns a direct reference to the newly created | ||
* // Mapper. | ||
* const UserMapper = container.defineMapper('user') | ||
* UserMapper === container.getMapper('user') // true | ||
* UserMapper === container.as('user').getMapper() // true | ||
* | ||
@@ -691,9 +735,8 @@ * @method Container#getMapper | ||
registerAdapter (name, adapter, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
self.getAdapters()[name] = adapter | ||
this.getAdapters()[name] = adapter | ||
// Optionally make it the default adapter for the target. | ||
if (opts === true || opts.default) { | ||
self.mapperDefaults.defaultAdapter = name | ||
utils.forOwn(self._mappers, function (mapper) { | ||
this.mapperDefaults.defaultAdapter = name | ||
utils.forOwn(this._mappers, function (mapper) { | ||
mapper.defaultAdapter = name | ||
@@ -705,3 +748,3 @@ }) | ||
toProxy.forEach(function (method) { | ||
proxiedMapperMethods.forEach(function (method) { | ||
props[method] = function (name, ...args) { | ||
@@ -785,3 +828,3 @@ return this.getMapper(name)[method](...args) | ||
*/ | ||
export default Component.extend(props) | ||
export const Container = Component.extend(props) | ||
@@ -788,0 +831,0 @@ /** |
@@ -7,6 +7,24 @@ import utils from './utils' | ||
} from './decorators' | ||
import Container from './Container' | ||
import {proxiedMapperMethods, Container} from './Container' | ||
import LinkedCollection from './LinkedCollection' | ||
const DOMAIN = 'DataStore' | ||
const proxiedCollectionMethods = [ | ||
'add', | ||
'between', | ||
'createIndex', | ||
'filter', | ||
'get', | ||
'getAll', | ||
'query', | ||
'toJson' | ||
] | ||
const ownMethodsForScoping = [ | ||
'addToCache', | ||
'cachedFind', | ||
'cachedFindAll', | ||
'cacheFind', | ||
'cacheFindAll', | ||
'hashQuery' | ||
] | ||
@@ -22,4 +40,3 @@ const safeSet = function (record, field, value) { | ||
const cachedFn = function (name, hashOrId, opts) { | ||
const self = this | ||
const cached = self._completedQueries[name][hashOrId] | ||
const cached = this._completedQueries[name][hashOrId] | ||
if (utils.isFunction(cached)) { | ||
@@ -33,11 +50,10 @@ return cached(name, hashOrId, opts) | ||
constructor: function DataStore (opts) { | ||
const self = this | ||
utils.classCallCheck(self, DataStore) | ||
DataStore.__super__.call(self, opts) | ||
utils.classCallCheck(this, DataStore) | ||
DataStore.__super__.call(this, opts) | ||
self.collectionClass = self.collectionClass || LinkedCollection | ||
self._collections = {} | ||
self._pendingQueries = {} | ||
self._completedQueries = {} | ||
return self | ||
this.collectionClass = this.collectionClass || LinkedCollection | ||
this._collections = {} | ||
this._pendingQueries = {} | ||
this._completedQueries = {} | ||
return this | ||
}, | ||
@@ -116,2 +132,60 @@ | ||
/** | ||
* Return a store scoped to a particular mapper/collection pair. | ||
* | ||
* @example | ||
* import {DataStore} from 'js-data' | ||
* const store = new DataStore() | ||
* const UserMapper = store.defineMapper('user') | ||
* const UserStore = store.as('user') | ||
* | ||
* const user1 = store.createRecord('user', { name: 'John' }) | ||
* const user2 = UserStore.createRecord({ name: 'John' }) | ||
* const user3 = UserMapper.createRecord({ name: 'John' }) | ||
* assert.deepEqual(user1, user2) | ||
* assert.deepEqual(user2, user3) | ||
* assert.deepEqual(user1, user3) | ||
* | ||
* @method DataStore#as | ||
* @param {string} name Name of the {@link Mapper}. | ||
* @returns {Object} A store scoped to a particular mapper/collection pair. | ||
* @since 3.0.0 | ||
*/ | ||
as (name) { | ||
const props = {} | ||
ownMethodsForScoping.forEach(function (method) { | ||
props[method] = { | ||
writable: true, | ||
value: function (...args) { | ||
return this[method](name, ...args) | ||
} | ||
} | ||
}) | ||
proxiedMapperMethods.forEach(function (method) { | ||
props[method] = { | ||
writable: true, | ||
value: function (...args) { | ||
return this.getMapper(name)[method](...args) | ||
} | ||
} | ||
}) | ||
props.getMapper = { | ||
writable: true, | ||
value: () => this.getMapper(name) | ||
} | ||
proxiedCollectionMethods.forEach(function (method) { | ||
props[method] = { | ||
writable: true, | ||
value: function (...args) { | ||
return this.getCollection(name)[method](...args) | ||
} | ||
} | ||
}) | ||
props.getCollection = { | ||
writable: true, | ||
value: () => this.getCollection(name) | ||
} | ||
return Object.create(this, props) | ||
}, | ||
/** | ||
* Retrieve a cached `find` result, if any. | ||
@@ -157,6 +231,3 @@ * | ||
cacheFind (name, data, id, opts) { | ||
const self = this | ||
self._completedQueries[name][id] = function (name, id, opts) { | ||
return self.get(name, id) | ||
} | ||
this._completedQueries[name][id] = (name, id, opts) => this.get(name, id) | ||
}, | ||
@@ -183,8 +254,13 @@ | ||
cacheFindAll (name, data, hash, opts) { | ||
const self = this | ||
self._completedQueries[name][hash] = function (name, hash, opts) { | ||
return self.filter(name, utils.fromJson(hash)) | ||
} | ||
this._completedQueries[name][hash] = (name, hash, opts) => this.filter(name, utils.fromJson(hash)) | ||
}, | ||
clear () { | ||
const removed = {} | ||
utils.forOwn(this._collections, (collection, name) => { | ||
removed[name] = collection.removeAll() | ||
}) | ||
return removed | ||
}, | ||
/** | ||
@@ -202,7 +278,5 @@ * TODO | ||
create (name, record, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('create', name, record, opts).then(function (data) { | ||
return self._end(name, data, opts) | ||
}) | ||
return this._callSuper('create', name, record, opts) | ||
.then((result) => this._end(name, result, opts)) | ||
}, | ||
@@ -222,10 +296,9 @@ | ||
createMany (name, records, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('createMany', name, records, opts).then(function (data) { | ||
return self._end(name, data, opts) | ||
}) | ||
return this._callSuper('createMany', name, records, opts) | ||
.then((result) => this._end(name, result, opts)) | ||
}, | ||
defineMapper (name, opts) { | ||
// Complexity of this method is beyond simply using => functions to bind context | ||
const self = this | ||
@@ -552,13 +625,12 @@ const mapper = utils.getSuper(self).prototype.defineMapper.call(self, name, opts) | ||
destroy (name, id, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('destroy', name, id, opts).then(function (data) { | ||
return this._callSuper('destroy', name, id, opts).then((result) => { | ||
if (opts.raw) { | ||
data.data = self.getCollection(name).remove(id, opts) | ||
result.data = this.getCollection(name).remove(id, opts) | ||
} else { | ||
data = self.getCollection(name).remove(id, opts) | ||
result = this.getCollection(name).remove(id, opts) | ||
} | ||
delete self._pendingQueries[name][id] | ||
delete self._completedQueries[name][id] | ||
return data | ||
delete this._pendingQueries[name][id] | ||
delete this._completedQueries[name][id] | ||
return result | ||
}) | ||
@@ -579,23 +651,24 @@ }, | ||
destroyAll (name, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('destroyAll', name, query, opts).then(function (data) { | ||
return this._callSuper('destroyAll', name, query, opts).then((result) => { | ||
if (opts.raw) { | ||
data.data = self.getCollection(name).removeAll(query, opts) | ||
result.data = this.getCollection(name).removeAll(query, opts) | ||
} else { | ||
data = self.getCollection(name).removeAll(query, opts) | ||
result = this.getCollection(name).removeAll(query, opts) | ||
} | ||
const hash = self.hashQuery(name, query, opts) | ||
delete self._pendingQueries[name][hash] | ||
delete self._completedQueries[name][hash] | ||
return data | ||
const hash = this.hashQuery(name, query, opts) | ||
delete this._pendingQueries[name][hash] | ||
delete this._completedQueries[name][hash] | ||
return result | ||
}) | ||
}, | ||
eject (id, opts) { | ||
return this.remove(id, opts) | ||
eject (name, id, opts) { | ||
console.warn('DEPRECATED: "eject" is deprecated, use "remove" instead') | ||
return this.remove(name, id, opts) | ||
}, | ||
ejectAll (query, opts) { | ||
return this.removeAll(query, opts) | ||
ejectAll (name, query, opts) { | ||
console.warn('DEPRECATED: "ejectAll" is deprecated, use "removeAll" instead') | ||
return this.removeAll(name, query, opts) | ||
}, | ||
@@ -614,7 +687,6 @@ | ||
find (name, id, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
const pendingQuery = self._pendingQueries[name][id] | ||
const pendingQuery = this._pendingQueries[name][id] | ||
utils.fillIn(opts, self.getMapper(name)) | ||
utils.fillIn(opts, this.getMapper(name)) | ||
@@ -624,13 +696,13 @@ if (pendingQuery) { | ||
} | ||
const item = self.cachedFind(name, id, opts) | ||
const item = this.cachedFind(name, id, opts) | ||
let promise | ||
if (opts.force || !item) { | ||
promise = self._pendingQueries[name][id] = self._callSuper('find', name, id, opts).then(function (data) { | ||
delete self._pendingQueries[name][id] | ||
const result = self._end(name, data, opts) | ||
self.cacheFind(name, result, id, opts) | ||
promise = this._pendingQueries[name][id] = this._callSuper('find', name, id, opts).then((result) => { | ||
delete this._pendingQueries[name][id] | ||
result = this._end(name, result, opts) | ||
this.cacheFind(name, result, id, opts) | ||
return result | ||
}, function (err) { | ||
delete self._pendingQueries[name][id] | ||
}, (err) => { | ||
delete this._pendingQueries[name][id] | ||
return utils.reject(err) | ||
@@ -655,8 +727,7 @@ }) | ||
findAll (name, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
const hash = self.hashQuery(name, query, opts) | ||
const pendingQuery = self._pendingQueries[name][hash] | ||
const hash = this.hashQuery(name, query, opts) | ||
const pendingQuery = this._pendingQueries[name][hash] | ||
utils.fillIn(opts, self.getMapper(name)) | ||
utils.fillIn(opts, this.getMapper(name)) | ||
@@ -667,13 +738,13 @@ if (pendingQuery) { | ||
const items = self.cachedFindAll(name, hash, opts) | ||
const items = this.cachedFindAll(name, hash, opts) | ||
let promise | ||
if (opts.force || !items) { | ||
promise = self._pendingQueries[name][hash] = self._callSuper('findAll', name, query, opts).then(function (data) { | ||
delete self._pendingQueries[name][hash] | ||
const result = self._end(name, data, opts) | ||
self.cacheFindAll(name, result, hash, opts) | ||
promise = this._pendingQueries[name][hash] = this._callSuper('findAll', name, query, opts).then((result) => { | ||
delete this._pendingQueries[name][hash] | ||
result = this._end(name, result, opts) | ||
this.cacheFindAll(name, result, hash, opts) | ||
return result | ||
}, function (err) { | ||
delete self._pendingQueries[name][hash] | ||
}, (err) => { | ||
delete this._pendingQueries[name][hash] | ||
return utils.reject(err) | ||
@@ -707,11 +778,11 @@ }) | ||
inject (records, opts) { | ||
return this.add(records, opts) | ||
inject (name, records, opts) { | ||
console.warn('DEPRECATED: "inject" is deprecated, use "add" instead') | ||
return this.add(name, records, opts) | ||
}, | ||
remove (name, id, opts) { | ||
const self = this | ||
const record = self.getCollection(name).remove(id, opts) | ||
const record = this.getCollection(name).remove(id, opts) | ||
if (record) { | ||
self.removeRelated(name, [record], opts) | ||
this.removeRelated(name, [record], opts) | ||
} | ||
@@ -722,6 +793,5 @@ return record | ||
removeAll (name, query, opts) { | ||
const self = this | ||
const records = self.getCollection(name).removeAll(query, opts) | ||
const records = this.getCollection(name).removeAll(query, opts) | ||
if (records.length) { | ||
self.removeRelated(name, records, opts) | ||
this.removeRelated(name, records, opts) | ||
} | ||
@@ -732,5 +802,4 @@ return records | ||
removeRelated (name, records, opts) { | ||
const self = this | ||
utils.forEachRelation(self.getMapper(name), opts, function (def, optsCopy) { | ||
records.forEach(function (record) { | ||
utils.forEachRelation(this.getMapper(name), opts, (def, optsCopy) => { | ||
records.forEach((record) => { | ||
let relatedData | ||
@@ -757,6 +826,6 @@ let query | ||
} else if (def.type === belongsToType) { | ||
relatedData = self.remove(def.relation, def.getForeignKey(record), optsCopy) | ||
relatedData = this.remove(def.relation, def.getForeignKey(record), optsCopy) | ||
} | ||
if (query) { | ||
relatedData = self.removeAll(def.relation, query, optsCopy) | ||
relatedData = this.removeAll(def.relation, query, optsCopy) | ||
} | ||
@@ -789,7 +858,5 @@ if (relatedData) { | ||
update (name, id, record, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('update', name, id, record, opts).then(function (data) { | ||
return self._end(name, data, opts) | ||
}) | ||
return this._callSuper('update', name, id, record, opts) | ||
.then((result) => this._end(name, result, opts)) | ||
}, | ||
@@ -810,7 +877,5 @@ | ||
updateAll (name, props, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('updateAll', name, query, props, opts).then(function (data) { | ||
return self._end(name, data, opts) | ||
}) | ||
return this._callSuper('updateAll', name, query, props, opts) | ||
.then((result) => this._end(name, result, opts)) | ||
}, | ||
@@ -830,22 +895,9 @@ | ||
updateMany (name, records, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self._callSuper('updateMany', name, records, opts).then(function (data) { | ||
return self._end(name, data, opts) | ||
}) | ||
return this._callSuper('updateMany', name, records, opts) | ||
.then((result) => this._end(name, result, opts)) | ||
} | ||
} | ||
const toProxy = [ | ||
'add', | ||
'between', | ||
'createIndex', | ||
'filter', | ||
'get', | ||
'getAll', | ||
'query', | ||
'toJson' | ||
] | ||
toProxy.forEach(function (method) { | ||
proxiedCollectionMethods.forEach(function (method) { | ||
props[method] = function (name, ...args) { | ||
@@ -875,4 +927,10 @@ return this.getCollection(name)[method](...args) | ||
* const store = new DataStore() | ||
* | ||
* // DataStore#defineMapper returns a direct reference to the newly created | ||
* // Mapper. | ||
* const UserMapper = store.defineMapper('user') | ||
* | ||
* // DataStore#as returns the store scoped to a particular Mapper. | ||
* const UserStore = store.as('user') | ||
* | ||
* // Call "find" on "UserMapper" (Stateless ORM) | ||
@@ -883,3 +941,3 @@ * UserMapper.find(1).then((user) => { | ||
* // Call "find" on "store" targeting "user" (Stateful DataStore) | ||
* return store.find('user', 1) | ||
* return store.find('user', 1) // same as "UserStore.find(1)" | ||
* }).then((user) => { | ||
@@ -886,0 +944,0 @@ * // not only was a "user" record retrieved, but it was added to the |
@@ -10,3 +10,2 @@ import utils from './utils' | ||
function Relation (related, opts) { | ||
const self = this | ||
const DOMAIN_ERR = `new ${DOMAIN}` | ||
@@ -38,3 +37,3 @@ | ||
opts.relation = related.name | ||
Object.defineProperty(self, 'relatedMapper', { | ||
Object.defineProperty(this, 'relatedMapper', { | ||
value: related | ||
@@ -46,3 +45,3 @@ }) | ||
Object.defineProperty(self, 'inverse', { | ||
Object.defineProperty(this, 'inverse', { | ||
value: undefined, | ||
@@ -52,3 +51,3 @@ writable: true | ||
utils.fillIn(self, opts) | ||
utils.fillIn(this, opts) | ||
} | ||
@@ -67,16 +66,15 @@ | ||
setForeignKey (record, relatedRecord) { | ||
const self = this | ||
if (!record || !relatedRecord) { | ||
return | ||
} | ||
if (self.type === belongsToType) { | ||
utils.set(record, self.foreignKey, utils.get(relatedRecord, self.getRelation().idAttribute)) | ||
if (this.type === belongsToType) { | ||
utils.set(record, this.foreignKey, utils.get(relatedRecord, this.getRelation().idAttribute)) | ||
} else { | ||
const idAttribute = self.mapper.idAttribute | ||
const idAttribute = this.mapper.idAttribute | ||
if (utils.isArray(relatedRecord)) { | ||
relatedRecord.forEach(function (relatedRecordItem) { | ||
utils.set(relatedRecordItem, self.foreignKey, utils.get(record, idAttribute)) | ||
relatedRecord.forEach((relatedRecordItem) => { | ||
utils.set(relatedRecordItem, this.foreignKey, utils.get(record, idAttribute)) | ||
}) | ||
} else { | ||
utils.set(relatedRecord, self.foreignKey, utils.get(record, idAttribute)) | ||
utils.set(relatedRecord, this.foreignKey, utils.get(record, idAttribute)) | ||
} | ||
@@ -92,16 +90,15 @@ } | ||
getInverse (mapper) { | ||
const self = this | ||
if (self.inverse) { | ||
return self.inverse | ||
if (this.inverse) { | ||
return this.inverse | ||
} | ||
self.getRelation().relationList.forEach(function (def) { | ||
this.getRelation().relationList.forEach((def) => { | ||
if (def.getRelation() === mapper) { | ||
if (def.foreignKey && def.foreignKey !== self.foreignKey) { | ||
if (def.foreignKey && def.foreignKey !== this.foreignKey) { | ||
return | ||
} | ||
self.inverse = def | ||
this.inverse = def | ||
return false | ||
} | ||
}) | ||
return self.inverse | ||
return this.inverse | ||
} | ||
@@ -108,0 +105,0 @@ }) |
@@ -113,3 +113,3 @@ /** | ||
*/ | ||
import Container from './Container' | ||
import {Container} from './Container' | ||
@@ -161,7 +161,7 @@ /** | ||
* const store = new Container() | ||
* const UserService = store.defineMapper('user') | ||
* store.defineMapper('user') | ||
* | ||
* @example <caption>Create Mapper manually</caption> | ||
* import {Mapper} from 'js-data' | ||
* const UserService = new Mapper({ name: 'user' }) | ||
* const UserMapper = new Mapper({ name: 'user' }) | ||
* | ||
@@ -194,4 +194,4 @@ * @name module:js-data.Mapper | ||
* const store = new Container() | ||
* const UserService = store.defineMapper('user') | ||
* const user = UserService.createRecord() | ||
* store.defineMapper('user') | ||
* const user = store.createRecord('user') | ||
* | ||
@@ -218,3 +218,3 @@ * @name module:js-data.Record | ||
* const store = new Container() | ||
* const UserService = store.defineMapper('user', { | ||
* store.defineMapper('user', { | ||
* schema: userSchema | ||
@@ -221,0 +221,0 @@ * }) |
@@ -27,21 +27,25 @@ import utils from './utils' | ||
constructor: function LinkedCollection (records, opts) { | ||
const self = this | ||
utils.classCallCheck(self, LinkedCollection) | ||
LinkedCollection.__super__.call(self, records, opts) | ||
utils.classCallCheck(this, LinkedCollection) | ||
// Make sure this collection has somewhere to store "added" timestamps | ||
Object.defineProperty(self, '_added', { | ||
value: {} | ||
Object.defineProperties(this, { | ||
_added: { | ||
value: {} | ||
}, | ||
datastore: { | ||
writable: true, | ||
value: undefined | ||
} | ||
}) | ||
LinkedCollection.__super__.call(this, records, opts) | ||
// Make sure this collection has a reference to a datastore | ||
if (!self.datastore) { | ||
throw utils.err(`new ${DOMAIN}`, 'opts.datastore')(400, 'DataStore', self.datastore) | ||
if (!this.datastore) { | ||
throw utils.err(`new ${DOMAIN}`, 'opts.datastore')(400, 'DataStore', this.datastore) | ||
} | ||
return self | ||
return this | ||
}, | ||
_onRecordEvent (...args) { | ||
const self = this | ||
utils.getSuper(self).prototype._onRecordEvent.apply(self, args) | ||
utils.getSuper(this).prototype._onRecordEvent.apply(this, args) | ||
const event = args[0] | ||
@@ -51,3 +55,3 @@ // This is a very brute force method | ||
if (utils.isString(event) && event.indexOf('change') === 0) { | ||
self.updateIndexes(args[1]) | ||
this.updateIndexes(args[1]) | ||
} | ||
@@ -57,5 +61,4 @@ }, | ||
add (records, opts) { | ||
const self = this | ||
const datastore = self.datastore | ||
const mapper = self.mapper | ||
const datastore = this.datastore | ||
const mapper = this.mapper | ||
const relationList = mapper.relationList | ||
@@ -72,3 +75,3 @@ const timestamp = new Date().getTime() | ||
records = utils.getSuper(self).prototype.add.call(self, records, opts) | ||
records = utils.getSuper(this).prototype.add.call(this, records, opts) | ||
@@ -182,5 +185,5 @@ if (relationList.length && records.length) { | ||
records.forEach(function (record) { | ||
records.forEach((record) => { | ||
// Track when this record was added | ||
self._added[self.recordId(record)] = timestamp | ||
this._added[this.recordId(record)] = timestamp | ||
@@ -196,7 +199,6 @@ if (usesRecordClass) { | ||
remove (id, opts) { | ||
const self = this | ||
const mapper = self.mapper | ||
const record = utils.getSuper(self).prototype.remove.call(self, id, opts) | ||
const mapper = this.mapper | ||
const record = utils.getSuper(this).prototype.remove.call(this, id, opts) | ||
if (record) { | ||
delete self._added[id] | ||
delete this._added[id] | ||
if (mapper.recordClass) { | ||
@@ -210,7 +212,6 @@ record._set('$') // unset | ||
removeAll (query, opts) { | ||
const self = this | ||
const mapper = self.mapper | ||
const records = utils.getSuper(self).prototype.removeAll.call(self, query, opts) | ||
records.forEach(function (record) { | ||
delete self._added[self.recordId(record)] | ||
const mapper = this.mapper | ||
const records = utils.getSuper(this).prototype.removeAll.call(this, query, opts) | ||
records.forEach((record) => { | ||
delete this._added[this.recordId(record)] | ||
if (mapper.recordClass) { | ||
@@ -217,0 +218,0 @@ record._set('$') // unset |
195
src/Query.js
@@ -45,4 +45,3 @@ import utils from './utils' | ||
constructor: function Query (collection) { | ||
const self = this | ||
utils.classCallCheck(self, Query) | ||
utils.classCallCheck(this, Query) | ||
@@ -56,3 +55,3 @@ /** | ||
*/ | ||
self.collection = collection | ||
this.collection = collection | ||
@@ -66,5 +65,86 @@ /** | ||
*/ | ||
self.data = null | ||
this.data = null | ||
}, | ||
_applyWhereFromObject (where) { | ||
const fields = [] | ||
const ops = [] | ||
const predicates = [] | ||
utils.forOwn(where, (clause, field) => { | ||
if (!utils.isObject(clause)) { | ||
clause = { | ||
'==': clause | ||
} | ||
} | ||
utils.forOwn(clause, (expr, op) => { | ||
fields.push(field) | ||
ops.push(op) | ||
predicates.push(expr) | ||
}) | ||
}) | ||
return { | ||
fields, | ||
ops, | ||
predicates | ||
} | ||
}, | ||
_applyWhereFromArray (where) { | ||
const groups = [] | ||
where.forEach((_where, i) => { | ||
if (utils.isString(_where)) { | ||
return | ||
} | ||
const prev = where[i - 1] | ||
const parser = utils.isArray(_where) ? this._applyWhereFromArray : this._applyWhereFromObject | ||
const group = parser.call(this, _where) | ||
if (prev === 'or') { | ||
group.isOr = true | ||
} | ||
groups.push(group) | ||
}) | ||
groups.isArray = true | ||
return groups | ||
}, | ||
_testObjectGroup (keep, first, group, item) { | ||
let i | ||
const fields = group.fields | ||
const ops = group.ops | ||
const predicates = group.predicates | ||
const len = ops.length | ||
for (i = 0; i < len; i++) { | ||
let op = ops[i] | ||
const isOr = op.charAt(0) === '|' | ||
op = isOr ? op.substr(1) : op | ||
const expr = this.evaluate(utils.get(item, fields[i]), op, predicates[i]) | ||
if (expr !== undefined) { | ||
keep = first ? expr : (isOr ? keep || expr : keep && expr) | ||
} | ||
first = false | ||
} | ||
return { keep, first } | ||
}, | ||
_testArrayGroup (keep, first, groups, item) { | ||
let i | ||
const len = groups.length | ||
for (i = 0; i < len; i++) { | ||
const group = groups[i] | ||
const parser = group.isArray ? this._testArrayGroup : this._testObjectGroup | ||
const result = parser.call(this, true, true, group, item) | ||
if (groups[i - 1]) { | ||
if (group.isOr) { | ||
keep = keep || result.keep | ||
} else { | ||
keep = keep && result.keep | ||
} | ||
} else { | ||
keep = result.keep | ||
} | ||
first = result.first | ||
} | ||
return { keep, first } | ||
}, | ||
/** | ||
@@ -95,9 +175,8 @@ * Find all entities between two boundaries. | ||
between (leftKeys, rightKeys, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
if (self.data) { | ||
if (this.data) { | ||
throw utils.err(`${DOMAIN}#between`)(500, 'Cannot access index') | ||
} | ||
self.data = self.collection.getIndex(opts.index).between(leftKeys, rightKeys, opts) | ||
return self | ||
this.data = this.collection.getIndex(opts.index).between(leftKeys, rightKeys, opts) | ||
return this | ||
}, | ||
@@ -203,4 +282,2 @@ | ||
filter (query, thisArg) { | ||
const self = this | ||
/** | ||
@@ -263,3 +340,3 @@ * Selection query as defined by JSData's [Query Syntax][querysyntax]. | ||
query || (query = {}) | ||
self.getData() | ||
this.getData() | ||
if (utils.isObject(query)) { | ||
@@ -280,3 +357,3 @@ let where = {} | ||
*/ | ||
if (utils.isObject(query.where)) { | ||
if (utils.isObject(query.where) || utils.isArray(query.where)) { | ||
where = query.where | ||
@@ -291,37 +368,13 @@ } | ||
}) | ||
let groups | ||
const fields = [] | ||
const ops = [] | ||
const predicates = [] | ||
utils.forOwn(where, function (clause, field) { | ||
if (!utils.isObject(clause)) { | ||
clause = { | ||
'==': clause | ||
} | ||
} | ||
utils.forOwn(clause, function (expr, op) { | ||
fields.push(field) | ||
ops.push(op) | ||
predicates.push(expr) | ||
}) | ||
}) | ||
if (fields.length) { | ||
let i | ||
let len = fields.length | ||
self.data = self.data.filter(function (item) { | ||
let first = true | ||
let keep = true | ||
// Apply filter for each field | ||
if (utils.isObject(where) && Object.keys(where).length !== 0) { | ||
groups = this._applyWhereFromArray([where]) | ||
} else if (utils.isArray(where)) { | ||
groups = this._applyWhereFromArray(where) | ||
} | ||
for (i = 0; i < len; i++) { | ||
let op = ops[i] | ||
const isOr = op.charAt(0) === '|' | ||
op = isOr ? op.substr(1) : op | ||
const expr = self.evaluate(utils.get(item, fields[i]), op, predicates[i]) | ||
if (expr !== undefined) { | ||
keep = first ? expr : (isOr ? keep || expr : keep && expr) | ||
} | ||
first = false | ||
} | ||
return keep | ||
}) | ||
if (groups) { | ||
this.data = this.data.filter((item, i) => this._testArrayGroup(true, true, groups, item).keep) | ||
} | ||
@@ -359,5 +412,3 @@ | ||
}) | ||
self.data.sort(function (a, b) { | ||
return self.compare(orderBy, index, a, b) | ||
}) | ||
this.data.sort((a, b) => this.compare(orderBy, index, a, b)) | ||
} | ||
@@ -382,5 +433,5 @@ | ||
if (utils.isNumber(query.skip)) { | ||
self.skip(query.skip) | ||
this.skip(query.skip) | ||
} else if (utils.isNumber(query.offset)) { | ||
self.skip(query.offset) | ||
this.skip(query.offset) | ||
} | ||
@@ -405,8 +456,8 @@ | ||
if (utils.isNumber(query.limit)) { | ||
self.limit(query.limit) | ||
this.limit(query.limit) | ||
} | ||
} else if (utils.isFunction(query)) { | ||
self.data = self.data.filter(query, thisArg) | ||
this.data = this.data.filter(query, thisArg) | ||
} | ||
return self | ||
return this | ||
}, | ||
@@ -458,6 +509,5 @@ | ||
get (keyList, opts) { | ||
const self = this | ||
keyList || (keyList = []) | ||
opts || (opts = {}) | ||
if (self.data) { | ||
if (this.data) { | ||
throw utils.err(`${DOMAIN}#get`)(500, INDEX_ERR) | ||
@@ -469,7 +519,7 @@ } | ||
if (!keyList.length) { | ||
self.getData() | ||
return self | ||
this.getData() | ||
return this | ||
} | ||
self.data = self.collection.getIndex(opts.index).get(keyList) | ||
return self | ||
this.data = this.collection.getIndex(opts.index).get(keyList) | ||
return this | ||
}, | ||
@@ -497,10 +547,9 @@ | ||
getAll (...args) { | ||
const self = this | ||
let opts = {} | ||
if (self.data) { | ||
if (this.data) { | ||
throw utils.err(`${DOMAIN}#getAll`)(500, INDEX_ERR) | ||
} | ||
if (!args.length || args.length === 1 && utils.isObject(args[0])) { | ||
self.getData() | ||
return self | ||
this.getData() | ||
return this | ||
} else if (args.length && utils.isObject(args[args.length - 1])) { | ||
@@ -510,9 +559,9 @@ opts = args[args.length - 1] | ||
} | ||
const collection = self.collection | ||
const collection = this.collection | ||
const index = collection.getIndex(opts.index) | ||
self.data = [] | ||
args.forEach(function (keyList) { | ||
self.data = self.data.concat(index.get(keyList)) | ||
this.data = [] | ||
args.forEach((keyList) => { | ||
this.data = this.data.concat(index.get(keyList)) | ||
}) | ||
return self | ||
return this | ||
}, | ||
@@ -528,7 +577,6 @@ | ||
getData () { | ||
const self = this | ||
if (!self.data) { | ||
self.data = self.collection.index.getAll() | ||
if (!this.data) { | ||
this.data = this.collection.index.getAll() | ||
} | ||
return self.data | ||
return this.data | ||
}, | ||
@@ -715,2 +763,5 @@ | ||
ops: { | ||
'=': function (value, predicate) { | ||
return value == predicate // eslint-disable-line | ||
}, | ||
'==': function (value, predicate) { | ||
@@ -717,0 +768,0 @@ return value == predicate // eslint-disable-line |
@@ -32,4 +32,3 @@ import utils from './utils' | ||
constructor: function Record (props, opts) { | ||
const self = this | ||
utils.classCallCheck(self, Record) | ||
utils.classCallCheck(this, Record) | ||
@@ -39,3 +38,3 @@ props || (props = {}) | ||
const _props = {} | ||
Object.defineProperties(self, { | ||
Object.defineProperties(this, { | ||
_get: { value (key) { return utils.get(_props, key) } }, | ||
@@ -45,3 +44,3 @@ _set: { value (key, value) { return utils.set(_props, key, value) } }, | ||
}) | ||
const _set = self._set | ||
const _set = this._set | ||
// TODO: Optimize these strings | ||
@@ -52,3 +51,3 @@ _set('creating', true) | ||
} | ||
utils.fillIn(self, props) | ||
utils.fillIn(this, props) | ||
_set('creating', false) | ||
@@ -67,4 +66,3 @@ _set('noValidate', false) | ||
_mapper () { | ||
const self = this | ||
const mapper = self.constructor.mapper | ||
const mapper = this.constructor.mapper | ||
if (!mapper) { | ||
@@ -107,5 +105,4 @@ throw utils.err(`${DOMAIN}#_mapper`, '')(404, 'mapper') | ||
changes (opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return utils.diffObjects(typeof self.toJSON === 'function' ? self.toJSON(opts) : self, self._get('previous'), opts) | ||
return utils.diffObjects(typeof this.toJSON === 'function' ? this.toJSON(opts) : this, this._get('previous'), opts) | ||
}, | ||
@@ -120,6 +117,5 @@ | ||
commit () { | ||
const self = this | ||
self._set('changed') // unset | ||
self._set('previous', utils.copy(self)) | ||
return self | ||
this._set('changed') // unset | ||
this._set('previous', utils.copy(this)) | ||
return this | ||
}, | ||
@@ -136,6 +132,5 @@ | ||
destroy (opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
const mapper = self._mapper() | ||
return superMethod(mapper, 'destroy')(utils.get(self, mapper.idAttribute), opts) | ||
const mapper = this._mapper() | ||
return superMethod(mapper, 'destroy')(utils.get(this, mapper.idAttribute), opts) | ||
}, | ||
@@ -166,5 +161,4 @@ | ||
hasChanges (opts) { | ||
const self = this | ||
const quickHasChanges = !!(self._get('changed') || []).length | ||
return quickHasChanges || utils.areDifferent(typeof self.toJSON === 'function' ? self.toJSON(opts) : self, self._get('previous'), opts) | ||
const quickHasChanges = !!(this._get('changed') || []).length | ||
return quickHasChanges || utils.areDifferent(typeof this.toJSON === 'function' ? this.toJSON(opts) : this, this._get('previous'), opts) | ||
}, | ||
@@ -179,9 +173,7 @@ | ||
hashCode () { | ||
const self = this | ||
return utils.get(self, self._mapper().idAttribute) | ||
return utils.get(this, this._mapper().idAttribute) | ||
}, | ||
isValid (opts) { | ||
const self = this | ||
return !self._mapper().validate(self, opts) | ||
return !this._mapper().validate(this, opts) | ||
}, | ||
@@ -199,4 +191,3 @@ | ||
let op | ||
const self = this | ||
const mapper = self._mapper() | ||
const mapper = this._mapper() | ||
@@ -217,17 +208,17 @@ // Default values for arguments | ||
op = opts.op = 'beforeLoadRelations' | ||
return utils.resolve(self[op](relations, opts)).then(function () { | ||
return utils.resolve(this[op](relations, opts)).then(() => { | ||
// Now delegate to the adapter | ||
op = opts.op = 'loadRelations' | ||
mapper.dbg(op, self, relations, opts) | ||
mapper.dbg(op, this, relations, opts) | ||
let tasks = [] | ||
let task | ||
utils.forEachRelation(mapper, opts, function (def, optsCopy) { | ||
utils.forEachRelation(mapper, opts, (def, optsCopy) => { | ||
const relatedMapper = def.getRelation() | ||
optsCopy.raw = false | ||
if (utils.isFunction(def.load)) { | ||
task = def.load(mapper, def, self, opts) | ||
task = def.load(mapper, def, this, opts) | ||
} else if (def.type === 'hasMany' || def.type === 'hasOne') { | ||
if (def.foreignKey) { | ||
task = superMethod(relatedMapper, 'findAll')({ | ||
[def.foreignKey]: utils.get(self, mapper.idAttribute) | ||
[def.foreignKey]: utils.get(this, mapper.idAttribute) | ||
}, optsCopy).then(function (relatedData) { | ||
@@ -243,3 +234,3 @@ if (def.type === 'hasOne') { | ||
[relatedMapper.idAttribute]: { | ||
'in': utils.get(self, def.localKeys) | ||
'in': utils.get(this, def.localKeys) | ||
} | ||
@@ -252,3 +243,3 @@ } | ||
[def.foreignKeys]: { | ||
'contains': utils.get(self, mapper.idAttribute) | ||
'contains': utils.get(this, mapper.idAttribute) | ||
} | ||
@@ -259,3 +250,3 @@ } | ||
} else if (def.type === 'belongsTo') { | ||
const key = utils.get(self, def.foreignKey) | ||
const key = utils.get(this, def.foreignKey) | ||
if (utils.isSorN(key)) { | ||
@@ -266,4 +257,4 @@ task = superMethod(relatedMapper, 'find')(key, optsCopy) | ||
if (task) { | ||
task = task.then(function (relatedData) { | ||
def.setLocalField(self, relatedData) | ||
task = task.then((relatedData) => { | ||
def.setLocalField(this, relatedData) | ||
}) | ||
@@ -274,8 +265,6 @@ tasks.push(task) | ||
return Promise.all(tasks) | ||
}).then(function () { | ||
}).then(() => { | ||
// afterLoadRelations lifecycle hook | ||
op = opts.op = 'afterLoadRelations' | ||
return utils.resolve(self[op](relations, opts)).then(function () { | ||
return self | ||
}) | ||
return utils.resolve(this[op](relations, opts)).then(() => this) | ||
}) | ||
@@ -292,7 +281,6 @@ }, | ||
previous (key) { | ||
const self = this | ||
if (key) { | ||
return self._get(`previous.${key}`) | ||
return this._get(`previous.${key}`) | ||
} | ||
return self._get('previous') | ||
return this._get('previous') | ||
}, | ||
@@ -308,9 +296,8 @@ | ||
revert (opts) { | ||
const self = this | ||
const previous = self._get('previous') | ||
const previous = this._get('previous') | ||
opts || (opts = {}) | ||
opts.preserve || (opts.preserve = []) | ||
utils.forOwn(self, (value, key) => { | ||
if (key !== self._mapper().idAttribute && !previous.hasOwnProperty(key) && self.hasOwnProperty(key) && opts.preserve.indexOf(key) === -1) { | ||
delete self[key] | ||
utils.forOwn(this, (value, key) => { | ||
if (key !== this._mapper().idAttribute && !previous.hasOwnProperty(key) && this.hasOwnProperty(key) && opts.preserve.indexOf(key) === -1) { | ||
delete this[key] | ||
} | ||
@@ -320,7 +307,7 @@ }) | ||
if (opts.preserve.indexOf(key) === -1) { | ||
self[key] = value | ||
this[key] = value | ||
} | ||
}) | ||
self.commit() | ||
return self | ||
this.commit() | ||
return this | ||
}, | ||
@@ -344,7 +331,6 @@ | ||
save (opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
const mapper = self._mapper() | ||
const id = utils.get(self, mapper.idAttribute) | ||
let props = self | ||
const mapper = this._mapper() | ||
const id = utils.get(this, mapper.idAttribute) | ||
let props = this | ||
if (utils.isUndefined(id)) { | ||
@@ -354,3 +340,3 @@ return superMethod(mapper, 'create')(props, opts) | ||
if (opts.changesOnly) { | ||
const changes = self.changes(opts) | ||
const changes = this.changes(opts) | ||
props = {} | ||
@@ -375,3 +361,2 @@ utils.fillIn(props, changes.added) | ||
'set' (key, value, opts) { | ||
const self = this | ||
if (utils.isObject(key)) { | ||
@@ -382,7 +367,7 @@ opts = value | ||
if (opts.silent) { | ||
self._set('silent', true) | ||
this._set('silent', true) | ||
} | ||
utils.set(self, key, value) | ||
if (!self._get('eventId')) { | ||
self._set('silent') // unset | ||
utils.set(this, key, value) | ||
if (!this._get('eventId')) { | ||
this._set('silent') // unset | ||
} | ||
@@ -389,0 +374,0 @@ }, |
@@ -514,3 +514,3 @@ import utils from './utils' | ||
if (!validType) { | ||
return makeError(value ? typeof value : '' + value, `one of (${type.join(', ')})`, opts) | ||
return makeError(!utils.isUndefined(value) && value !== null ? typeof value : '' + value, `one of (${type.join(', ')})`, opts) | ||
} | ||
@@ -626,3 +626,3 @@ // Run keyword validators for matched type | ||
// Check if property is required | ||
if (schema.required === true) { | ||
if (schema.required === true && !opts.existingOnly) { | ||
addError(value, 'a value', opts, errors) | ||
@@ -684,7 +684,6 @@ } | ||
descriptor.set = function (value) { | ||
const self = this | ||
// These are accessed a lot | ||
const _get = self[getter] | ||
const _set = self[setter] | ||
const _unset = self[unsetter] | ||
const _get = this[getter] | ||
const _set = this[setter] | ||
const _unset = this[unsetter] | ||
@@ -754,5 +753,5 @@ // Optionally check that the new value passes validation | ||
for (i = 0; i < changed.length; i++) { | ||
self.emit('change:' + changed[i], self, utils.get(self, changed[i])) | ||
this.emit('change:' + changed[i], this, utils.get(this, changed[i])) | ||
} | ||
self.emit('change', self, self.changes()) | ||
this.emit('change', this, this.changes()) | ||
} | ||
@@ -877,3 +876,2 @@ _unset(silentPath) | ||
constructor: function Schema (definition) { | ||
// const self = this | ||
definition || (definition = {}) | ||
@@ -880,0 +878,0 @@ // TODO: schema validation |
@@ -350,3 +350,3 @@ /** | ||
if (!utils.isFunction(equalsFn)) { | ||
equalsFn = utils.strictEqual | ||
equalsFn = utils.deepEqual | ||
} | ||
@@ -508,6 +508,8 @@ | ||
} | ||
Object.defineProperty(subClass, '__super__', { | ||
configurable: true, | ||
value: superClass | ||
}) | ||
if (!subClass.hasOwnProperty('__super__')) { | ||
Object.defineProperty(subClass, '__super__', { | ||
configurable: true, | ||
value: superClass | ||
}) | ||
} | ||
@@ -538,3 +540,3 @@ utils.addHiddenPropsToTarget(subClass.prototype, props) | ||
/** | ||
* Find the index of something according to the given checker function. | ||
* Find the last index of something according to the given checker function. | ||
* | ||
@@ -861,2 +863,12 @@ * @ignore | ||
pick (props, keys) { | ||
const _props = {} | ||
utils.forOwn(props, function (value, key) { | ||
if (keys.indexOf(key) !== -1) { | ||
_props[key] = value | ||
} | ||
}) | ||
return _props | ||
}, | ||
/** | ||
@@ -883,5 +895,7 @@ * TODO | ||
/** | ||
* TODO | ||
* Remove the last item found in array according to the given checker function. | ||
* | ||
* @ignore | ||
* @param {Array} array The array to search. | ||
* @param {Function} fn Checker function. | ||
*/ | ||
@@ -939,17 +953,26 @@ remove (array, fn) { | ||
*/ | ||
strictEqual (a, b) { | ||
let _equal = a === b | ||
if (!_equal) { | ||
if (utils.isObject(a) && utils.isObject(b)) { | ||
utils.forOwn(a, function (value, key) { | ||
_equal = _equal && utils.strictEqual(value, b[key]) | ||
}) | ||
utils.forOwn(b, function (value, key) { | ||
_equal = _equal && utils.strictEqual(value, a[key]) | ||
}) | ||
} else if (utils.isArray(a) && utils.isArray(b)) { | ||
a.forEach(function (value, i) { | ||
_equal = _equal && utils.strictEqual(value, b[i]) | ||
}) | ||
deepEqual (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
let _equal = true | ||
if (utils.isObject(a) && utils.isObject(b)) { | ||
utils.forOwn(a, function (value, key) { | ||
_equal = _equal && utils.deepEqual(value, b[key]) | ||
}) | ||
if (!_equal) { | ||
return _equal | ||
} | ||
utils.forOwn(b, function (value, key) { | ||
_equal = _equal && utils.deepEqual(value, a[key]) | ||
}) | ||
} else if (utils.isArray(a) && utils.isArray(b)) { | ||
a.forEach(function (value, i) { | ||
_equal = _equal && utils.deepEqual(value, b[i]) | ||
if (!_equal) { | ||
return false | ||
} | ||
}) | ||
} else { | ||
return false | ||
} | ||
@@ -956,0 +979,0 @@ return _equal |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
1810653
24962
62