UNPKG

js-data-cloudmine

Version:
11 lines 18.3 kB
/*! * js-data-cloudmine * @version 1.0.0-alpha.4 - Homepage <https://github.com/nvahalik/js-data-cloudmine> * @author Nick Vahalik <nick@codeandsalt.com> * @copyright (c) 2016 Nick Vahalik * @license MIT <https://github.com/nvahalik/js-data-cloudmine/blob/master/LICENSE> * * @overview CloudMine adapter for js-data. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("js-data")):"function"==typeof define&&define.amd?define("js-data-cloudmine",["exports","js-data"],t):t(e.JSDataCloudMine=e.JSDataCloudMine||{},e.JSData)}(this,function(e,t){"use strict";function n(e,n,r){n||(n={}),this.data=e,t.utils.fillIn(this,n),this.op=r}function r(e){t.utils.classCallCheck(this,r),t.Component.call(this,e),e||(e={}),t.utils.fillIn(e,b),t.utils.fillIn(this,e)}function i(e){g.push(e)}function o(){g.length&&!w&&(w=!0,g[0]())}function u(e){g.length?i(e):(i(e),o())}function a(e){return new t.utils.Promise(e).then(function(e){return w=!1,g.shift(),setTimeout(o,0),e},function(e){return w=!1,g.shift(),setTimeout(o,0),t.utils.reject(e)})}function s(e,n){return new t.utils.Promise(function(t,r){e.on("success",function(e,n){t([e,n])}).on("error",function(e,t){r([e,t])}),n.meta&&e.on("meta",n.meta),n.result&&e.on("result",n.result),n.complete&&e.on("complete",n.complete)})}function l(e){t.utils.classCallCheck(this,l),e||(e={}),r.call(this,e),this.ws=e.ws}var f="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},c=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},d=function(){function e(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var u,a=e[Symbol.iterator]();!(r=(u=a.next()).done)&&(n.push(u.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{!r&&a.return&&a.return()}finally{if(i)throw o}}return n}return function(t,n){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),h=function(){for(var e=arguments.length,n=Array(e),r=0;e>r;r++)n[r]=arguments[r];var i=n[n.length-1];return this.dbg.apply(this,[i.op].concat(n)),t.utils.resolve()},v=function(){for(var e=arguments.length,n=Array(e),r=0;e>r;r++)n[r]=arguments[r];var i=n[n.length-2];return this.dbg.apply(this,[i.op].concat(n)),t.utils.resolve()},p=function(e){var t={},n=[];return e.forEach(function(e){e in t||(n.push(e),t[e]=0)}),n},y=function(e,n,r){r||(r={}),r.with||(r.with=[]);var i=e.relationFields||[],o=i.filter(function(e){return-1===r.with.indexOf(e)});return t.utils.omit(n,o)},b={debug:!1,raw:!1};t.Component.extend({constructor:r,afterCount:v,afterCreate:v,afterCreateMany:v,afterDestroy:v,afterDestroyAll:v,afterFind:v,afterFindAll:v,afterSum:v,afterUpdate:v,afterUpdateAll:v,afterUpdateMany:v,beforeCount:h,beforeCreate:h,beforeCreateMany:h,beforeDestroy:h,beforeDestroyAll:h,beforeFind:h,beforeFindAll:h,beforeSum:h,beforeUpdate:h,beforeUpdateAll:h,beforeUpdateMany:h,count:function(e,r,i){var o=this,u=void 0;return r||(r={}),i||(i={}),u=i.op="beforeCount",t.utils.resolve(this[u](e,r,i)).then(function(){return u=i.op="count",o.dbg(u,e,r,i),t.utils.resolve(o._count(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];f||(f={});var c=new n(l,f,u);return c=o.respond(c,i),u=i.op="afterCount",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})},create:function(e,r,i){var o=this,u=void 0;return r||(r={}),i||(i={}),u=i.op="beforeCreate",t.utils.resolve(this[u](e,r,i)).then(function(n){return r=void 0===n?r:n,r=y(e,r,i),u=i.op="create",o.dbg(u,e,r,i),t.utils.resolve(o._create(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];f||(f={});var c=new n(l,f,"create");return c.created=l?1:0,c=o.respond(c,i),u=i.op="afterCreate",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})},createMany:function(e,r,i){var o=this,u=void 0;return r||(r={}),i||(i={}),u=i.op="beforeCreateMany",t.utils.resolve(this[u](e,r,i)).then(function(n){return r=void 0===n?r:n,r=r.map(function(t){return y(e,t,i)}),u=i.op="createMany",o.dbg(u,e,r,i),t.utils.resolve(o._createMany(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];l||(l=[]),f||(f={});var c=new n(l,f,"createMany");return c.created=l.length,c=o.respond(c,i),u=i.op="afterCreateMany",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})},destroy:function(e,r,i){var o=this,u=void 0;return i||(i={}),u=i.op="beforeDestroy",t.utils.resolve(this[u](e,r,i)).then(function(){return u=i.op="destroy",o.dbg(u,e,r,i),t.utils.resolve(o._destroy(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];f||(f={});var c=new n(l,f,"destroy");return c=o.respond(c,i),u=i.op="afterDestroy",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})},destroyAll:function(e,r,i){var o=this,u=void 0;return r||(r={}),i||(i={}),u=i.op="beforeDestroyAll",t.utils.resolve(this[u](e,r,i)).then(function(){return u=i.op="destroyAll",o.dbg(u,e,r,i),t.utils.resolve(o._destroyAll(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];f||(f={});var c=new n(l,f,"destroyAll");return c=o.respond(c,i),u=i.op="afterDestroyAll",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})},loadBelongsTo:function(e,n,r,i){var o=this,u=n.getRelation();if(!t.utils.isObject(r)||t.utils.isArray(r)){var a=r.map(function(t){return o.makeBelongsToForeignKey(e,n,t)}).filter(function(e){return e});return this.findAll(u,{where:c({},u.idAttribute,{in:a})},i).then(function(e){r.forEach(function(t){e.forEach(function(e){e[u.idAttribute]===t[n.foreignKey]&&n.setLocalField(t,e)})})})}var s=function(){var t=r;return{v:o.find(u,o.makeBelongsToForeignKey(e,n,t),i).then(function(e){n.setLocalField(t,e)})}}();return"object"===("undefined"==typeof s?"undefined":f(s))?s.v:void 0},find:function(e,r,i){var o=this,u=void 0,a=void 0,s={};return i||(i={}),i.with||(i.with=[]),a=i.op="beforeFind",t.utils.resolve(this[a](e,r,i)).then(function(){return a=i.op="find",o.dbg(a,e,r,i),t.utils.resolve(o._find(e,r,i))}).then(function(n){var r=d(n,2),a=r[0],l=r[1];if(a){u=a,s=l;var f=[];return t.utils.forEachRelation(e,i,function(t,n){var r=void 0;!t.foreignKey||"hasOne"!==t.type&&"hasMany"!==t.type?"hasMany"===t.type&&t.localKeys?r=o.loadHasManyLocalKeys(e,t,u,n):"hasMany"===t.type&&t.foreignKeys?r=o.loadHasManyForeignKeys(e,t,u,n):"belongsTo"===t.type&&(r=o.loadBelongsTo(e,t,u,n)):r="hasOne"===t.type?o.loadHasOne(e,t,u,n):o.loadHasMany(e,t,u,n),r&&f.push(r)}),t.utils.Promise.all(f)}}).then(function(){var l=new n(u,s,"find");return l.found=u?1:0,l=o.respond(l,i),a=i.op="afterFind",t.utils.resolve(o[a](e,r,i,l)).then(function(e){return void 0===e?l:e})})},findAll:function(e,r,i){var o=this;i||(i={}),i.with||(i.with=[]);var u=[],a={},s=void 0,l=i._activeWith;if(t.utils.isObject(l)){var f=l.query||{};l.replace?r=f:t.utils.deepFillIn(r,f)}return s=i.op="beforeFindAll",t.utils.resolve(this[s](e,r,i)).then(function(){return s=i.op="findAll",o.dbg(s,e,r,i),t.utils.resolve(o._findAll(e,r,i))}).then(function(n){var r=d(n,2),s=r[0],l=r[1];s||(s=[]),u=s,a=l;var f=[];return t.utils.forEachRelation(e,i,function(t,n){var r=void 0;!t.foreignKey||"hasOne"!==t.type&&"hasMany"!==t.type?"hasMany"===t.type&&t.localKeys?r=o.loadHasManyLocalKeys(e,t,u,n):"hasMany"===t.type&&t.foreignKeys?r=o.loadHasManyForeignKeys(e,t,u,n):"belongsTo"===t.type&&(r=o.loadBelongsTo(e,t,u,n)):r="hasMany"===t.type?o.loadHasMany(e,t,u,n):o.loadHasOne(e,t,u,n),r&&f.push(r)}),t.utils.Promise.all(f)}).then(function(){var l=new n(u,a,"findAll");return l.found=u.length,l=o.respond(l,i),s=i.op="afterFindAll",t.utils.resolve(o[s](e,r,i,l)).then(function(e){return void 0===e?l:e})})},getOpt:function(e,n){return n||(n={}),void 0===n[e]?t.utils.plainCopy(this[e]):t.utils.plainCopy(n[e])},loadHasMany:function(e,n,r,i){var o=this,u=!1;t.utils.isObject(r)&&!t.utils.isArray(r)&&(u=!0,r=[r]);var a=r.map(function(t){return o.makeHasManyForeignKey(e,n,t)}),s={where:{}},l=s.where[n.foreignKey]={};return u?l["=="]=a[0]:l.in=a.filter(function(e){return e}),this.findAll(n.getRelation(),s,i).then(function(i){r.forEach(function(r){var o=[];u?o=i:i.forEach(function(i){t.utils.get(i,n.foreignKey)===r[e.idAttribute]&&o.push(i)}),n.setLocalField(r,o)})})},loadHasManyLocalKeys:function(e,n,r,i){var o=this,u=void 0,a=n.getRelation();if(t.utils.isObject(r)&&!t.utils.isArray(r)&&(u=r),u)return this.findAll(a,{where:c({},a.idAttribute,{in:this.makeHasManyLocalKeys(e,n,u)})},i).then(function(e){n.setLocalField(u,e)});var s=function(){var u=[];return r.forEach(function(t){u=u.concat(o.makeHasManyLocalKeys(e,n,t))}),{v:o.findAll(a,{where:c({},a.idAttribute,{in:p(u).filter(function(e){return e})})},i).then(function(e){return r.forEach(function(r){var i=[],o=t.utils.get(r,n.localKeys)||[];o=t.utils.isArray(o)?o:Object.keys(o),e.forEach(function(e){o&&-1!==o.indexOf(e[a.idAttribute])&&i.push(e)}),n.setLocalField(r,i)}),e})}}();return"object"===("undefined"==typeof s?"undefined":f(s))?s.v:void 0},loadHasManyForeignKeys:function(e,n,r,i){var o=this,u=n.getRelation(),a=e.idAttribute,s=void 0;return t.utils.isObject(r)&&!t.utils.isArray(r)&&(s=r),s?this.findAll(n.getRelation(),{where:c({},n.foreignKeys,{contains:this.makeHasManyForeignKeys(e,n,s)})},i).then(function(e){n.setLocalField(s,e)}):this.findAll(u,{where:c({},n.foreignKeys,{isectNotEmpty:r.map(function(t){return o.makeHasManyForeignKeys(e,n,t)})})},i).then(function(e){var i=n.foreignKeys;r.forEach(function(r){var o=[],u=t.utils.get(r,a);e.forEach(function(n){var r=t.utils.get(e,i)||[];-1!==r.indexOf(u)&&o.push(n)}),n.setLocalField(r,o)})})},loadHasOne:function(e,n,r,i){return t.utils.isObject(r)&&!t.utils.isArray(r)&&(r=[r]),this.loadHasMany(e,n,r,i).then(function(){r.forEach(function(e){var r=n.getLocalField(e);t.utils.isArray(r)&&r.length&&n.setLocalField(e,r[0])})})},makeHasManyForeignKey:function(e,t,n){return t.getForeignKey(n)},makeHasManyLocalKeys:function(e,n,r){var i=[],o=t.utils.get(r,n.localKeys)||[];return o=t.utils.isArray(o)?o:Object.keys(o),i=i.concat(o),p(i).filter(function(e){return e})},makeHasManyForeignKeys:function(e,n,r){return t.utils.get(r,e.idAttribute)},makeBelongsToForeignKey:function(e,t,n){return t.getForeignKey(n)},sum:function(e,r,i,o){var u=this,a=void 0;if(!t.utils.isString(r))throw new Error("field must be a string!");return i||(i={}),o||(o={}),a=o.op="beforeSum",t.utils.resolve(this[a](e,r,i,o)).then(function(){return a=o.op="sum",u.dbg(a,e,r,i,o),t.utils.resolve(u._sum(e,r,i,o))}).then(function(s){var l=d(s,2),f=l[0],c=l[1];c||(c={});var h=new n(f,c,a);return h=u.respond(h,o),a=o.op="afterSum",t.utils.resolve(u[a](e,r,i,o,h)).then(function(e){return void 0===e?h:e})})},respond:function(e,t){return this.getOpt("raw",t)?e:e.data},update:function(e,r,i,o){var u=this;i||(i={}),o||(o={});var a=void 0;return a=o.op="beforeUpdate",t.utils.resolve(this[a](e,r,i,o)).then(function(n){return i=void 0===n?i:n,i=y(e,i,o),a=o.op="update",u.dbg(a,e,r,i,o),t.utils.resolve(u._update(e,r,i,o))}).then(function(s){var l=d(s,2),f=l[0],c=l[1];c||(c={});var h=new n(f,c,"update");return h.updated=f?1:0,h=u.respond(h,o),a=o.op="afterUpdate",t.utils.resolve(u[a](e,r,i,o,h)).then(function(e){return void 0===e?h:e})})},updateAll:function(e,r,i,o){var u=this;r||(r={}),i||(i={}),o||(o={});var a=void 0;return a=o.op="beforeUpdateAll",t.utils.resolve(this[a](e,r,i,o)).then(function(n){return r=void 0===n?r:n,r=y(e,r,o),a=o.op="updateAll",u.dbg(a,e,r,i,o),t.utils.resolve(u._updateAll(e,r,i,o))}).then(function(s){var l=d(s,2),f=l[0],c=l[1];f||(f=[]),c||(c={});var h=new n(f,c,"updateAll");return h.updated=f.length,h=u.respond(h,o),a=o.op="afterUpdateAll",t.utils.resolve(u[a](e,r,i,o,h)).then(function(e){return void 0===e?h:e})})},updateMany:function(e,r,i){var o=this;r||(r=[]),i||(i={});var u=void 0,a=e.idAttribute;return r=r.filter(function(e){return t.utils.get(e,a)}),u=i.op="beforeUpdateMany",t.utils.resolve(this[u](e,r,i)).then(function(n){return r=void 0===n?r:n,r=r.map(function(t){return y(e,t,i)}),u=i.op="updateMany",o.dbg(u,e,r,i),t.utils.resolve(o._updateMany(e,r,i))}).then(function(a){var s=d(a,2),l=s[0],f=s[1];l||(l=[]),f||(f={});var c=new n(l,f,"updateMany");return c.updated=l.length,c=o.respond(c,i),u=i.op="afterUpdateMany",t.utils.resolve(o[u](e,r,i,c)).then(function(e){return void 0===e?c:e})})}});var g=[],w=!1,m=r.prototype;l.prototype=Object.create(r.prototype,{constructor:{value:l,enumerable:!1,writable:!0,configurable:!0}}),Object.defineProperty(l,"__super__",{configurable:!0,value:r}),l.extend=t.utils.extend,t.utils.addHiddenPropsToTarget(l.prototype,{_count:function(e,t,n){return t||(t={}),n||(n={}),t.__mapper__=e.name,t.count=!0,this._findAll(e,t,n).then(function(e){return e[0]=e[1].count,e[1].data=e[1].count,e})},_create:function(e,t,n){return t||(t={}),n||(n={}),this._upsert(e,t,n)},_upsert:function(e,n,r){var i=t.utils.plainCopy(n);r||(r={});var o=t.utils.get(i,e.idAttribute);return t.utils.isSorN(o)||(o=null),i.__mapper__=e.name,s(this.ws.set(o,i),r).then(function(n){var r=n[0];if(t.utils.set(i,e.idAttribute,Object.keys(r)[0]),!r)throw new Error("Not Found");return delete i.__mapper__,[i]})},_upsertBatch:function(e,n,r){var i=this;r||(r={});var o=[];return n.forEach(function(t){o.push(i._upsert(e,t,r))}),t.utils.Promise.all(o).then(function(e){return e.map(function(e){return e[0]})}).then(function(e){return[e]})},_createMany:function(e,t,n){return n||(n={}),this._upsertBatch(e,t,n)},_destroy:function(e,t,n){return n||(n={}),s(this.ws.destroy(t),n).then(function(){return[void 0,{}]}).catch(function(e){return console.error(e),[void 0,{}]})},_destroyAll:function(e,n,r){var i=this;return n||(n={}),r||(r={}),n.__mapper__=e.name,this._findAll(e,n).then(function(n){var o=d(n,1),u=o[0],a=e.idAttribute;return t.utils.Promise.all(u.map(function(n){return i._destroy(e,t.utils.get(n,a),r)}))}).then(function(){return[void 0,{}]})},_find:function(e,n,r){return r||(r={}),s(this.ws.get(n),r).then(function(r){var i=r[0][n];return t.utils.set(i,e.idAttribute,n),delete i.__mapper__,r[0]=i,[i,{}]}).catch(function(e){return console.error(e.stack),[void 0,{}]})},_findAll:function(e,n,r){var i=this;n||(n={}),r||(r={});var o={},u=[],a=void 0;if(void 0!==n.count&&(o={count:n.count},delete n.count),t.utils.isObject(n.where)&&1===Object.keys(n.where).length){var l,c=function(){var o=Object.keys(n.where[Object.keys(n.where)[0]])[0];if(n.where.hasOwnProperty(e.idAttribute)){if(t.utils.isString(n.where[e.idAttribute]))a=s(i.ws.get(n.where[e.idAttribute]),r);else if(t.utils.isObject(n.where[e.idAttribute])&&(n.where[e.idAttribute].hasOwnProperty("in")||n.where[e.idAttribute].hasOwnProperty("=="))){if("in"===o&&0===n.where[e.idAttribute].length)return{v:Promise.resolve([void 0,{}])};a=s(i.ws.get(n.where[e.idAttribute][o]),r)}}else l=Object.keys(n.where)[0],e.hasOwnProperty("relationList")&&e.relationList.forEach(function(t){t.foreignKey===l&&t.mapper!==e&&(a=s(i.ws.get(n.where[l][o]),r))}),e.hasOwnProperty("relationFields")&&e.relationFields.indexOf(Object.keys(n.where)[0])!==!1&&!function(){var t=Object.keys(n.where)[0],o=void 0;if(e.relationList.forEach(function(e){e.localField===t&&(o=e)}),o){var u=Object.keys(n.where[t])[0];a=s(i.ws.get(n.where[t][u]),r)}}();Object.keys(n.where).forEach(function(e){if(t.utils.isObject(n.where[e]))Object.keys(n.where[e]).forEach(function(r){var i=void 0,o="=="===r?"=":r;if(t.utils.isNumber(n.where[e][r]))i=n.where[e][r];else if("in"===r&&t.utils.isArray(n.where[e][r])){var a=function(){var i=[];return n.where[e][r].forEach(function(n){i.push(t.utils.isNumber(n)?e+" = "+n:e+' = "'+n+'"')}),u.push(i.join(" or ")),{v:void 0}}();if("object"===("undefined"==typeof a?"undefined":f(a)))return a.v}else i='"'+n.where[e][r]+'"';u.push(e+" "+o+" "+i)});else{var r=void 0;r=t.utils.isNumber(n.where[e])?n.where[e]:'"'+n.where[e]+'"',u.push(e+" = "+r)}})}();if("object"===("undefined"==typeof c?"undefined":f(c)))return c.v}return void 0===r._activeWith&&(n.__mapper__=e.name),Object.keys(n).forEach(function(e){if("where"!==e)if(t.utils.isObject(n[e]))Object.keys(n[e]).forEach(function(r){var i=void 0,o="=="===r?"=":r;i=t.utils.isNumber(n[e][r])?n[e][r]:'"'+n[e][r]+'"',u.push(e+" "+o+" "+i)});else{var r=void 0;r=t.utils.isNumber(n[e])?n[e]:'"'+n[e]+'"',u.push(e+" = "+r)}}),a||(a=s(this.ws.search("["+u.join(", ")+"]",o),r)),a.then(function(n){var i=[],o=n[0];for(var u in o){var a=o[u];void 0!==r._activeWith&&a.__mapper__!==e.name||(t.utils.set(a,e.idAttribute,u),delete a.__mapper__,i.push(a))}return n[0]=i,delete n[1]._events,n})},_sum:function(e,n,r,i){return this._findAll(e,r,i).then(function(e){return e[0]=e[0].reduce(function(e,r){return e+(t.utils.get(r,n)||0)},0),e})},_update:function(e,n,r,i){var o=this;return r||(r={}),i||(i={}),this._find(e,n).then(function(n){if(n=n[0],!n)throw new Error("Not Found");return t.utils.deepMixIn(n,r),o._upsert(e,n,i)}).then(function(e){if(!e)throw new Error("Not Found");return e})},_updateAll:function(e,n,r,i){var o=this;return i||(i={}),n||(n={}),r||(r={}),this._findAll(e,r,i).then(function(r){var u=d(r,1),a=u[0];return a.forEach(function(e){return t.utils.deepMixIn(e,n)}),o._upsertBatch(e,a,i)})},_updateMany:function(e,t,n){return n||(n={}),this._upsertBatch(e,t,n)},create:function(e,t,n){var r=this;return a(function(i,o){u(function(){m.create.call(r,e,t,n).then(i,o)})})},createMany:function(e,t,n){var r=this;return a(function(i,o){u(function(){m.createMany.call(r,e,t,n).then(i,o)})})},destroy:function(e,t,n){var r=this;return a(function(i,o){u(function(){m.destroy.call(r,e,t,n).then(i,o)})})},destroyAll:function(e,t,n){var r=this;return a(function(i,o){u(function(){m.destroyAll.call(r,e,t,n).then(i,o)})})},update:function(e,t,n,r){var i=this;return a(function(o,a){u(function(){m.update.call(i,e,t,n,r).then(o,a)})})},updateAll:function(e,t,n,r){var i=this;return a(function(o,a){u(function(){m.updateAll.call(i,e,t,n,r).then(o,a)})})},updateMany:function(e,t,n){var r=this;return a(function(i,o){u(function(){m.updateMany.call(r,e,t,n).then(i,o)})})}});var _={alpha:4,full:"1.0.0-alpha.4",major:1,minor:0,patch:0};e.CloudMineAdapter=l,e.version=_,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=js-data-cloudmine.min.map