text: !function(a,b){"use strict";if("function"==typeof define&&define.amd)define("mondo",["underscore","jquery","URIjs/URI","nedb","q"],b);else if("undefined"!=typeof exports){var c=require("underscore"),d=require("jquery")(a),e=require("URIjs"),f=require("nedb"),g=require("q");module.exports=b(c,d,e,f,g)}else a.mondo=b(a._,a.jQuery,a.URI,a.Nedb,a.Q)}(this,function(a,b,c,d,e){"use strict";function f(a){return a instanceof f?a:this instanceof f?void(this._wrapped=a):new f(a)}function g(a,b,c){return this instanceof g?(this._mondo=a,this.name=b,void(this.options=c||{})):new g(a,b,c)}function h(a){this.attributes=a}function i(a){return this instanceof i?(this._stores=[],void(this.options=a||{})):new i(a)}function j(a,b,c){return this instanceof j?(this._mondo=a,this._collection=b,this._options=c,void(this._query={options:{}})):new j(a,b,c)}function k(a,b){return this instanceof k?(this.name=a,void(this.options=b||{})):new k(a)}function l(a,b,c){return this instanceof l?(k.call(this,a,c),void(this._storage=b)):new l(a,b,c)}function m(a){return this instanceof m?void k.call(this,"IndexedDB",a):new m(a)}function n(a){return this instanceof n?void l.call(this,"LocalStorage",window.localStorage,a):new n(a)}function o(a){return this instanceof o?void k.call(this,"NeDB",a):new o(a)}function p(a,b){if(!(this instanceof p))return new p(a,b);if("string"!=typeof a&&(b=a,a=void 0),k.call(this,"Rest",b),this._rootUrl=a||this.options.url,"undefined"==typeof this._rootUrl)throw new TypeError("url must not be undefined")}function q(a){return this instanceof q?void(this._uri=new c(a)):new q(a)}function r(a){return this instanceof r?void l.call(this,"SessionStorage",window.sessionStorage,a):new r(a)}function s(a){return this instanceof s?void k.call(this,"WebSQL",a):new s(a)}f.extend=function(a){for(var b=1,c=arguments.length;c>b;b++){var d=arguments[b];if(d)for(var e in d)a[e]=d[e]}return a},f.forEach=function(b,c,d){var e={};if(null==b)return b;if(Array.prototype.forEach&&b.forEach===Array.prototype.forEach)b.forEach(c,d);else if(b.length===+b.length){for(var f=0,g=b.length;g>f;f++)if(c.call(d,b[f],f,b)===e)return}else for(var h=a.keys(b),f=0,g=h.length;g>f;f++)if(c.call(d,b[h[f]],h[f],b)===e)return;return b},f.keys=function(b){if(!a.isObject(b))return[];if(Object.keys)return Object.keys(b);var c=[];for(var d in b)a.has(b,d)&&c.push(d);return c},f.map=function(a,b,c){var d=[];return null==a?d:Array.prototype.map&&a.map===Array.prototype.map?a.map(b,c):(f.forEach(a,function(a,e,f){d.push(b.call(c,a,e,f))}),d)},f.filter=function(a,b,c){var d=[];return null==a?d:Array.prototype.filter&&a.filter===Array.prototype.filter?a.filter(b,c):(each(a,function(a,e,f){b.call(c,a,e,f)&&d.push(a)}),d)},f.isArray=Array.isArray||function(a){return"[object Array]"==toString.call(a)},f.indexOf=function(b,c,d){if(null==b)return-1;var e=0,f=b.length;if(d){if("number"!=typeof d)return e=a.sortedIndex(b,c),b[e]===c?e:-1;e=0>d?Math.max(0,f+d):d}if(Array.prototype.indexOf&&b.indexOf===Array.prototype.indexOf)return b.indexOf(c,d);for(;f>e;e++)if(b[e]===c)return e;return-1},f.isEmpty=function(b){if(null===b)return!0;for(var c in b)if(a.has(b,c))return!1;return!0},f.mixin=function(a,b){for(var c in b)if("function"==typeof b[c]){var d=a[c]=b[c];a.prototype[c]=function(){var a=[this._wrapped];return Array.prototype.push.apply(a,arguments),d.apply(this,a)}}},f.mixin(f,f),g.prototype.filter=function(a,b,c,d){"function"==typeof a?(d=a,a={},b=void 0,c={}):"function"==typeof b?(d=b,b=void 0,c={}):"function"==typeof c&&(d=c,c={}),c=c||{};var e=new j(this._mondo,this,c);return e.from(this.name).filter(a),b&&e.select(b),f.forEach(["sort","skip","limit","lean"],function(a){c[a]&&e[a](c[a])}),d&&e.exec(d),e},g.prototype.find=function(a,b,c,d){"function"==typeof a?(d=a,a={},b=void 0,c={}):"function"==typeof b?(d=b,b=void 0,c={}):"function"==typeof c&&(d=c,c={}),c=c||{};var e=new j(this._mondo,this,c);return e.from(this.name).find(a),b&&e.select(b),f.forEach(["lean"],function(a){c[a]&&e[a](c[a])}),d&&e.exec(d),e},g.prototype.count=function(a,b,c){"function"==typeof a?(c=a,a={},b={}):"function"==typeof b&&(c=b,b={}),b=b||{};var d=new j(this._mondo,this,b);return d.from(this.name).count(a),f.forEach(["skip","limit"],function(a){b[a]&&d[a](b[a])}),c&&d.exec(c),d},g.prototype.distinct=function(a,b,c,d){"function"==typeof b?(d=b,b=void 0,c={}):"function"==typeof c&&(d=c,c={}),c=c||{};var e=new j(this._mondo,this,c);return e.from(this.name).distinct(a),b&&e.select(b),f.forEach(["lean"],function(a){c[a]&&e[a](c[a])}),d&&e.exec(d),e},g.prototype.mapReduce=function(a,b,c,d){"function"==typeof c&&(d=c,c={});var e=new j(this._mondo,this,c);return e.from(this.name).mapReduce(a,b),f.forEach(["qb","sort","limit"],function(a){c[a]&&e[a](c[a])}),d&&e.exec(d),e},g.prototype.insert=function(a,b,c){"function"==typeof b&&(c=b,b={}),b=b||{};var d=new j(this._mondo,this,b);return d.from(this.name).insert(a),c&&d.exec(c),d},g.prototype.update=function(a,b,c,d){"function"==typeof a?(d=a,a={},b=void 0,c={}):"function"==typeof b?(d=b,b=void 0,c={}):"function"==typeof c&&(d=c,c={}),c=c||{};var e=new j(this._mondo,this,c);return e.from(this.name).update(b).where(a),f.forEach(["upsert","multi"],function(a){c[a]&&e[a](c[a])}),d&&e.exec(d),e},g.prototype.remove=function(a,b,c){"function"==typeof a?(c=a,a={},b={}):"function"==typeof b&&(c=b,b={}),b=b||{};var d=new j(this._mondo,this,b);return d.from(this.name).remove(a),f.forEach(["single"],function(a){b[a]&&d[a](b[a])}),c&&d.exec(c),d},g.prototype.findById=function(a,b,c,d){return this.findOne({_id:a},b,c,d)},g.prototype.removeById=function(a,b,c,d){return this.remove({_id:a},b,c,d)},g.prototype.updateById=function(a,b,c,d){return this.update({_id:a},b,c,d)},h.collection=null,h.prototype.defaults={},h.prototype.attributes={},h.prototype.changedAttributes={},h.prototype.idAttribute="_id",h.prototype.set=function(){},h.prototype.get=function(){},h.prototype.markModified=function(){},h.prototype.isModified=function(){},h.prototype.isNew=function(){},h.prototype.update=function(a,b,c){this.set(a),this.save(c)},h.prototype.save=function(a){this._collection.update({_id:this.get("_id")},{upsert:!0},a)},h.prototype.remove=function(a){this._collection.remove({_id:this.get("_id")},a)},i.version="0.0.0",i.Model=h,i.Collection=g,i.QueryBuilder=j,i.stores={_abstract:k,localStorage:n,sessionStorage:r,nedb:o,rest:p,webSQL:s},i.prototype.use=function(a){this._stores.push(a)},i.prototype.unuse=function(a){for(var b=0,c=this._stores.length;c>b;b++)if(this._stores[b].name===a)return this._stores.splice(b,1),!0;return!1},i.prototype.collection=function(a,b){"string"!=typeof a&&(b=a);var c=new g(this,a,b);return f.forEach(this._stores,function(a){a.initCollection(c)}),c},i.prototype.handle=function(a,b,c){function d(c){if(c&&h.options.onError)return h.options.onError(c);var f=g[i++];if(!f){var j=e.defer();return j.resolve(null),j.promise}return f.handle(a,b,d)}var g;c=c||{},g=c.stores?f.filter(this._stores,function(a){return f.indexOf(c.stores,a.name)>-1}):this._stores;var h=this,i=0;return d()};var t={from:"collectionName",setCommand:"command",select:"fields",skip:"skip",limit:"limit",sort:"sort",multi:"multi",setDoc:"doc"};return f.forEach(Object.keys(t),function(a){j.prototype[a]=function(b){return this._query[t[a]]=b,this}}),j.prototype.where=function(a){return"undefined"==typeof a&&(a={}),this._query.query=a,this},j.prototype.lean=function(a){return"undefined"==typeof a&&(a=!0),this._query.options.lean=a,this},j.prototype.toQuery=function(){return this._query},j.prototype.exec=function(a){var b=this._mondo.handle(this._collection,this._query,this._options);return a&&b.then(function(){a.apply(null,[null].concat([].slice.call(arguments,0)))},function(b){a(b)}),b},j.prototype.insert=function(a){return this.setCommand("insert"),this.setDoc(a),this},j.prototype.filter=function(a){return this.setCommand("filter"),this.where(a),this},j.prototype.find=function(a){return this.setCommand("find"),this.where(a),this},j.prototype.update=function(a){return this.setCommand("update"),this.setDoc(a),this},j.prototype.remove=function(a){return this.setCommand("remove"),this.where(a),this},j.prototype.count=function(a){return this.setCommand("count"),this.where(a),this},j.prototype.mapReduce=function(a,b){return this.setCommand("mapReduce"),this._map=a,this._reduce=b,this},j.prototype.distinct=function(a){return this.setCommand("distinct"),this.where(a),this},k.prototype.initCollection=function(){},k.prototype.handle=function(a,b,c,d){var e=this;if("function"==typeof this.options.beforeHandle&&this.options.beforeHandle(a,b),"function"==typeof this[b.command])return this[b.command](a,b,c,function(c){c&&"function"==typeof e.options.onError&&e.options.onError(c),"function"==typeof e.options.beforeNext&&e.options.beforeNext(a,b),d(c)});throw new Error("Handler for operation "+b.command+" not defined")},f.forEach(["filter","find","count","mapReduce","distinct","insert","update","remove"],function(a){k.prototype[a]=function(){throw new Error("You have not implement "+this.name+"#"+a+"()")}}),l.prototype=new k,l.prototype.constructor=l,l.prototype._get=function(a){var b;try{b=JSON.parse(this._storage[a])}catch(c){b=[]}return f.isArray(b)||(b=[]),b},l.prototype._save=function(a,b){this._storage[a]=JSON.stringify(b)},l.prototype.filter=function(a,b){var c=this._get(b.collectionName);return e.Promise(function(a){options.transform&&!b.options.lean&&a(f.map(c,options.transform)),a(c)})},l.prototype.insert=function(a,b,c,d){var e,g=this._get(b.collectionName);e=f.isArray(b.doc)?b.doc:[b.doc],g=g.concat(e),this._save(b.collectionName,g);var i=f.map(e,function(a){return new h(a)});return setTimeout(function(){c.resolve(i)},0),d()},m.prototype=new k,m.prototype.constructor=m,n.prototype=new l,n.prototype.constructor=n,o.prototype=new k,o.prototype.constructor=o,o.prototype.initCollection=function(a){a._nedb=new d},p.prototype=new k,p.prototype.constructor=p,p.prototype.filter=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).where(c.query),c.fields&&d.select(c.fields),c.options.sort&&d.sort(c.options.sort),c.options.skip&&d.skip(c.options.skip),c.options.limit&&d.limit(c.options.limit),e(b.ajax(f.extend(this.options,{type:"GET",url:d.toString()}))).then(function(b){return a.options.transform&&!c.options.lean&&(b=f.map(b,function(b){return a.options.transform(b,a)})),b})},p.prototype.find=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).setOp("find").where(c.query),c.fields&&d.select(c.fields),c.options.sort&&d.sort(c.options.sort),c.options.skip&&d.skip(c.options.skip),c.options.limit&&d.limit(c.options.limit),e(b.ajax(f.extend(this.options,{type:"GET",url:d.toString()}))).then(function(b){return a.options.transform&&!c.options.lean?a.options.transform(b,a):b})},p.prototype.count=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).setOp("count").where(c.query),c.options.skip&&d.skip(c.options.skip),c.options.limit&&d.limit(c.options.limit),e(b.ajax(f.extend(this.options,{type:"GET",url:d.toString()})))},p.prototype.mapReduce=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).setOp("map_reduce").map(c.map).reduce(c.reduce).where(c.query),c.options.sort&&d.sort(c.options.sort),c.options.limit&&d.limit(c.options.limit),e(b.ajax(f.extend(this.options,{type:"GET",url:d.toString()})))},p.prototype.distinct=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).setOp("distinct").key(c.key),e(b.ajax(f.extend(this.options,{type:"GET",url:d.toString()}))).then(function(b){return a.options.transform&&!c.options.lean?a.options.transform(b,a):b})},p.prototype.insert=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName),e(b.ajax(f.extend(this.options,{type:"POST",url:d.toString(),data:JSON.stringify(c.doc)}))).then(function(b){return a.options.transform&&!c.options.lean?f.isArray(c.doc)?f.map(b,function(b){return a.options.transform(b,a)}):a.options.transform(b,a):b})},p.prototype.update=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).where(c.query),c.options.multi&&d.multi(c.options.multi),c.options.upsert&&d.upsert(c.options.upsert),e(b.ajax(f.extend(this.options,{type:"PATCH",url:d.toString(),data:JSON.stringify(c.doc)}))).then(function(b){return a.options.transform&&!c.options.lean?c.options.multi?f.map(b,function(b){return a.options.transform(b,a)}):a.options.transform(b,a):b})},p.prototype.remove=function(a,c){var d=new q(this._rootUrl);return d.from(c.collectionName).where(c.query),c.options.multi&&d.multi(c.options.multi),e(b.ajax(f.extend(this.options,{type:"DELETE",url:d.toString()}))).then(function(b){return a.options.transform&&!c.options.lean?c.options.multi?f.map(b,function(b){return a.options.transform(b,a)}):a.options.transform(b,a):b})},q.prototype.from=function(a){return this._uri.filename(a),this},q.prototype.setOp=function(a){return"find"!==a&&this._uri.addSearch("op",a),this},q.prototype.where=function(a){return f.isEmpty(a)||this._uri.addSearch("query",JSON.stringify(a)),this},q.prototype.skip=function(a){return this._uri.addSearch("skip",a),this},q.prototype.limit=function(a){return this._uri.addSearch("limit",a),this},q.prototype.sort=function(a){return this._uri.addSearch("sort",JSON.stringify(a)),this},q.prototype.toString=function(){return this._uri.toString()},r.prototype=new l,r.prototype.constructor=r,s.prototype=new k,s.prototype.constructor=s,i});
  //# sourceMappingURL=mondo.min.js.map