js-data-localstorage
Version:
localStorage adapter for js-data.
11 lines • 11.5 kB
JavaScript
/*!
* js-data-localstorage
* @version 2.3.2 - Homepage <http://www.js-data.io/docs/dslocalstorageadapter>
* @author Jason Dobry <jason.dobry@gmail.com>
* @copyright (c) 2014-2015 Jason Dobry
* @license MIT <https://github.com/js-data/js-data-localstorage/blob/master/LICENSE>
*
* @overview localStorage adapter for js-data.
*/
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("js-data")):"function"==typeof define&&define.amd?define(["js-data"],e):"object"==typeof exports?exports.DSLocalStorageAdapter=e(require("js-data")):t.DSLocalStorageAdapter=e(t.JSData)}(this,function(t){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function r(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t){m.push(t)}function u(){m.length&&!b&&(b=!0,m[0]())}function a(t){m.length?i(t):(i(t),u())}function c(t){return new p.Promise(t).then(function(t){return b=!1,m.shift(),setTimeout(u,0),t},function(t){return b=!1,m.shift(),setTimeout(u,0),p.Promise.reject(t)})}var f=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),l=n(1),s=n(2),h=n(13),d=n(22),v=new l.DS,p=l.DSUtils,y=v.defaults.defaultFilter,g=function t(){o(this,t)};g.prototype.basePath="";var m=[],b=!1,w=function(){function t(e){o(this,t),e=e||{},this.defaults=new g,this.storage=e.storage||localStorage,p.deepMixIn(this.defaults,e)}return f(t,[{key:"getPath",value:function(t,e){return e=e||{},p.makePath(e.basePath||this.defaults.basePath||t.basePath,t.name)}},{key:"getIdPath",value:function(t,e,n){return e=e||{},p.makePath(e.basePath||this.defaults.basePath||t.basePath,t.endpoint,n)}},{key:"getIds",value:function(t,e){var n=void 0,r=this.getPath(t,e),o=this.storage.getItem(r);return n=o?p.fromJson(o):{}}},{key:"saveKeys",value:function(t,e,n){var r=this.getPath(e,n);p.isEmpty(t)?this.storage.removeItem(r):this.storage.setItem(r,p.toJson(t))}},{key:"ensureId",value:function(t,e,n){var r=this.getIds(e,n);if(p.isArray(t)){if(!t.length)return;p.forEach(t,function(t){r[t]=1})}else r[t]=1;this.saveKeys(r,e,n)}},{key:"removeId",value:function(t,e,n){var r=this.getIds(e,n);if(p.isArray(t)){if(!t.length)return;p.forEach(t,function(t){delete r[t]})}else delete r[t];this.saveKeys(r,e,n)}},{key:"GET",value:function(t){var e=this;return new p.Promise(function(n){var r=e.storage.getItem(t);n(r?p.fromJson(r):void 0)})}},{key:"PUT",value:function(t,e){var n=this,r=this;return r.GET(t).then(function(o){return o&&p.deepMixIn(o,p.removeCircular(e)),n.storage.setItem(t,p.toJson(o||e)),r.GET(t)})}},{key:"DEL",value:function(t){var e=this;return new p.Promise(function(n){e.storage.removeItem(t),n()})}},{key:"find",value:function(t,e,n){var o=this,i=void 0;return n=n||{},n.with=n.with||[],new p.Promise(function(u,a){o.GET(o.getIdPath(t,n||{},e)).then(function(t){return t?t:a(new Error("Not Found!"))}).then(function(e){i=e;var u=[];return p.forEach(t.relationList,function(e){var a=e.relation,c=t.getResource(a),f=null;p.contains(n.with,a)?f=a:p.contains(n.with,e.localField)&&(f=e.localField),f&&!function(){var l=p.deepMixIn({},n.orig?n.orig():n);l.with=n.with.slice(),l=p._(c,l),p.remove(l.with,f),p.forEach(l.with,function(t,e){t&&0===t.indexOf(f)&&t.length>=f.length&&"."===t[f.length]?l.with[e]=t.substr(f.length+1):l.with[e]=""});var s=void 0;if("hasOne"!==e.type&&"hasMany"!==e.type||!e.foreignKey)if("hasMany"===e.type&&e.localKeys){var d=[],v=i[e.localKeys]||[];v=Array.isArray(v)?v:p.keys(v),d=d.concat(v||[]),s=o.findAll(t.getResource(a),{where:r({},c.idAttribute,{in:p.filter(h(d),function(t){return t})})},l).then(function(t){return p.set(i,e.localField,t),t})}else("belongsTo"===e.type||"hasOne"===e.type&&e.localKey)&&(s=o.find(t.getResource(a),p.get(i,e.localKey),l).then(function(t){return p.set(i,e.localField,t),t}));else s=o.findAll(t.getResource(a),{where:r({},e.foreignKey,{"==":i[t.idAttribute]})},l).then(function(t){return"hasOne"===e.type&&t.length?p.set(i,e.localField,t[0]):p.set(i,e.localField,t),t});s&&u.push(s)}()}),p.Promise.all(u)}).then(function(){return u(i)}).catch(a)})}},{key:"findAll",value:function(t,e,n){var o=this,i=null;return n=n||{},n.with=n.with||[],new p.Promise(function(r,i){try{!function(){n=n||{},"allowSimpleWhere"in n||(n.allowSimpleWhere=!0);var i=[],u=p.keys(o.getIds(t,n));p.forEach(u,function(e){var r=o.storage.getItem(o.getIdPath(t,n,e));r&&i.push(p.fromJson(r))}),r(y.call(v,i,t.name,e,n))}()}catch(t){i(t)}}).then(function(e){i=e;var u=[];return p.forEach(t.relationList,function(e){var a=e.relation,c=t.getResource(a),f=null;p.contains(n.with,a)?f=a:p.contains(n.with,e.localField)&&(f=e.localField),f&&!function(){var l=p.deepMixIn({},n.orig?n.orig():n);l.with=n.with.slice(),l=p._(c,l),p.remove(l.with,f),p.forEach(l.with,function(t,e){t&&0===t.indexOf(f)&&t.length>=f.length&&"."===t[f.length]?l.with[e]=t.substr(f.length+1):l.with[e]=""});var s=void 0;"hasOne"!==e.type&&"hasMany"!==e.type||!e.foreignKey?"hasMany"===e.type&&e.localKeys?!function(){var n=[];p.forEach(i,function(t){var r=t[e.localKeys]||[];r=Array.isArray(r)?r:p.keys(r),n=n.concat(r||[])}),s=o.findAll(t.getResource(a),{where:r({},c.idAttribute,{in:p.filter(h(n),function(t){return t})})},l).then(function(t){return p.forEach(i,function(n){var r=[],o=n[e.localKeys]||[];o=Array.isArray(o)?o:p.keys(o),p.forEach(t,function(t){o&&p.contains(o,t[c.idAttribute])&&r.push(t)}),p.set(n,e.localField,r)}),t})}():("belongsTo"===e.type||"hasOne"===e.type&&e.localKey)&&(s=o.findAll(t.getResource(a),{where:r({},c.idAttribute,{in:p.filter(d(i,function(t){return p.get(t,e.localKey)}),function(t){return t})})},l).then(function(t){return p.forEach(i,function(n){p.forEach(t,function(t){t[c.idAttribute]===n[e.localKey]&&p.set(n,e.localField,t)})}),t})):s=o.findAll(t.getResource(a),{where:r({},e.foreignKey,{in:p.filter(d(i,function(e){return p.get(e,t.idAttribute)}),function(t){return t})})},l).then(function(n){return p.forEach(i,function(r){var o=[];p.forEach(n,function(n){p.get(n,e.foreignKey)===r[t.idAttribute]&&o.push(n)}),"hasOne"===e.type&&o.length?p.set(r,e.localField,o[0]):p.set(r,e.localField,o)}),n}),s&&u.push(s)}()}),p.Promise.all(u)}).then(function(){return i})}},{key:"create",value:function(t,e,n){var r=this;return c(function(o,i){a(function(){e[t.idAttribute]=e[t.idAttribute]||s(),n=n||{},r.PUT(p.makePath(r.getIdPath(t,n,e[t.idAttribute])),p.omit(e,t.relationFields||[])).then(function(e){r.ensureId(e[t.idAttribute],t,n),o(e)}).catch(i)})})}},{key:"createMany",value:function(t,e,n){var r=this;return c(function(o,i){a(function(){var u=[],a=[];return p.forEach(e,function(e){var o=e[t.idAttribute]=e[t.idAttribute]||s();a.push(o),n=n||{},u.push(r.PUT(p.makePath(r.getIdPath(t,n,o)),p.omit(e,t.relationFields||[])))}),r.ensureId(a,t,n),p.Promise.all(u).then(o).catch(i)})})}},{key:"update",value:function(t,e,n,r){var o=this;return c(function(i,u){a(function(){r=r||{},o.PUT(o.getIdPath(t,r,e),p.omit(n,t.relationFields||[])).then(function(e){o.ensureId(e[t.idAttribute],t,r),i(e)}).catch(u)})})}},{key:"updateAll",value:function(t,e,n,r){var o=this;return this.findAll(t,n,r).then(function(n){var i=[];return p.forEach(n,function(n){return i.push(o.update(t,n[t.idAttribute],p.omit(e,t.relationFields||[]),r))}),p.Promise.all(i)})}},{key:"destroy",value:function(t,e,n){var r=this;return c(function(o,i){a(function(){n=n||{},r.DEL(r.getIdPath(t,n,e)).then(function(){return r.removeId(e,t,n)}).then(function(){return o(null)},i)})})}},{key:"destroyAll",value:function(t,e,n){var r=this;return this.findAll(t,e,n).then(function(e){var o=[];return p.forEach(e,function(e){var i=e[t.idAttribute];o.push(i),r.storage.removeItem(r.getIdPath(t,n,i))}),r.removeId(o,t,n),o})}}]),t}();w.version={full:"<%= pkg.version %>",major:parseInt("<%= major %>",10),minor:parseInt("<%= minor %>",10),patch:parseInt("<%= patch %>",10),alpha:"<%= alpha %>",beta:"<%= beta %>"},t.exports=w},function(e,n){e.exports=t},function(t,e,n){function r(){return o(8)+"-"+o(4)+"-4"+o(3)+"-"+i(8,9,"a","b")+o(3)+"-"+o(12)}var o=n(3),i=n(4);t.exports=r},function(t,e,n){function r(t){t=t&&t>0?t:6;for(var e="";t--;)e+=o(i);return e}var o=n(4),i="0123456789abcdef".split("");t.exports=r},function(t,e,n){function r(t){var e=1===arguments.length&&i(t)?t:arguments;return e[o(0,e.length-1)]}var o=n(5),i=n(10);t.exports=r},function(t,e,n){function r(t,e){return t=null==t?o:~~t,e=null==e?i:~~e,Math.round(u(t-.5,e+.499999999999))}var o=n(6),i=n(7),u=n(8);t.exports=r},function(t,e){t.exports=-2147483648},function(t,e){t.exports=2147483647},function(t,e,n){function r(t,e){return t=null==t?i:t,e=null==e?u:e,t+(e-t)*o()}var o=n(9),i=n(6),u=n(7);t.exports=r},function(t,e){function n(){return n.get()}n.get=Math.random,t.exports=n},function(t,e,n){var r=n(11),o=Array.isArray||function(t){return r(t,"Array")};t.exports=o},function(t,e,n){function r(t,e){return o(t)===e}var o=n(12);t.exports=r},function(t,e){function n(t){return null===t?"Null":t===r?"Undefined":o.exec(i.call(t))[1]}var r,o=/^\[object (.*)\]$/,i=Object.prototype.toString;t.exports=n},function(t,e,n){function r(t,e){return e=e||o,i(t,function(t,n,r){for(var o=r.length;++n<o;)if(e(t,r[n]))return!1;return!0})}function o(t,e){return t===e}var i=n(14);t.exports=r},function(t,e,n){function r(t,e,n){e=o(e,n);var r=[];if(null==t)return r;for(var i,u=-1,a=t.length;++u<a;)i=t[u],e(i,u,t)&&r.push(i);return r}var o=n(15);t.exports=r},function(t,e,n){function r(t,e){if(null==t)return o;switch(typeof t){case"function":return"undefined"!=typeof e?function(n,r,o){return t.call(e,n,r,o)}:t;case"object":return function(e){return u(e,t)};case"string":case"number":return i(t)}}var o=n(16),i=n(17),u=n(18);t.exports=r},function(t,e){function n(t){return t}t.exports=n},function(t,e){function n(t){return function(e){return e[t]}}t.exports=n},function(t,e,n){function r(t,e){for(var n=-1,r=t.length;++n<r;)if(u(t[n],e))return!0;return!1}function o(t,e){for(var n=-1,o=e.length;++n<o;)if(!r(t,e[n]))return!1;return!0}function i(t,e){var n=!0;return a(e,function(e,r){return u(t[r],e)?void 0:n=!1}),n}function u(t,e){return t&&"object"==typeof t?c(t)&&c(e)?o(t,e):i(t,e):t===e}var a=n(19),c=n(10);t.exports=u},function(t,e,n){function r(t,e,n){i(t,function(r,i){return o(t,i)?e.call(n,t[i],i,t):void 0})}var o=n(20),i=n(21);t.exports=r},function(t,e){function n(t,e){return Object.prototype.hasOwnProperty.call(t,e)}t.exports=n},function(t,e,n){function r(){a=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],u=!0;for(var t in{toString:null})u=!1}function o(t,e,n){var o,f=0;null==u&&r();for(o in t)if(i(e,t,o,n)===!1)break;if(u)for(var l=t.constructor,s=!!l&&t===l.prototype;(o=a[f++])&&("constructor"===o&&(s||!c(t,o))||t[o]===Object.prototype[o]||i(e,t,o,n)!==!1););}function i(t,e,n,r){return t.call(r,e[n],n,e)}var u,a,c=n(20);t.exports=o},function(t,e,n){function r(t,e,n){e=o(e,n);var r=[];if(null==t)return r;for(var i=-1,u=t.length;++i<u;)r[i]=e(t[i],i,t);return r}var o=n(15);t.exports=r}])});
//# sourceMappingURL=dist/js-data-localstorage.min.map