!function(y){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=y();else if("function"==typeof define&&define.amd)define([],y);else{var h;"undefined"!=typeof window?h=window:"undefined"!=typeof global?h=global:"undefined"!=typeof self&&(h=self);h.ForerunnerDB=y()}}(function(){return function h(n,k,d){function a(b,e){if(!k[b]){if(!n[b]){var c="function"==typeof require&&require;if(!e&&c)return c(b,!0);if(g)return g(b,!0);c=Error("Cannot find module '"+b+"'");throw c.code="MODULE_NOT_FOUND",
c;}c=k[b]={exports:{}};n[b][0].call(c.exports,function(c){var e=n[b][1][c];return a(e?e:c)},c,c.exports,h,n,k,d)}return k[b].exports}for(var g="function"==typeof require&&require,b=0;b<d.length;b++)a(d[b]);return a}({1:[function(h,n,k){k=h("../lib/Core");h("../lib/CollectionGroup");h("../lib/View");h("../lib/Highchart");h("../lib/Persist");h("../lib/Document");h("../lib/Overview");n.exports=k},{"../lib/CollectionGroup":4,"../lib/Core":5,"../lib/Document":7,"../lib/Highchart":8,"../lib/Overview":18,
"../lib/Persist":20,"../lib/View":23}],2:[function(h,n,k){k=h("./Shared");h("./Path");h=function(d){var a;this._primaryKey="_id";this._keyArr=[];this._data=[];this._objLookup={};this._count=0;for(a in d)d.hasOwnProperty(a)&&this._keyArr.push({key:a,dir:d[a]})};k.addModule("ActiveBucket",h);k.synthesize(h.prototype,"primaryKey");h.prototype.qs=function(d,a,g,b){if(!a.length)return 0;for(var f=-1,e,c,m=0,u=a.length-1;u>=m;){e=Math.floor((m+u)/2);if(f===e)break;f=a[e];void 0!==f&&(c=b(this,d,g,f),0<
c&&(m=e+1),0>c&&(u=e-1));f=e}return 0<c?e+1:e};h.prototype._sortFunc=function(d,a,g,b){g=g.split(".:.");b=b.split(".:.");var f=d._keyArr,e=f.length,c,m,u;for(c=0;c<e;c++)if(m=f[c],u=typeof a[m.key],"number"===u&&(g[c]=Number(g[c]),b[c]=Number(b[c])),g[c]!==b[c]){if(1===m.dir)return d.sortAsc(g[c],b[c]);if(-1===m.dir)return d.sortDesc(g[c],b[c])}};h.prototype.insert=function(d){var a,g;a=this.documentKey(d);g=this._data.indexOf(a);-1===g&&(g=this.qs(d,this._data,a,this._sortFunc));this._data.splice(g,
0,a);this._objLookup[d[this._primaryKey]]=a;this._count++;return g};h.prototype.remove=function(d){var a,g;return(a=this._objLookup[d[this._primaryKey]])?(g=this._data.indexOf(a),this._data.splice(g,0,a),delete this._objLookup[d[this._primaryKey]],this._count--,!0):!1};h.prototype.index=function(d){var a,g;a=this.documentKey(d);g=this._data.indexOf(a);-1===g&&(g=this.qs(d,this._data,a,this._sortFunc));return g};h.prototype.documentKey=function(d){var a="",g=this._keyArr,b=g.length,f,e;for(f=0;f<b;f++)e=
g[f],a&&(a+=".:."),a+=d[e.key];return a+=".:."+d[this._primaryKey]};h.prototype.count=function(){return this._count};h.prototype.sortAsc=function(d,a){return"string"===typeof d&&"string"===typeof a?d.localeCompare(a):d>a?1:d<a?-1:0};h.prototype.sortDesc=function(d,a){return"string"===typeof d&&"string"===typeof a?a.localeCompare(d):d>a?-1:d<a?1:0};k.finishModule("ActiveBucket");n.exports=h},{"./Path":19,"./Shared":22}],3:[function(h,n,k){var d,a,g,b,f;k=h("./Shared");var e=function(c){this.init.apply(this,
arguments)};e.prototype.init=function(c){this._primaryKey="_id";this._primaryIndex=new a("primary");this._primaryCrc=new a("primaryCrc");this._crcLookup=new a("crcLookup");this._name=c;this._data=[];this._groups=[];this._metrics=new d;this._deferQueue={insert:[],update:[],remove:[],upsert:[]};this._deferThreshold={insert:100,update:100,remove:100,upsert:100};this._deferTime={insert:1,update:1,remove:1,upsert:1};this._subsetOf(this)};k.addModule("Collection",e);k.mixin(e.prototype,"Mixin.Common");
k.mixin(e.prototype,"Mixin.Events");k.mixin(e.prototype,"Mixin.ChainReactor");k.mixin(e.prototype,"Mixin.CRUD");d=h("./Metrics");a=h("./KeyValueStore");g=h("./Path");b=h("./Index");f=h("./Crc");h=k.modules.Core;e.prototype.crc=f;k.synthesize(e.prototype,"name");e.prototype.data=function(){return this._data};e.prototype.drop=function(){if(this._db&&this._db._collection&&this._name){this.debug()&&console.log("Dropping collection "+this._name);this.emit("drop");delete this._db._collection[this._name];
var c=[],m;for(m=0;m<this._groups.length;m++)c.push(this._groups[m]);for(m=0;m<c.length;m++)this._groups[m].removeCollection(this);return!0}return!1};e.prototype.primaryKey=function(c){return void 0!==c?(this._primaryKey!==c&&(this._primaryKey=c,this._primaryIndex.primaryKey(c),this.rebuildPrimaryKeyIndex()),this):this._primaryKey};e.prototype._onInsert=function(c,m){this.emit("insert",c,m)};e.prototype._onUpdate=function(c){this.emit("update",c)};e.prototype._onRemove=function(c){this.emit("remove",
c)};k.synthesize(e.prototype,"db");e.prototype.setData=function(c,m,b){if(c){var a=this._metrics.create("setData");a.start();m=this.options(m);this.preSetData(c,m,b);m.$decouple&&(c=this.decouple(c));c instanceof Array||(c=[c]);a.time("transformIn");c=this.transformIn(c);a.time("transformIn");var e=[].concat(this._data);this._dataReplace(c);a.time("Rebuild Primary Key Index");this.rebuildPrimaryKeyIndex(m);a.time("Rebuild Primary Key Index");a.time("Resolve chains");this.chainSend("setData",c,{oldData:e});
a.time("Resolve chains");a.stop();this.emit("setData",this._data,e)}b&&b(!1);return this};e.prototype.rebuildPrimaryKeyIndex=function(c){var m=c&&void 0!==c.$ensureKeys?c.$ensureKeys:!0;c=c&&void 0!==c.$violationCheck?c.$violationCheck:!0;var b,a,e,g=this._primaryIndex,d=this._primaryCrc,f=this._crcLookup,h=this._primaryKey,k;g.truncate();d.truncate();f.truncate();b=this._data;for(a=b.length;a--;){e=b[a];m&&this.ensurePrimaryKey(e);if(c){if(!g.uniqueSet(e[h],e))throw"Call to setData failed because your data violates the primary key unique constraint. One or more documents are using the same primary key: "+
e[this._primaryKey];}else g.set(e[h],e);k=JSON.stringify(e);d.set(e[h],k);f.set(k,e)}};e.prototype.ensurePrimaryKey=function(c){void 0===c[this._primaryKey]&&(c[this._primaryKey]=this.objectId())};e.prototype.truncate=function(){this.emit("truncate",this._data);this._data.length=0;this.deferEmit("change");return this};e.prototype.upsert=function(c,m){if(c){var b=this._deferQueue.upsert,a=this._deferThreshold.upsert,e={},g;if(c instanceof Array){if(c.length>a)return this._deferQueue.upsert=b.concat(c),
this.processQueue("upsert",m),{};e=[];for(b=0;b<c.length;b++)e.push(this.upsert(c[b]));m&&m();return e}c[this._primaryKey]?(g={},g[this._primaryKey]=c[this._primaryKey],this._primaryIndex.lookup(g)[0]?e.op="update":e.op="insert"):e.op="insert";switch(e.op){case "insert":e.result=this.insert(c);break;case "update":e.result=this.update(g,c)}return e}m&&m();return{}};e.prototype.update=function(c,m,b){m=this.decouple(m);m=this.transformIn(m);this.debug()&&console.log('Updating some collection data for collection "'+
this.name()+'"');var a=this,e=this._metrics.create("update"),g=this._primaryKey,d,f,h=function(e){if(m&&void 0!==m[g]&&m[g]!=e[g]){a._removeIndex(e);var d=a.updateObject(e,m,c,b,"");if(a._insertIndex(e))return d;throw"Primary key violation in update! Key violated: "+e[g];}return a.updateObject(e,m,c,b,"")};e.start();e.time("Retrieve documents to update");d=this.find(c,{$decouple:!1});e.time("Retrieve documents to update");d.length&&(e.time("Update documents"),f=d.filter(h),e.time("Update documents"),
f.length&&(e.time("Resolve chains"),this.chainSend("update",{query:c,update:m,dataSet:d},b),e.time("Resolve chains"),this._onUpdate(f),this.deferEmit("change",{type:"update",data:f})));e.stop();return f||[]};e.prototype.updateById=function(c,m){var b={};b[this._primaryKey]=c;return this.update(b,m)};e.prototype.updateObject=function(c,m,b,a,e,d){m=this.decouple(m);e=e||"";"."===e.substr(0,1)&&(e=e.substr(1,e.length-1));var f=!1,h=!1,k,n,p,l;for(l in m)if(m.hasOwnProperty(l)){k=!1;if("$"===l.substr(0,
1))switch(l){case "$index":k=!0;break;default:k=!0,h=this.updateObject(c,m[l],b,a,e,l),f=f||h}if(this._isPositionalKey(l)&&(k=!0,l=l.substr(0,l.length-2),h=new g(e+"."+l),c[l]&&c[l]instanceof Array&&c[l].length)){n=[];for(p=0;p<c[l].length;p++)this._match(c[l][p],h.value(b)[0])&&n.push(p);for(p=0;p<n.length;p++)h=this.updateObject(c[l][n[p]],m[l+".$"],b,a,e+"."+l,d),f=f||h}if(!k)if(d||"object"!==typeof m[l])switch(d){case "$inc":this._updateIncrement(c,l,m[l]);f=!0;break;case "$push":void 0===c[l]&&
(c[l]=[]);if(c[l]instanceof Array){if(void 0!==m[l].$position&&m[l].$each instanceof Array)for(h=m[l].$position,k=m[l].$each.length,p=0;p<k;p++)this._updateSplicePush(c[l],h+p,m[l].$each[p]);else if(m[l].$each instanceof Array)for(k=m[l].$each.length,p=0;p<k;p++)this._updatePush(c[l],m[l].$each[p]);else this._updatePush(c[l],m[l]);f=!0}else throw"Cannot push to a key that is not an array! ("+l+")";break;case "$pull":if(c[l]instanceof Array){n=[];for(p=0;p<c[l].length;p++)this._match(c[l][p],m[l])&&
n.push(p);for(k=n.length;k--;)this._updatePull(c[l],n[k]),f=!0}break;case "$pullAll":if(c[l]instanceof Array)if(m[l]instanceof Array){if(n=c[l],k=n.length,0<k)for(;k--;){for(h=0;h<m[l].length;h++)n[k]===m[l][h]&&(this._updatePull(c[l],k),k--,f=!0);if(0>k)break}}else throw"Cannot pullAll without being given an array of values to pull! ("+l+")";break;case "$addToSet":void 0===c[l]&&(c[l]=[]);if(c[l]instanceof Array){p=c[l];var r;n=p.length;var t;k=!0;r=a&&a.$addToSet;var s;r&&r.key?(h=!1,s=new g(r.key),
t=s.value(m[l])[0]):(t=JSON.stringify(m[l]),h=!0);for(r=0;r<n;r++)if(h){if(JSON.stringify(p[r])===t){k=!1;break}}else if(t===s.value(p[r])[0]){k=!1;break}k&&(this._updatePush(c[l],m[l]),f=!0)}else throw"Cannot addToSet on a key that is not an array! (undefined)!";break;case "$splicePush":void 0===c[l]&&(c[l]=[]);if(c[l]instanceof Array)if(h=m.$index,void 0!==h)delete m.$index,h>c[l].length&&(h=c[l].length),this._updateSplicePush(c[l],h,m[l]),f=!0;else throw"Cannot splicePush without a $index integer value!";
else throw"Cannot splicePush with a key that is not an array! ("+l+")";break;case "$move":if(c[l]instanceof Array)for(p=0;p<c[l].length;p++){if(this._match(c[l][p],m[l])){f=m.$index;if(void 0!==f)delete m.$index,this._updateSpliceMove(c[l],p,f),f=!0;else throw"Cannot move without a $index integer value!";break}}else throw"Cannot move on a key that is not an array! ("+l+")";break;case "$mul":this._updateMultiply(c,l,m[l]);f=!0;break;case "$rename":this._updateRename(c,l,m[l]);f=!0;break;case "$unset":this._updateUnset(c,
l);f=!0;break;case "$pop":if(c[l]instanceof Array)this._updatePop(c[l],m[l])&&(f=!0);else throw"Cannot pop from a key that is not an array! ("+l+")";break;default:c[l]!==m[l]&&(this._updateProperty(c,l,m[l]),f=!0)}else if(null!==c[l]&&"object"===typeof c[l])if(p=c[l]instanceof Array,n=m[l]instanceof Array,p||n)if(!n&&p)for(p=0;p<c[l].length;p++)h=this.updateObject(c[l][p],m[l],b,a,e+"."+l,d),f=f||h;else c[l]!==m[l]&&(this._updateProperty(c,l,m[l]),f=!0);else h=this.updateObject(c[l],m[l],b,a,e+"."+
l,d),f=f||h;else c[l]!==m[l]&&(this._updateProperty(c,l,m[l]),f=!0)}return f};e.prototype._isPositionalKey=function(c){return".$"===c.substr(c.length-2,2)};e.prototype._updateProperty=function(c,m,b){c[m]=b;this.debug()&&console.log('ForerunnerDB.Collection: Setting non-data-bound document property "'+m+'" for collection "'+this.name()+'"')};e.prototype._updateIncrement=function(c,m,b){c[m]+=b};e.prototype._updateSpliceMove=function(c,m,b){c.splice(b,0,c.splice(m,1)[0]);this.debug()&&console.log('ForerunnerDB.Collection: Moving non-data-bound document array index from "'+
m+'" to "'+b+'" for collection "'+this.name()+'"')};e.prototype._updateSplicePush=function(c,m,b){c.length>m?c.splice(m,0,b):c.push(b)};e.prototype._updatePush=function(c,m){c.push(m)};e.prototype._updatePull=function(c,m){c.splice(m,1)};e.prototype._updateMultiply=function(c,m,b){c[m]*=b};e.prototype._updateRename=function(c,m,b){c[b]=c[m];delete c[m]};e.prototype._updateUnset=function(c,m){delete c[m]};e.prototype._updatePop=function(c,m){var b=!1;0<c.length&&(1===m?(c.pop(),b=!0):-1===m&&(c.shift(),
b=!0));return b};e.prototype.remove=function(c,b,a){var e,f;if(c instanceof Array){f=[];for(a=0;a<c.length;a++)f.push(this.remove(c[a],{noEmit:!0}));(!b||b&&!b.noEmit)&&this._onRemove(f);return f}a=this.find(c,{$decouple:!1});if(a.length){for(f=0;f<a.length;f++)e=a[f],this._removeIndex(e),e=this._data.indexOf(e),this._dataRemoveIndex(e);this.chainSend("remove",{query:c,dataSet:a},b);(!b||b&&!b.noEmit)&&this._onRemove(a);this.deferEmit("change",{type:"remove",data:a})}return a};e.prototype.removeById=
function(c){var b={};b[this._primaryKey]=c;return this.remove(b)};e.prototype.deferEmit=function(){var c=this,b;this._noEmitDefer||this._db&&(!this._db||this._db._noEmitDefer)?this.emit.apply(this,arguments):(b=arguments,this._changeTimeout&&clearTimeout(this._changeTimeout),this._changeTimeout=setTimeout(function(){c.debug()&&console.log("ForerunnerDB.Collection: Emitting "+b[0]);c.emit.apply(c,b)},100))};e.prototype.processQueue=function(c,b){var a=this._deferQueue[c],e=this._deferThreshold[c],
f=this._deferTime[c];if(a.length){var d=this;a.length&&(a=a.length>e?a.splice(0,e):a.splice(0,a.length),this[c](a));setTimeout(function(){d.processQueue(c,b)},f)}else b&&b()};e.prototype.insert=function(c,b,a){"function"===typeof b?(a=b,b=this._data.length):void 0===b&&(b=this._data.length);c=this.transformIn(c);return this._insertHandle(c,b,a)};e.prototype._insertHandle=function(c,b,a){var e=this._deferQueue.insert,f=this._deferThreshold.insert,d=[],g=[];if(c instanceof Array){if(c.length>f){this._deferQueue.insert=
e.concat(c);this.processQueue("insert",a);return}for(f=0;f<c.length;f++)e=this._insert(c[f],b+f),!0===e?d.push(c[f]):g.push({doc:c[f],reason:e})}else e=this._insert(c,b),!0===e?d.push(c):g.push({doc:c,reason:e});this.chainSend("insert",c,{index:b});this._onInsert(d,g);a&&a();this.deferEmit("change",{type:"insert",data:d});return{inserted:d,failed:g}};e.prototype._insert=function(c,b){if(c){var a;this.ensurePrimaryKey(c);if(a=this.insertIndexViolation(c))return"Index violation in index: "+a;this._insertIndex(c);
b>this._data.length&&(b=this._data.length);this._dataInsertIndex(b,c);return!0}return"No document passed to insert"};e.prototype._dataInsertIndex=function(c,b){this._data.splice(c,0,b)};e.prototype._dataRemoveIndex=function(c){this._data.splice(c,1)};e.prototype._dataReplace=function(c){for(;this._data.length;)this._data.pop();this._data=this._data.concat(c)};e.prototype._insertIndex=function(c){var b=this._indexByName,a,e,f=JSON.stringify(c);e=this._primaryIndex.uniqueSet(c[this._primaryKey],c);
this._primaryCrc.uniqueSet(c[this._primaryKey],f);this._crcLookup.uniqueSet(f,c);for(a in b)b.hasOwnProperty(a)&&b[a].insert(c);return e};e.prototype._removeIndex=function(c){var b=this._indexByName,a,e=JSON.stringify(c);this._primaryIndex.unSet(c[this._primaryKey]);this._primaryCrc.unSet(c[this._primaryKey]);this._crcLookup.unSet(e);for(a in b)b.hasOwnProperty(a)&&b[a].remove(c)};e.prototype.subset=function(c,b){var a=this.find(c,b);return(new e)._subsetOf(this).primaryKey(this._primaryKey).setData(a)};
e.prototype.subsetOf=function(){return this.__subsetOf};e.prototype._subsetOf=function(c){this.__subsetOf=c;return this};e.prototype.distinct=function(c,b,a){b=this.find(b,a);c=new g(c);a={};var e=[],f,d;for(d=0;d<b.length;d++)(f=c.value(b[d])[0])&&!a[f]&&(a[f]=!0,e.push(f));return e};e.prototype.findById=function(c,b){var a={};a[this._primaryKey]=c;return this.find(a,b)[0]};e.prototype.peek=function(c,b){var a=this._data,f=a.length,d,g,h=new e;if("string"===typeof c){for(d=0;d<f;d++)g=JSON.stringify(a[d]),
-1<g.indexOf(c)&&h.insert(a[d]);return h.find({},b)}return this.find(c,b)};e.prototype.explain=function(c,b){return this.find(c,b).__fdbOp._data};e.prototype.options=function(c){c=c||{};c.$decouple=void 0!==c.$decouple?c.$decouple:!0;c.$explain=void 0!==c.$explain?c.$explain:!1;return c};e.prototype.find=function(c,b){c=c||{};b=this.options(b);var a=this._metrics.create("find"),e=this,f,d=!0,h,k,n,q,p,l,r,t,s,x=[];k=function(b){return e._match(b,c,"and")};a.start();if(c){a.time("analyseQuery");f=
this._analyseQuery(c,b,a);a.time("analyseQuery");a.data("analysis",f);if(f.hasJoin&&f.queriesJoin){a.time("joinReferences");for(n=0;n<f.joinsOn.length;n++)p=f.joinsOn[n],q=new g(f.joinQueries[p]),q=q.value(c)[0],this._db.collection(f.joinsOn[n]).subset(q);a.time("joinReferences")}f.indexMatch.length&&(!b||b&&!b.$skipIndex)?(a.data("index.potential",f.indexMatch),a.data("index.used",f.indexMatch[0].index),a.time("indexLookup"),h=f.indexMatch[0].lookup,a.time("indexLookup"),f.indexMatch[0].keyData.totalKeyCount===
f.indexMatch[0].keyData.matchedKeyCount&&(d=!1)):a.flag("usedIndex",!1);d&&(h&&h.length?(f=h.length,a.time("tableScan: "+f),h=h.filter(k)):(f=this._data.length,a.time("tableScan: "+f),h=this._data.filter(k)),b.$orderBy&&(a.time("sort"),h=this.sort(b.$orderBy,h),a.time("sort")),a.time("tableScan: "+f));b.limit&&h&&h.length>b.limit&&(h.length=b.limit,a.data("limit",b.limit));b.$decouple&&(a.time("decouple"),h=this.decouple(h),a.time("decouple"),a.data("flag.decouple",!0));if(b.join){for(k=0;k<b.join.length;k++)for(p in b.join[k])if(b.join[k].hasOwnProperty(p))for(t=
p,f=this._db.collection(p),d=b.join[k][p],s=0;s<h.length;s++){r={};q=n=!1;for(l in d)if(d.hasOwnProperty(l))if("$"===l.substr(0,1))switch(l){case "$as":t=d[l];break;case "$multi":n=d[l];break;case "$require":q=d[l];break;default:l.substr(0,3)}else r[l]=(new g(d[l])).value(h[s])[0];r=f.find(r);!q||q&&r[0]?h[s][t]=!1===n?r[0]:r:x.push(h[s])}a.data("flag.join",!0)}if(x.length){a.time("removalQueue");for(l=0;l<x.length;l++)p=h.indexOf(x[l]),-1<p&&h.splice(p,1);a.time("removalQueue")}if(b.transform){a.time("transform");
for(l=0;l<h.length;l++)h.splice(l,1,b.transform(h[l]));a.time("transform");a.data("flag.transform",!0)}this._transformEnabled&&this._transformOut&&(a.time("transformOut"),h=this.transformOut(h),a.time("transformOut"));a.data("results",h.length);a.stop()}else a.stop(),h=[];h.__fdbOp=a;return h};e.prototype.indexOf=function(c){if(c=this.find(c,{$decouple:!1})[0])return this._data.indexOf(c)};e.prototype.transform=function(c){return void 0!==c?("object"===typeof c?(void 0!==c.enabled&&(this._transformEnabled=
c.enabled),void 0!==c.dataIn&&(this._transformIn=c.dataIn),void 0!==c.dataOut&&(this._transformOut=c.dataOut)):this._transformEnabled=!1===c?!1:!0,this):{enabled:this._transformEnabled,dataIn:this._transformIn,dataOut:this._transformOut}};e.prototype.transformIn=function(c){if(this._transformEnabled&&this._transformIn){if(c instanceof Array){var b=[],a;for(a=0;a<c.length;a++)b[a]=this._transformIn(c[a]);return b}return this._transformIn(c)}return c};e.prototype.transformOut=function(c){if(this._transformEnabled&&
this._transformOut){if(c instanceof Array){var b=[],a;for(a=0;a<c.length;a++)b[a]=this._transformOut(c[a]);return b}return this._transformOut(c)}return c};e.prototype.sort=function(c,b){b=b||[];var a=[],e,f;for(e in c)c.hasOwnProperty(e)&&(f={},f[e]=c[e],f.___fdbKey=e,a.push(f));return 2>a.length?this._sort(c,b):this._bucketSort(a,b)};e.prototype._bucketSort=function(c,b){var a=c.shift(),e,f,d=[];if(0<c.length){b=this._sort(a,b);e=this.bucket(a.___fdbKey,b);for(f in e)e.hasOwnProperty(f)&&(a=[].concat(c),
d=d.concat(this._bucketSort(a,e[f])));return d}return this._sort(a,b)};e.prototype._sort=function(c,b){var a,e=new g;a=e.parse(c,!0)[0];e.path(a.path);if(1===a.value)a=function(c,b){var a=e.value(c)[0],f=e.value(b)[0];return"string"===typeof a&&"string"===typeof f?a.localeCompare(f):a>f?1:a<f?-1:0};else if(-1===a.value)a=function(c,b){var a=e.value(c)[0],f=e.value(b)[0];return"string"===typeof a&&"string"===typeof f?f.localeCompare(a):a>f?-1:a<f?1:0};else throw this._name+": $orderBy clause has invalid direction: "+
a.value+", accepted values are 1 or -1 for ascending or descending!";return b.sort(a)};e.prototype.bucket=function(c,b){var a,e={};for(a=0;a<b.length;a++)e[b[a][c]]=e[b[a][c]]||[],e[b[a][c]].push(b[a]);return e};e.prototype._analyseQuery=function(c,a,b){var e={queriesOn:[this._name],indexMatch:[],hasJoin:!1,queriesJoin:!1,joinQueries:{},query:c,options:a},f,d=[],h=[],k,n,q,p,l;b.time("checkIndexes");void 0!==c[this._primaryKey]&&(b.time("checkIndexMatch: Primary Key"),k=new g,e.indexMatch.push({lookup:this._primaryIndex.lookup(c,
a),keyData:{matchedKeys:[this._primaryKey],matchedKeyCount:1,totalKeyCount:k.countKeys(c)},index:this._primaryIndex}),b.time("checkIndexMatch: Primary Key"));for(l in this._indexById)if(this._indexById.hasOwnProperty(l)&&(n=this._indexById[l],q=n.name(),b.time("checkIndexMatch: "+q),k=n.match(c,a),p=n.lookup(c,a),0<k.matchedKeyCount&&e.indexMatch.push({lookup:p,keyData:k,index:n}),b.time("checkIndexMatch: "+q),k.totalKeyCount===k.matchedKeyCount))break;b.time("checkIndexes");1<e.indexMatch.length&&
(b.time("findOptimalIndex"),e.indexMatch.sort(function(c,b){return c.keyData.totalKeyCount===c.keyData.matchedKeyCount?-1:b.keyData.totalKeyCount===b.keyData.matchedKeyCount?1:c.keyData.matchedKeyCount===b.keyData.matchedKeyCount?c.lookup.length-b.lookup.length:b.keyData.matchedKeyCount-c.keyData.matchedKeyCount}),b.time("findOptimalIndex"));if(a.join){e.hasJoin=!0;for(b=0;b<a.join.length;b++)for(f in a.join[b])a.join[b].hasOwnProperty(f)&&(d.push(f),"$as"in a.join[b][f]?h.push(a.join[b][f].$as):
h.push(f));for(f=0;f<h.length;f++)if(a=this._queryReferencesCollection(c,h[f],""))e.joinQueries[d[f]]=a,e.queriesJoin=!0;e.joinsOn=d;e.queriesOn=e.queriesOn.concat(d)}return e};e.prototype._queryReferencesCollection=function(c,b,a){for(var e in c)if(c.hasOwnProperty(e)){if(e===b)return a&&(a+="."),a+e;if("object"===typeof c[e])return a&&(a+="."),a+=e,this._queryReferencesCollection(c[e],b,a)}return!1};e.prototype._match=function(c,b,a){var e,f;e=typeof c;f=typeof b;var d=!0,g;if(!("string"!==e&&"number"!==
e||"string"!==f&&"number"!==f))c!==b&&(d=!1);else for(g in b)if(b.hasOwnProperty(g)){e=!1;if("$"===g.substr(0,1))switch(g){case "$gt":if(c>b[g]){if("or"===a)return!0}else d=!1;e=!0;break;case "$gte":if(c>=b[g]){if("or"===a)return!0}else d=!1;e=!0;break;case "$lt":if(c<b[g]){if("or"===a)return!0}else d=!1;e=!0;break;case "$lte":if(c<=b[g]){if("or"===a)return!0}else d=!1;e=!0;break;case "$exists":if(void 0===c!==b[g]){if("or"===a)return!0}else d=!1;e=!0;break;case "$or":e=!0;for(f=0;f<b[g].length;f++){if(this._match(c,
b[g][f],"and"))return!0;d=!1}break;case "$and":e=!0;for(f=0;f<b[g].length;f++)if(!this._match(c,b[g][f],"and"))return!1;break;case "$in":if(b[g]instanceof Array){e=b[g];f=e.length;var h,k=!1;for(h=0;h<f;h++)if(e[h]===c){k=!0;break}if(k){if("or"===a)return!0}else d=!1}else throw"Cannot use a $nin operator on a non-array key: "+g;e=!0;break;case "$nin":if(b[g]instanceof Array){e=b[g];f=e.length;k=!0;for(h=0;h<f;h++)if(e[h]===c){k=!1;break}if(k){if("or"===a)return!0}else d=!1}else throw"Cannot use a $nin operator on a non-array key: "+
g;e=!0;break;case "$ne":if(c!=b[g]){if("or"===a)return!0}else d=!1;e=!0}if(!e&&b[g]instanceof RegExp)if(e=!0,"object"===typeof c&&void 0!==c[g]&&b[g].test(c[g])){if("or"===a)return!0}else d=!1;if(!e)if("object"===typeof b[g])if(void 0!==c[g]){if(c[g]instanceof Array&&!(b[g]instanceof Array))for(e=!1,f=0;f<c[g].length&&!(e=this._match(c[g][f],b[g],void 0));f++);else if(!(c[g]instanceof Array)&&b[g]instanceof Array)for(e=!1,f=0;f<b[g].length&&!(e=this._match(c[g],b[g][f],void 0));f++);else e="object"===
typeof c?this._match(c[g],b[g],void 0):this._match(void 0,b[g],void 0);if(e){if("or"===a)return!0}else d=!1}else if(b[g]&&void 0!==b[g].$exists)if(e=this._match(void 0,b[g],void 0)){if("or"===a)return!0}else d=!1;else d=!1;else if(c&&c[g]===b[g]){if("or"===a)return!0}else if(c&&c[g]&&c[g]instanceof Array&&b[g]&&"object"!==typeof b[g]){e=!1;for(f=0;f<c[g].length&&!(e=this._match(c[g][f],b[g],void 0));f++);if(e){if("or"===a)return!0}else d=!1}else d=!1;if("and"===a&&!d)return!1}return d};e.prototype.count=
function(c,b){return c?this.find(c,b).length:this._data.length};e.prototype.findSub=function(c,b,a,e){var f=new g(b);c=this.find(c);var d=c.length,h,k,n=this._db.collection("__FDB_temp_"+this.objectId()),q={parents:d,subDocTotal:0,subDocs:[],pathFound:!1,err:""};for(h=0;h<d;h++)if(k=f.value(c[h])[0]){n.setData(k);k=n.find(a,e);if(e.returnFirst&&k.length)return k[0];q.subDocs.push(k);q.subDocTotal+=k.length;q.pathFound=!0}n.drop();if(e.noStats)return q.subDocs;q.pathFound||(q.err="No objects found in the parent documents with a matching path of: "+
b);return q};e.prototype.insertIndexViolation=function(c){var b,a=this._indexByName,e,f;if(this._primaryIndex.get(c[this._primaryKey]))b=this._primaryIndex;else for(e in a)if(a.hasOwnProperty(e)&&(f=a[e],f.unique()&&f.violation(c))){b=f;break}return b?b.name():!1};e.prototype.ensureIndex=function(c,a){this._indexByName=this._indexByName||{};this._indexById=this._indexById||{};var e=new b(c,a,this),f={start:(new Date).getTime()};if(this._indexByName[e.name()])return{err:"Index with that name already exists"};
if(this._indexById[e.id()])return{err:"Index with those keys already exists"};e.rebuild();this._indexByName[e.name()]=e;this._indexById[e.id()]=e;f.end=(new Date).getTime();f.total=f.end-f.start;this._lastOp={type:"ensureIndex",stats:{time:f}};return{index:e,id:e.id(),name:e.name(),state:e.state()}};e.prototype.index=function(c){if(this._indexByName)return this._indexByName[c]};e.prototype.lastOp=function(){return this._metrics.list()};e.prototype.diff=function(c){var b={insert:[],update:[],remove:[]},
a=this.primaryKey(),e,f,d,g;if(a===c.primaryKey()){for(e=c._data;e&&!(e instanceof Array);)c=e,e=c._data;g=e.length;for(f=0;f<g;f++)d=e[f],this._primaryIndex.get(d[a])?this._primaryCrc.get(d[a])!==c._primaryCrc.get(d[a])&&b.update.push(d):b.insert.push(d);e=this._data;g=e.length;for(f=0;f<g;f++)d=e[f],c._primaryIndex.get(d[a])||b.remove.push(d)}else throw"Collection diffing requires that both collections have the same primary key!";return b};h.prototype.collection=function(c,b){if(c)return this._collection[c]||
this.debug()&&console.log("Creating collection "+c),this._collection[c]=this._collection[c]||(new e(c)).db(this),void 0!==b&&this._collection[c].primaryKey(b),this._collection[c];throw"Cannot get collection with undefined name!";};h.prototype.collectionExists=function(c){return Boolean(this._collection[c])};h.prototype.collections=function(){var c=[],b;for(b in this._collection)this._collection.hasOwnProperty(b)&&c.push({name:b,count:this._collection[b].count()});return c};k.finishModule("Collection");
n.exports=e},{"./Crc":6,"./Index":9,"./KeyValueStore":10,"./Metrics":11,"./Path":19,"./Shared":22}],4:[function(h,n,k){var d,a;k=h("./Shared");var g=function(){this.init.apply(this,arguments)};g.prototype.init=function(b){this._name=b;this._data=new a("__FDB__cg_data_"+this._name);this._collections=[];this._views=[]};k.addModule("CollectionGroup",g);k.mixin(g.prototype,"Mixin.Common");k.mixin(g.prototype,"Mixin.ChainReactor");a=h("./Collection");h=k.modules.Core;d=k.modules.Core.prototype.init;g.prototype.on=
function(){this._data.on.apply(this._data,arguments)};g.prototype.off=function(){this._data.off.apply(this._data,arguments)};g.prototype.emit=function(){this._data.emit.apply(this._data,arguments)};g.prototype.primaryKey=function(b){return void 0!==b?(this._primaryKey=b,this):this._primaryKey};k.synthesize(g.prototype,"db");g.prototype.addCollection=function(b){if(b&&-1===this._collections.indexOf(b)){if(this._collections.length){if(this._primaryKey!==b.primaryKey())throw"All collections in a collection group must have the same primary key!";
}else this.primaryKey(b.primaryKey());this._collections.push(b);b._groups.push(this);b.chain(this);this._data.insert(b.find())}return this};g.prototype.removeCollection=function(b){if(b){var a=this._collections.indexOf(b);-1!==a&&(b.unChain(this),this._collections.splice(a,1),a=b._groups.indexOf(this),-1!==a&&b._groups.splice(a,1));0===this._collections.length&&delete this._primaryKey}return this};g.prototype._chainHandler=function(b){switch(b.type){case "setData":b.data=this.decouple(b.data);this._data.remove(b.options.oldData);
this._data.insert(b.data);break;case "insert":b.data=this.decouple(b.data);this._data.insert(b.data);break;case "update":this._data.update(b.data.query,b.data.update,b.options);break;case "remove":this._data.remove(b.data.query,b.options)}};g.prototype.insert=function(){this._collectionsRun("insert",arguments)};g.prototype.update=function(){this._collectionsRun("update",arguments)};g.prototype.updateById=function(){this._collectionsRun("updateById",arguments)};g.prototype.remove=function(){this._collectionsRun("remove",
arguments)};g.prototype._collectionsRun=function(b,a){for(var e=0;e<this._collections.length;e++)this._collections[e][b].apply(this._collections[e],a)};g.prototype.find=function(b,a){return this._data.find(b,a)};g.prototype.removeById=function(b){for(var a=0;a<this._collections.length;a++)this._collections[a].removeById(b)};g.prototype.subset=function(b,f){var e=this.find(b,f);return(new a)._subsetOf(this).primaryKey(this._primaryKey).setData(e)};g.prototype.drop=function(){var b,a=[].concat(this._collections),
e=[].concat(this._views);this._debug&&console.log("Dropping collection group "+this._name);for(b=0;b<a.length;b++)this.removeCollection(a[b]);for(b=0;b<e.length;b++)this._removeView(e[b]);this.emit("drop");return!0};h.prototype.init=function(){this._collectionGroup={};d.apply(this,arguments)};h.prototype.collectionGroup=function(b){return b?(this._collectionGroup[b]=this._collectionGroup[b]||(new g(b)).db(this),this._collectionGroup[b]):this._collectionGroup};n.exports=g},{"./Collection":3,"./Shared":22}],
5:[function(h,n,k){var d,a,g;d=h("./Shared");g=h("./Overload");k=function(){this.init.apply(this,arguments)};k.prototype.init=function(){this._collection={};this._debug={};this._version="1.2.7"};k.prototype.moduleLoaded=g({string:function(b){if(void 0!==b){b=b.replace(/ /g,"");var a=b.split(","),e;for(e=0;e<a.length;e++)if(!d.modules[b])return!1;return!0}return!1},"string, function":function(b,a){if(void 0!==b){b=b.replace(/ /g,"");var e=b.split(","),c;for(c=0;c<e.length;c++)if(!d.modules[b])return!1;
a()}},"string, function, function":function(b,a,e){if(void 0!==b){b=b.replace(/ /g,"");var c=b.split(","),g;for(g=0;g<c.length;g++)if(!d.modules[b])return e(),!1;a()}}});k.moduleLoaded=k.prototype.moduleLoaded;k.shared=d;k.prototype.shared=d;d.addModule("Core",k);d.mixin(k.prototype,"Mixin.Common");d.mixin(k.prototype,"Mixin.ChainReactor");a=h("./Collection.js");h("./Metrics.js");h=h("./Crc.js");k.prototype._isServer=!1;k.prototype.isClient=function(){return!this._isServer};k.prototype.isServer=function(){return this._isServer};
k.prototype.crc=h;k.prototype.isClient=function(){return!this._isServer};k.prototype.isServer=function(){return this._isServer};k.prototype.arrayToCollection=function(b){return(new a).setData(b)};k.prototype.on=function(b,a){this._listeners=this._listeners||{};this._listeners[b]=this._listeners[b]||[];this._listeners[b].push(a);return this};k.prototype.off=function(b,a){if(b in this._listeners){var e=this._listeners[b],c=e.indexOf(a);-1<c&&e.splice(c,1)}return this};k.prototype.emit=function(b,a){this._listeners=
this._listeners||{};if(b in this._listeners){var e=this._listeners[b],c=e.length,d;for(d=0;d<c;d++)e[d].apply(this,Array.prototype.slice.call(arguments,1))}return this};k.prototype.peek=function(b){var a,e,c=[],d=typeof b;for(a in this._collection)this._collection.hasOwnProperty(a)&&(e=this._collection[a],c="string"===d?c.concat(e.peek(b)):c.concat(e.find(b)));return c};k.prototype.peekCat=function(a){var f,e,c={},d,g=typeof a;for(f in this._collection)this._collection.hasOwnProperty(f)&&(e=this._collection[f],
(d="string"===g?e.peek(a):e.find(a))&&d.length&&(c[e.name()]=d));return c};n.exports=k},{"./Collection.js":3,"./Crc.js":6,"./Metrics.js":11,"./Overload":17,"./Shared":22}],6:[function(h,n,k){var d=function(){var a=[],d,b,f;for(b=0;256>b;b++){d=b;for(f=0;8>f;f++)d=d&1?3988292384^d>>>1:d>>>1;a[b]=d}return a}();n.exports=function(a){var g=-1,b;for(b=0;b<a.length;b++)g=g>>>8^d[(g^a.charCodeAt(b))&255];return(g^-1)>>>0}},{}],7:[function(h,n,k){var d,a;k=h("./Shared");var g=function(){this.init.apply(this,
arguments)};g.prototype.init=function(a){this._name=a;this._data={}};k.addModule("Document",g);k.mixin(g.prototype,"Mixin.Common");k.mixin(g.prototype,"Mixin.Events");k.mixin(g.prototype,"Mixin.ChainReactor");h=h("./Collection");d=k.modules.Core;a=k.modules.Core.prototype.init;k.synthesize(g.prototype,"db");k.synthesize(g.prototype,"name");g.prototype.setData=function(a){var f,e;if(a)if(a=this.decouple(a),this._linked){e={};for(f in this._data)"jQuery"!==f.substr(0,6)&&this._data.hasOwnProperty(f)&&
void 0===a[f]&&(e[f]=1);a.$unset=e;this.updateObject(this._data,a,{})}else this._data=a;return this};g.prototype.update=function(a,f,e){this.updateObject(this._data,f,a,e)};g.prototype.updateObject=h.prototype.updateObject;g.prototype._isPositionalKey=function(a){return".$"===a.substr(a.length-2,2)};g.prototype._updateProperty=function(a,f,e){this._linked?(jQuery.observable(a).setProperty(f,e),this.debug()&&console.log('ForerunnerDB.Document: Setting data-bound document property "'+f+'" for collection "'+
this.name()+'"')):(a[f]=e,this.debug()&&console.log('ForerunnerDB.Document: Setting non-data-bound document property "'+f+'" for collection "'+this.name()+'"'))};g.prototype._updateIncrement=function(a,f,e){this._linked?jQuery.observable(a).setProperty(f,a[f]+e):a[f]+=e};g.prototype._updateSpliceMove=function(a,f,e){this._linked?(jQuery.observable(a).move(f,e),this.debug()&&console.log('ForerunnerDB.Document: Moving data-bound document array index from "'+f+'" to "'+e+'" for collection "'+this.name()+
'"')):(a.splice(e,0,a.splice(f,1)[0]),this.debug()&&console.log('ForerunnerDB.Document: Moving non-data-bound document array index from "'+f+'" to "'+e+'" for collection "'+this.name()+'"'))};g.prototype._updateSplicePush=function(a,f,e){a.length>f?this._linked?jQuery.observable(a).insert(f,e):a.splice(f,0,e):this._linked?jQuery.observable(a).insert(e):a.push(e)};g.prototype._updatePush=function(a,f){this._linked?jQuery.observable(a).insert(f):a.push(f)};g.prototype._updatePull=function(a,f){this._linked?
jQuery.observable(a).remove(f):a.splice(f,1)};g.prototype._updateMultiply=function(a,f,e){this._linked?jQuery.observable(a).setProperty(f,a[f]*e):a[f]*=e};g.prototype._updateRename=function(a,f,e){var c=a[f];this._linked?(jQuery.observable(a).setProperty(e,c),jQuery.observable(a).removeProperty(f)):(a[e]=c,delete a[f])};g.prototype._updateUnset=function(a,f){this._linked?jQuery.observable(a).removeProperty(f):delete a[f]};g.prototype._updatePop=function(a,f){var e,c=!1;0<a.length&&(this._linked?(1===
f?e=a.length-1:-1===f&&(e=0),-1<e&&(jQuery.observable(arr).remove(e),c=!0)):1===f?(a.pop(),c=!0):-1===f&&(a.shift(),c=!0));return c};g.prototype.drop=function(){this._db&&this._name&&this._db&&this._db._document&&this._db._document[this._name]&&(delete this._db._document[this._name],this._data={})};d.prototype.init=function(){a.apply(this,arguments)};d.prototype.document=function(a){return a?(this._document=this._document||{},this._document[a]=this._document[a]||(new g(a)).db(this),this._document[a]):
this._document};k.finishModule("Document");n.exports=g},{"./Collection":3,"./Shared":22}],8:[function(h,n,k){var d;h=h("./Shared");var a=function(a,b){this.init.apply(this,arguments)};a.prototype.init=function(a,b){this._options=b;this._selector=$(this._options.selector);this._listeners={};this._collection=a;this._options.series=[];b.chartOptions=b.chartOptions||{};b.chartOptions.credits=!1;var f,e;switch(this._options.type){case "pie":this._selector.highcharts(this._options.chartOptions);this._chart=
this._selector.highcharts();f=this._collection.find();e={allowPointSelect:!0,cursor:"pointer",dataLabels:{enabled:!0,format:"<b>{point.name}</b>: {y} ({point.percentage:.0f}%)",style:{color:Highcharts.theme&&Highcharts.theme.contrastTextColor||"black"}}};f=this.pieDataFromCollectionData(f,this._options.keyField,this._options.valField);$.extend(e,this._options.seriesOptions);$.extend(e,{type:"pie",name:this._options.seriesName,data:f});this._chart.addSeries(e);break;case "line":f=this.lineDataFromCollectionData(this._options.seriesField,
this._options.keyField,this._options.valField,this._options.orderBy),this._options.chartOptions.xAxis=f.xAxis,this._options.chartOptions.series=f.series,this._selector.highcharts(this._options.chartOptions),this._chart=this._selector.highcharts()}this._hookEvents()};h.addModule("Highchart",a);k=h.modules.Collection;d=k.prototype.init;a.prototype.pieDataFromCollectionData=function(a,b,f){var e=[],c;for(c=0;c<a.length;c++)e.push([a[c][b],a[c][f]]);return e};a.prototype.lineDataFromCollectionData=function(a,
b,f,e){var c=this._collection.distinct(a),d=[],h={categories:[]},k,n,z,w,v;for(w=0;w<c.length;w++){k=c[w];n={};n[a]=k;z=[];n=this._collection.find(n,{orderBy:e});for(v=0;v<n.length;v++)h.categories.push(n[v][b]),z.push(n[v][f]);d.push({name:k,data:z})}return{xAxis:h,series:d}};a.prototype._hookEvents=function(){this._collection.on("change",this._changeListener);this._collection.on("drop",this._dropListener)};a.prototype._changeListener=function(){if("undefined"!==typeof this._collection&&this._chart){var a=
this._collection.find();switch(this._options.type){case "pie":this._chart.series[0].setData(this.pieDataFromCollectionData(a,this._options.keyField,this._options.valField));break;case "line":a=this.lineDataFromCollectionData(this._options.seriesField,this._options.keyField,this._options.valField,this._options.orderBy);this._chart.xAxis[0].setCategories(a.xAxis.categories);for(var b=0;b<a.series.length;b++)this._chart.series[b].setData(a.series[b].data)}}};a.prototype._dropListener=function(){this._collection.off("change",
this._changeListener);this._collection.off("drop",this._dropListener)};a.prototype.drop=function(){this._chart.destroy();this._collection.off("change",this._changeListener);this._collection.off("drop",this._dropListener);delete this._collection._highcharts[this._options.selector];delete this._chart;delete this._options;delete this._collection;return this};k.prototype.init=function(){this._highcharts={};d.apply(this,arguments)};k.prototype.chart=function(d){this._highcharts[d.selector]||(this._highcharts[d.selector]=
new a(this,d));return this._highcharts[d.selector]};k.prototype.dropChart=function(a){this._highcharts[a]&&this._highcharts[a].drop()};h.finishModule("Highchart");n.exports=a},{"./Shared":22}],9:[function(h,n,k){k=h("./Shared");var d=h("./Path");h=function(){this.init.apply(this,arguments)};h.prototype.init=function(a,d,b){this._crossRef={};this._size=0;this._id=this._itemKeyHash(a,a);this.data({});this.unique(d&&d.unique?d.unique:!1);void 0!==a&&this.keys(a);void 0!==b&&this.collection(b);this.name(d&&
d.name?d.name:this._id)};k.addModule("Index",h);k.mixin(h.prototype,"Mixin.ChainReactor");h.prototype.id=function(){return this._id};h.prototype.state=function(){return this._state};h.prototype.size=function(){return this._size};h.prototype.data=function(a){return void 0!==a?(this._data=a,this):this._data};k.synthesize(h.prototype,"name");h.prototype.collection=function(a){return void 0!==a?(this._collection=a,this):this._collection};h.prototype.keys=function(a){return void 0!==a?(this._keys=a,this._keyCount=
(new d).parse(this._keys).length,this):this._keys};h.prototype.type=function(a){return void 0!==a?(this._type=a,this):this._type};h.prototype.unique=function(a){return void 0!==a?(this._unique=a,this):this._unique};h.prototype.rebuild=function(){if(this._collection){var a=this._collection.subset({},{$decouple:!1,$orderBy:this._keys}).find(),d,b=a.length;this._data={};this._unique&&(this._uniqueLookup={});for(d=0;d<b;d++)this.insert(a[d])}this._state={name:this._name,keys:this._keys,indexSize:this._size,
built:new Date,updated:new Date,ok:!0}};h.prototype.insert=function(a,d){var b,f;this._unique&&(b=this._itemHash(a,this._keys),this._uniqueLookup[b]=a);b=this._itemHashArr(a,this._keys);for(f=0;f<b.length;f++)this.pushToPathValue(b[f],a)};h.prototype.remove=function(a,d){var b,f;this._unique&&(b=this._itemHash(a,this._keys),delete this._uniqueLookup[b]);b=this._itemHashArr(a,this._keys);for(f=0;f<b.length;f++)this.pullFromPathValue(b[f],a)};h.prototype.violation=function(a){a=this._itemHash(a,this._keys);
return Boolean(this._uniqueLookup[a])};h.prototype.hashViolation=function(a){return Boolean(this._uniqueLookup[a])};h.prototype.pushToPathValue=function(a,d){var b=this._data[a]=this._data[a]||[];-1===b.indexOf(d)&&(b.push(d),this._size++,this.pushToCrossRef(d,b))};h.prototype.pullFromPathValue=function(a,d){var b=this._data[a],f;f=b.indexOf(d);-1<f&&(b.splice(f,1),this._size--,this.pullFromCrossRef(d,b));b.length||delete this._data[a]};h.prototype.pull=function(a){var d=a[this._collection.primaryKey()],
b=this._crossRef[d],f,e=b.length,c;for(f=0;f<e;f++)c=b[f],this._pullFromArray(c,a);this._size--;delete this._crossRef[d]};h.prototype._pullFromArray=function(a,d){for(var b=a.length;b--;)a[b]===d&&a.splice(b,1)};h.prototype.pushToCrossRef=function(a,d){var b=a[this._collection.primaryKey()];this._crossRef[b]=this._crossRef[b]||[];b=this._crossRef[b];-1===b.indexOf(d)&&b.push(d)};h.prototype.pullFromCrossRef=function(a,d){var b=a[this._collection.primaryKey()];delete this._crossRef[b]};h.prototype.lookup=
function(a){return this._data[this._itemHash(a,this._keys)]||[]};h.prototype.match=function(a,g){return(new d).countObjectPaths(this._keys,a)};h.prototype._itemHash=function(a,g){var b=new d,f,e="",c;f=b.parse(g);for(c=0;c<f.length;c++)e&&(e+="_"),e+=b.value(a,f[c].path).join(":");return e};h.prototype._itemKeyHash=function(a,g){var b=new d,f,e="",c;f=b.parse(g);for(c=0;c<f.length;c++)e&&(e+="_"),e+=b.keyValue(a,f[c].path);return e};h.prototype._itemHashArr=function(a,g){var b=new d,f,e=[],c,h,k,
n;f=b.parse(g);for(k=0;k<f.length;k++)for(c=b.value(a,f[k].path),h=0;h<c.length;h++)if(0===k)e.push(c[h]);else for(n=0;n<e.length;n++)e[n]=e[n]+"_"+c[h];return e};k.finishModule("Index");n.exports=h},{"./Path":19,"./Shared":22}],10:[function(h,n,k){h=h("./Shared");k=function(d){this.init.apply(this,arguments)};k.prototype.init=function(d){this._name=d;this._data={};this._primaryKey="_id"};h.addModule("KeyValueStore",k);h.mixin(k.prototype,"Mixin.ChainReactor");h.synthesize(k.prototype,"name");k.prototype.primaryKey=
function(d){return void 0!==d?(this._primaryKey=d,this):this._primaryKey};k.prototype.truncate=function(){this._data={};return this};k.prototype.set=function(d,a){this._data[d]=a?a:!0;return this};k.prototype.get=function(d){return this._data[d]};k.prototype.lookup=function(d){d=d[this._primaryKey];var a,g,b,f;if(d instanceof Array){g=d.length;f=[];for(a=0;a<g;a++)(b=this._data[d[a]])&&f.push(b);return f}if(d instanceof RegExp){f=[];for(a in this._data)this._data.hasOwnProperty(a)&&d.test(a)&&f.push(this._data[a]);
return f}if("object"===typeof d){if(d.$ne){f=[];for(a in this._data)this._data.hasOwnProperty(a)&&a!==d.$ne&&f.push(this._data[a]);return f}if(d.$in&&d.$in instanceof Array){f=[];for(a in this._data)this._data.hasOwnProperty(a)&&-1<d.$in.indexOf(a)&&f.push(this._data[a]);return f}if(d.$nin&&d.$nin instanceof Array){f=[];for(a in this._data)this._data.hasOwnProperty(a)&&-1===d.$nin.indexOf(a)&&f.push(this._data[a]);return f}if(d.$or&&d.$or instanceof Array){f=[];for(a=0;a<d.$or.length;a++)f=f.concat(this.lookup(d.$or[a]));
return f}}else return b=this._data[d],void 0!==b?[b]:[]};k.prototype.unSet=function(d){delete this._data[d];return this};k.prototype.uniqueSet=function(d,a){return void 0===this._data[d]?(this._data[d]=a,!0):!1};h.finishModule("KeyValueStore");n.exports=k},{"./Shared":22}],11:[function(h,n,k){k=h("./Shared");var d=h("./Operation");h=function(){this.init.apply(this,arguments)};h.prototype.init=function(){this._data=[]};k.addModule("Metrics",h);k.mixin(h.prototype,"Mixin.ChainReactor");h.prototype.create=
function(a){a=new d(a);this._enabled&&this._data.push(a);return a};h.prototype.start=function(){this._enabled=!0;return this};h.prototype.stop=function(){this._enabled=!1;return this};h.prototype.clear=function(){this._data=[];return this};h.prototype.list=function(){return this._data};k.finishModule("Metrics");n.exports=h},{"./Operation":16,"./Shared":22}],12:[function(h,n,k){n.exports={preSetData:function(){},postSetData:function(){}}},{}],13:[function(h,n,k){n.exports={chain:function(d){this._chain=
this._chain||[];-1===this._chain.indexOf(d)&&this._chain.push(d)},unChain:function(d){this._chain&&(d=this._chain.indexOf(d),-1<d&&this._chain.splice(d,1))},chainSend:function(d,a,g){if(this._chain){var b=this._chain,f=b.length,e;for(e=0;e<f;e++)b[e].chainReceive(this,d,a,g)}},chainReceive:function(d,a,g,b){d={sender:d,type:a,data:g,options:b};(!this._chainHandler||this._chainHandler&&!this._chainHandler(d))&&this.chainSend(d.type,d.data,d.options)}}},{}],14:[function(h,n,k){var d=0;h={decouple:function(a){if(void 0!==
a)return JSON.parse(JSON.stringify(a))},objectId:function(a){var g=Math.pow(10,17);if(a){var b=0,f=a.length,e;for(e=0;e<f;e++)b+=a.charCodeAt(e)*g;a=b.toString(16)}else d++,a=(d+(Math.random()*g+Math.random()*g+Math.random()*g+Math.random()*g)).toString(16);return a},debug:h("./Overload")([function(){return this._debug&&this._debug.all},function(a){return void 0!==a?"boolean"===typeof a?(this._debug=this._debug||{},this._debug.all=a,this.chainSend("debug",this._debug),this):this._debug&&this._debug[a]||
this._db&&this._db._debug&&this._db._debug[a]||this._debug&&this._debug.all:this._debug&&this._debug.all},function(a,d){return void 0!==a?void 0!==d?(this._debug=this._debug||{},this._debug[a]=d,this.chainSend("debug",this._debug),this):this._debug&&this._debug[d]||this._db&&this._db._debug&&this._db._debug[a]:this._debug&&this._debug.all}])};n.exports=h},{"./Overload":17}],15:[function(h,n,k){h={on:new Overload({"string, function":function(d,a){this._listeners=this._listeners||{};this._listeners[d]=
this._listeners[d]||{};this._listeners[d]["*"]=this._listeners[d]["*"]||[];this._listeners[d]["*"].push(a);return this},"string, *, function":function(d,a,g){this._listeners=this._listeners||{};this._listeners[d]=this._listeners[d]||{};this._listeners[d][a]=this._listeners[d][a]||[];this._listeners[d][a].push(g);return this}}),off:new Overload({string:function(d){this._listeners&&this._listeners[d]&&d in this._listeners&&delete this._listeners[d];return this},"string, function":function(d,a){var g,
b;"string"===typeof a?this._listeners&&this._listeners[d]&&this._listeners[d][a]&&delete this._listeners[d][a]:d in this._listeners&&(g=this._listeners[d]["*"],b=g.indexOf(a),-1<b&&g.splice(b,1));return this},"string, *, function":function(d,a,g){this._listeners&&d in this._listeners&&a in this.listeners[d]&&(d=this._listeners[d][a],g=d.indexOf(g),-1<g&&d.splice(g,1))},"string, *":function(d,a){this._listeners&&d in this._listeners&&a in this._listeners[d]&&delete this._listeners[d][a]}}),emit:function(d,
a){this._listeners=this._listeners||{};if(d in this._listeners){if(this._listeners[d]["*"]){var g=this._listeners[d]["*"],b=g.length,f;for(f=0;f<b;f++)g[f].apply(this,Array.prototype.slice.call(arguments,1))}if(a instanceof Array&&a[0]&&a[0][this._primaryKey]){var g=this._listeners[d],e,c,b=a.length;for(f=0;f<b;f++)if(g[a[f][this._primaryKey]])for(e=g[a[f][this._primaryKey]].length,c=0;c<e;c++)g[a[f][this._primaryKey]][c].apply(this,Array.prototype.slice.call(arguments,1))}}return this}};n.exports=
h},{}],16:[function(h,n,k){k=h("./Shared");var d=h("./Path");h=function(a){this.pathSolver=new d;this.counter=0;this.init.apply(this,arguments)};h.prototype.init=function(a){this._data={operation:a,index:{potential:[],used:!1},steps:[],time:{startMs:0,stopMs:0,totalMs:0,process:{}},flag:{},log:[]}};k.addModule("Operation",h);k.mixin(h.prototype,"Mixin.ChainReactor");h.prototype.start=function(){this._data.time.startMs=(new Date).getTime()};h.prototype.log=function(a){if(a){var d=0<this._log.length?
this._data.log[this._data.log.length-1].time:0;a={event:a,time:(new Date).getTime(),delta:0};this._data.log.push(a);d&&(a.delta=a.time-d);return this}return this._data.log};h.prototype.time=function(a){if(void 0!==a){var d=this._data.time.process,d=d[a]=d[a]||{};d.startMs?(d.stopMs=(new Date).getTime(),d.totalMs=d.stopMs-d.startMs,d.stepObj.totalMs=d.totalMs,delete d.stepObj):(d.startMs=(new Date).getTime(),d.stepObj={name:a},this._data.steps.push(d.stepObj));return this}return this._data.time};h.prototype.flag=
function(a,d){if(void 0!==a&&void 0!==d)this._data.flag[a]=d;else return void 0!==a?this._data.flag[a]:this._data.flag};h.prototype.data=function(a,d,b){return void 0!==d?(this.pathSolver.set(this._data,a,d),this):this.pathSolver.get(this._data,a)};h.prototype.pushData=function(a,d,b){this.pathSolver.push(this._data,a,d)};h.prototype.stop=function(){this._data.time.stopMs=(new Date).getTime();this._data.time.totalMs=this._data.time.stopMs-this._data.time.startMs};k.finishModule("Operation");n.exports=
h},{"./Path":19,"./Shared":22}],17:[function(h,n,k){Overload=function(d){if(d){var a,g,b,f,e;if(!(d instanceof Array)){b={};for(a in d)if(d.hasOwnProperty(a))if(f=a.replace(/ /g,""),-1===f.indexOf("*"))b[f]=d[a];else for(e=generateSignaturePermutations(f),f=0;f<e.length;f++)b[e[f]]||(b[e[f]]=d[a]);d=b}return function(){if(d instanceof Array)for(g=d.length,a=0;a<g;a++){if(d[a].length===arguments.length)return d[a].apply(this,arguments)}else{var c=[],b;for(a=0;a<arguments.length;a++)b=typeof arguments[a],
"object"===b&&arguments[a]instanceof Array&&(b="array"),c.push(b);b=c.join(",");if(d[b])return d[b].apply(this,arguments);for(a=c.length;0<=a;a--)if(b=c.slice(0,a).join(","),d[b+",..."])return d[b+",..."].apply(this,arguments)}throw"Overloaded method does not have a matching signature for the passed arguments: "+JSON.stringify(c);}}return function(){}};generateSignaturePermutations=function(d){var a=[],g,b=["string","object","number","function","undefined"],f;if(-1<d.indexOf("*"))for(f=0;f<b.length;f++)g=
d.replace("*",b[f]),a=a.concat(generateSignaturePermutations(g));else a.push(d);return a};n.exports=Overload},{}],18:[function(h,n,k){var d,a,g;k=h("./Shared");var b=function(){this.init.apply(this,arguments)};b.prototype.init=function(b){this._name=b;this._data=new g("__FDB__dc_data_"+this._name);this._collData=new a;this._collections=[]};k.addModule("Overview",b);k.mixin(b.prototype,"Mixin.Common");k.mixin(b.prototype,"Mixin.ChainReactor");a=h("./Collection");g=h("./Document");h=k.modules.Core;
d=k.modules.Core.prototype.init;k.synthesize(b.prototype,"db");k.synthesize(b.prototype,"name");k.synthesize(b.prototype,"query",function(a){var b=this.$super(a);void 0!==a&&this._refresh();return b});k.synthesize(b.prototype,"queryOptions",function(a){var b=this.$super(a);void 0!==a&&this._refresh();return b});k.synthesize(b.prototype,"reduce",function(a){var b=this.$super(a);void 0!==a&&this._refresh();return b});b.prototype.from=function(a){void 0!==a&&("string"===typeof a&&(a=this._db.collection(a)),
this._addCollection(a));return this};b.prototype.find=function(){return this._collData.find.apply(this._collData,arguments)};b.prototype.count=function(){return this._collData.count.apply(this._collData,arguments)};b.prototype._addCollection=function(a){-1===this._collections.indexOf(a)&&(this._collections.push(a),a.chain(this),this._refresh());return this};b.prototype._removeCollection=function(a){-1<this._collections.indexOf(a)&&(this._collections.splice(a,1),a.unChain(this),this._refresh());return this};
b.prototype._refresh=function(){if(this._collections&&this._collections[0]){this._collData.primaryKey(this._collections[0].primaryKey());var a=[],b;for(b=0;b<this._collections.length;b++)a=a.concat(this._collections[b].find(this._query,this._queryOptions));this._collData.setData(a)}this._reduce&&(a=this._reduce(),this._data.setData(a))};b.prototype._chainHandler=function(a){switch(a.type){case "setData":case "insert":case "update":case "remove":this._refresh()}};b.prototype.data=function(){return this._data};
h.prototype.init=function(){this._overview={};d.apply(this,arguments)};h.prototype.overview=function(a){return a?(this._overview[a]=this._overview[a]||(new b(a)).db(this),this._overview[a]):this._overview};k.finishModule("Overview");n.exports=b},{"./Collection":3,"./Document":7,"./Shared":22}],19:[function(h,n,k){h=h("./Shared");k=function(d){this.init.apply(this,arguments)};k.prototype.init=function(d){d&&this.path(d)};h.addModule("Path",k);h.mixin(k.prototype,"Mixin.ChainReactor");k.prototype.path=
function(d){return void 0!==d?(this._path=this.clean(d),this._pathParts=this._path.split("."),this):this._path};k.prototype.hasObjectPaths=function(d,a){var g=!0,b;for(b in d)if(d.hasOwnProperty(b)&&(void 0===a[b]||"object"===typeof d[b]&&(g=this.hasObjectPaths(d[b],a[b]),!g)))return!1;return g};k.prototype.countKeys=function(d){var a=0,g;for(g in d)d.hasOwnProperty(g)&&void 0!==d[g]&&("object"!==typeof d[g]?a++:a+=this.countKeys(d[g]));return a};k.prototype.countObjectPaths=function(d,a){var g,b=
{},f=0,e=0,c;for(c in a)a.hasOwnProperty(c)&&("object"===typeof a[c]?(g=this.countObjectPaths(d[c],a[c]),b[c]=g.matchedKeys,e+=g.totalKeyCount,f+=g.matchedKeyCount):(e++,d&&d[c]&&"object"!==typeof d[c]?(b[c]=!0,f++):b[c]=!1));return{matchedKeys:b,matchedKeyCount:f,totalKeyCount:e}};k.prototype.parse=function(d,a){var g=[],b="",f,e,c;for(e in d)if(d.hasOwnProperty(e))if(b=e,"object"===typeof d[e])if(a)for(f=this.parse(d[e],a),c=0;c<f.length;c++)g.push({path:b+"."+f[c].path,value:f[c].value});else for(f=
this.parse(d[e]),c=0;c<f.length;c++)g.push({path:b+"."+f[c].path});else a?g.push({path:b,value:d[e]}):g.push({path:b});return g};k.prototype.parseArr=function(d,a){a=a||{};return this._parseArr(d,"",[],a)};k.prototype._parseArr=function(d,a,g,b){var f,e="";a=a||"";g=g||[];for(f in d)d.hasOwnProperty(f)&&(!b.ignore||b.ignore&&!b.ignore.test(f))&&(e=a?a+"."+f:f,"object"===typeof d[f]?this._parseArr(d[f],e,g,b):g.push(e));return g};k.prototype.value=function(d,a){if(void 0!==d&&"object"===typeof d){var g,
b,f,e,c=[],h;void 0!==a&&(a=this.clean(a),g=a.split("."));g=g||this._pathParts;b=g.length;f=d;for(h=0;h<b;h++){f=f[g[h]];if(e instanceof Array){for(b=0;b<e.length;b++)c=c.concat(this.value(e,b+"."+g[h]));return c}if(!f||"object"!==typeof f)break;e=f}return[f]}return[]};k.prototype.set=function(d,a,g){if(void 0!==d&&void 0!==a){var b;a=this.clean(a);a=a.split(".");b=a.shift();a.length?(d[b]=d[b]||{},this.set(d[b],a.join("."),g)):d[b]=g}return d};k.prototype.get=function(d,a){return this.value(d,a)[0]};
k.prototype.push=function(d,a,g){if(void 0!==d&&void 0!==a){var b;a=this.clean(a);a=a.split(".");b=a.shift();if(a.length)d[b]=d[b]||{},this.set(d[b],a.join("."),g);else if(d[b]=d[b]||[],d[b]instanceof Array)d[b].push(g);else throw"Cannot push to a path whose endpoint is not an array!";}return d};k.prototype.keyValue=function(d,a){var g,b,f,e,c;void 0!==a&&(a=this.clean(a),g=a.split("."));g=g||this._pathParts;b=g.length;f=d;for(c=0;c<b;c++)if(f=f[g[c]],!f||"object"!==typeof f){e=g[c]+":"+f;break}return e};
k.prototype.clean=function(d){"."===d.substr(0,1)&&(d=d.substr(1,d.length-1));return d};h.finishModule("Path");n.exports=k},{"./Shared":22}],20:[function(h,n,k){k=h("./Shared");var d,a,g,b,f;f=function(){this.init.apply(this,arguments)};f.prototype.init=function(a){a.isClient()&&void 0!==Storage&&this.mode("localStorage")};k.addModule("Persist",f);k.mixin(f.prototype,"Mixin.ChainReactor");d=k.modules.Core;a=h("./Collection");g=a.prototype.drop;h("./CollectionGroup");b=d.prototype.init;f.prototype.mode=
function(a){return void 0!==a?(this._mode=a,this):this._mode};f.prototype.save=function(a,c,b){switch(this.mode()){case "localStorage":c="object"===typeof c?"json::fdb::"+JSON.stringify(c):"raw::fdb::"+c;try{localStorage.setItem(a,c)}catch(d){if(b)b(d);else throw d;break}if(b)b(!1);else return!1;break;default:if(b)b("No data handler.");else throw"No data handler.";}};f.prototype.load=function(a,c){var b,d;switch(this.mode()){case "localStorage":try{b=localStorage.getItem(a)}catch(f){c(f,null)}if(b){b=
b.split("::fdb::");switch(b[0]){case "json":d=JSON.parse(b[1]);break;case "raw":d=b[1]}if(c)c(!1,d);else return d}break;default:if(c)c("No data handler or unrecognised data type.");else throw"No data handler or unrecognised data type.";}};f.prototype.drop=function(a,c){switch(this.mode()){case "localStorage":try{localStorage.removeItem(a)}catch(b){c&&c(b)}c&&c(!1);break;default:c&&c("No data handler or unrecognised data type.")}};a.prototype.drop=function(a){if(a)if(this._name)if(this._db)this._db.persist.drop(this._name);
else return callback&&callback("Cannot drop a collection's persistent storage when the collection is not attached to a database!"),"Cannot drop a collection's persistent storage when the collection is not attached to a database!";else return callback&&callback("Cannot drop a collection's persistent storage when no name assigned to collection!"),"Cannot drop a collection's persistent storage when no name assigned to collection!";g.apply(this)};a.prototype.save=function(a){if(this._name)if(this._db)this._db.persist.save(this._name,
this._data,a);else return a&&a("Cannot save a collection that is not attached to a database!"),"Cannot save a collection that is not attached to a database!";else return a&&a("Cannot save a collection with no assigned name!"),"Cannot save a collection with no assigned name!"};a.prototype.load=function(a){var c=this;if(this._name)if(this._db)this._db.persist.load(this._name,function(b,d){if(b)return a&&a(b),b;d&&c.setData(d);a&&a(!1)});else return a&&a("Cannot load a collection that is not attached to a database!"),
"Cannot load a collection that is not attached to a database!";else return a&&a("Cannot load a collection with no assigned name!"),"Cannot load a collection with no assigned name!"};d.prototype.init=function(){this.persist=new f(this);b.apply(this,arguments)};d.prototype.load=function(a){var c=this._collection,b=c.keys().length,d;for(d in c)c.hasOwnProperty(d)&&c[d].load(function(c){c?a(c):(b--,0===b&&a(!1))})};d.prototype.save=function(a){var c=this._collection,b=c.keys().length,d;for(d in c)c.hasOwnProperty(d)&&
c[d].save(function(c){c?a(c):(b--,0===b&&a(!1))})};k.finishModule("Persist");n.exports=f},{"./Collection":3,"./CollectionGroup":4,"./Shared":22}],21:[function(h,n,k){h=h("./Shared");k=function(d,a,g){if(d&&a&&g){this._reactorIn=d;this._reactorOut=a;this._chainHandler=g;if(!d.chain||!a.chainReceive)throw"ReactorIO requires passed in and out objects to implement the ChainReactor mixin!";d.chain(this);this.chain(a)}else throw"ReactorIO requires an in, out and process argument to instantiate!";};h.addModule("ReactorIO",
k);k.prototype.drop=function(){this._reactorIn.unChain(this);this.unChain(this._reactorOut);delete this._reactorIn;delete this._reactorOut;delete this._chainHandler};h.mixin(k.prototype,"Mixin.ChainReactor");h.finishModule("ReactorIO");n.exports=k},{"./Shared":22}],22:[function(h,n,k){h={modules:{},_synth:{},addModule:function(d,a){this.modules[d]=a;this.emit("moduleLoad",[d,a])},finishModule:function(d){if(this.modules[d])this.modules[d]._fdbFinished=!0,this.emit("moduleFinished",[d,this.modules[d]]);
else throw"finishModule called on a module that has not been registered with addModule(): "+d;},moduleFinished:function(d,a){if(this.modules[d]&&this.modules[d]._fdbFinished)a(d,this.modules[d]);else this.on("moduleFinished",a)},mixin:function(d,a){var g=this.mixins[a];if(g)for(var b in g)g.hasOwnProperty(b)&&(d[b]=g[b]);else throw"Cannot find mixin named: "+a;},synthesize:function(d,a,g){this._synth[a]=this._synth[a]||function(b){return void 0!==b?(this["_"+a]=b,this):this["_"+a]};if(g){var b=this;
d[a]=function(){var d=this.$super,e;this.$super=b._synth[a];e=g.apply(this,arguments);this.$super=d;return e}}else d[a]=this._synth[a]},overload:h("./Overload"),mixins:{"Mixin.Common":h("./Mixin.Common"),"Mixin.Events":h("./Mixin.Events"),"Mixin.ChainReactor":h("./Mixin.ChainReactor"),"Mixin.CRUD":h("./Mixin.CRUD")}};h.mixin(h,"Mixin.Events");n.exports=h},{"./Mixin.CRUD":12,"./Mixin.ChainReactor":13,"./Mixin.Common":14,"./Mixin.Events":15,"./Overload":17}],23:[function(h,n,k){var d,a,g,b,f;k=h("./Shared");
var e=function(a,b,d){this.init.apply(this,arguments)};e.prototype.init=function(a,b,e){this._name=a;this._groups=[];this._listeners={};this._querySettings={};this._debug={};this.query(b,!1);this.queryOptions(e,!1);this._privateData=new d("__FDB__view_privateData_"+this._name)};k.addModule("View",e);k.mixin(e.prototype,"Mixin.Common");k.mixin(e.prototype,"Mixin.ChainReactor");d=h("./Collection");CollectionGroup=h("./CollectionGroup");f=h("./ActiveBucket");b=h("./ReactorIO");a=d.prototype.init;h=k.modules.Core;
g=h.prototype.init;k.synthesize(e.prototype,"name");e.prototype.insert=function(){this._from.insert.apply(this._from,arguments)};e.prototype.update=function(){this._from.update.apply(this._from,arguments)};e.prototype.updateById=function(){this._from.updateById.apply(this._from,arguments)};e.prototype.remove=function(){this._from.remove.apply(this._from,arguments)};e.prototype.find=function(a,b){return this.publicData().find(a,b)};e.prototype.data=function(){return this._privateData};e.prototype.from=
function(a){var d=this;if(void 0!==a){"string"===typeof a&&(a=this._db.collection(a));this._from=a;this._io=new b(a,this,function(a){var c,b,e;if(d._querySettings.query){if("insert"===a.type){c=a.data;if(c instanceof Array)for(b=[],a=0;a<c.length;a++)d._privateData._match(c[a],d._querySettings.query,"and")&&(b.push(c[a]),e=!0);else d._privateData._match(c,d._querySettings.query,"and")&&(b=c,e=!0);e&&this.chainSend("insert",b);return!0}if("update"===a.type&&(b=d._privateData.diff(d._from.subset(d._querySettings.query,
d._querySettings.options)),b.insert.length||b.remove.length)){b.insert.length&&this.chainSend("insert",b.insert);if(b.update.length)for(c=d._privateData.primaryKey(),a=0;a<b.update.length;a++)e={},e[c]=b.update[a][c],this.chainSend("update",{query:e,update:b.update[a]});if(b.remove.length){e=[];var f={$or:e};for(a=0;a<b.remove.length;a++)e.push({_id:b.remove[a][c]});this.chainSend("remove",f)}return!0}}return!1});var e=a.find(this._querySettings.query,this._querySettings.options);this._transformPrimaryKey(a.primaryKey());
this._transformSetData(e);this._privateData.primaryKey(a.primaryKey());this._privateData.setData(e)}return this};e.prototype.ensureIndex=function(){return this._privateData.ensureIndex.apply(this._privateData,arguments)};e.prototype._chainHandler=function(a){var b,d,e,f,g;switch(a.type){case "setData":this.debug()&&console.log('ForerunnerDB.View: Setting data on view "'+this.name()+'" in underlying (internal) view collection "'+this._privateData.name()+'"');a=this._from.find(this._querySettings.query,
this._querySettings.options);this._transformSetData(a);this._privateData.setData(a);break;case "insert":this.debug()&&console.log('ForerunnerDB.View: Inserting some data on view "'+this.name()+'" in underlying (internal) view collection "'+this._privateData.name()+'"');a.data=this.decouple(a.data);a.data instanceof Array||(a.data=[a.data]);if(this._querySettings.options&&this._querySettings.options.$orderBy)for(b=a.data,d=b.length,e=0;e<d;e++)f=this._activeBucket.insert(b[e]),this._transformInsert(a.data,
f),this._privateData._insertHandle(a.data,f);else f=this._privateData._data.length,this._transformInsert(a.data,f),this._privateData._insertHandle(a.data,f);break;case "update":this.debug()&&console.log('ForerunnerDB.View: Updating some data on view "'+this.name()+'" in underlying (internal) view collection "'+this._privateData.name()+'"');this._privateData.primaryKey();a=this._privateData.update(a.data.query,a.data.update,a.data.options);if(this._querySettings.options&&this._querySettings.options.$orderBy)for(d=
a.length,e=0;e<d;e++)b=a[e],f=this._activeBucket.index(b),g=this._privateData._data.indexOf(b),f!==g&&(this._activeBucket.remove(b),f=this._activeBucket.insert(b),this._privateData._updateSpliceMove(this._privateData._data,g,f));if(this._transformEnabled&&this._transformIn)for(d=this._publicData.primaryKey(),f=0;f<a.length;f++)e={},b=a[f],e[d]=b[d],this._transformUpdate(e,b);break;case "remove":this.debug()&&console.log('ForerunnerDB.View: Removing some data on view "'+this.name()+'" in underlying (internal) view collection "'+
this._privateData.name()+'"'),this._transformRemove(a.data.query,a.options),this._privateData.remove(a.data.query,a.options)}};e.prototype.on=function(){this._privateData.on.apply(this._privateData,arguments)};e.prototype.off=function(){this._privateData.off.apply(this._privateData,arguments)};e.prototype.emit=function(){this._privateData.emit.apply(this._privateData,arguments)};e.prototype.drop=function(){return this._from?((this.debug()||this._db&&this._db.debug())&&console.log("ForerunnerDB.View: Dropping view "+
this._name),this._io.drop(),this._privateData.drop(),!0):!1};e.prototype.db=function(a){return void 0!==a?(this._db=a,this.privateData().db(a),this.publicData().db(a),this):this._db};e.prototype.primaryKey=function(){return this._privateData.primaryKey()};e.prototype.queryData=function(a,b,d){void 0!==a&&(this._querySettings.query=a);void 0!==b&&(this._querySettings.options=b);return void 0!==a||void 0!==b?(void 0!==d&&!0!==d||this.refresh(),this):this._querySettings};e.prototype.queryAdd=function(a,
b,d){var e=this._querySettings.query,f;if(void 0!==a)for(f in a)a.hasOwnProperty(f)&&(void 0===e[f]||void 0!==e[f]&&b)&&(e[f]=a[f]);void 0!==d&&!0!==d||this.refresh()};e.prototype.queryRemove=function(a,b){var d=this._querySettings.query,e;if(void 0!==a)for(e in a)a.hasOwnProperty(e)&&delete d[e];void 0!==b&&!0!==b||this.refresh()};e.prototype.query=function(a,b){return void 0!==a?(this._querySettings.query=a,void 0!==b&&!0!==b||this.refresh(),this):this._querySettings.query};e.prototype.queryOptions=
function(a,b){return void 0!==a?(this._querySettings.options=a,void 0===a.$decouple&&(a.$decouple=!0),void 0===b||!0===b?this.refresh():this.rebuildActiveBucket(a.$orderBy),this):this._querySettings.options};e.prototype.rebuildActiveBucket=function(a){if(a){var b=this._privateData._data,d=b.length;this._activeBucket=new f(a);this._activeBucket.primaryKey(this._privateData.primaryKey());for(a=0;a<d;a++)this._activeBucket.insert(b[a])}else delete this._activeBucket};e.prototype.refresh=function(){if(this._from){var a=
this.publicData();this._privateData.remove();a.remove();this._privateData.insert(this._from.find(this._querySettings.query,this._querySettings.options));this._querySettings.options&&this._querySettings.options.$orderBy?this.rebuildActiveBucket(this._querySettings.options.$orderBy):this.rebuildActiveBucket()}return this};e.prototype.count=function(){return this._privateData&&this._privateData._data?this._privateData._data.length:0};e.prototype.transform=function(a){return void 0!==a?("object"===typeof a?
(void 0!==a.enabled&&(this._transformEnabled=a.enabled),void 0!==a.dataIn&&(this._transformIn=a.dataIn),void 0!==a.dataOut&&(this._transformOut=a.dataOut)):this._transformEnabled=!1===a?!1:!0,this._transformPrimaryKey(this.privateData().primaryKey()),this._transformSetData(this.privateData().find()),this):{enabled:this._transformEnabled,dataIn:this._transformIn,dataOut:this._transformOut}};e.prototype.privateData=function(){return this._privateData};e.prototype.publicData=function(){return this._transformEnabled?
this._publicData:this._privateData};e.prototype._transformSetData=function(a){this._transformEnabled&&(this._publicData=new d("__FDB__view_publicData_"+this._name),this._publicData.db(this._privateData._db),this._publicData.transform({enabled:!0,dataIn:this._transformIn,dataOut:this._transformOut}),this._publicData.setData(a))};e.prototype._transformInsert=function(a,b){this._transformEnabled&&this._publicData&&this._publicData.insert(a,b)};e.prototype._transformUpdate=function(a,b,d){this._transformEnabled&&
this._publicData&&this._publicData.update(a,b,d)};e.prototype._transformRemove=function(a,b){this._transformEnabled&&this._publicData&&this._publicData.remove(a,b)};e.prototype._transformPrimaryKey=function(a){this._transformEnabled&&this._publicData&&this._publicData.primaryKey(a)};d.prototype.init=function(){this._views=[];a.apply(this,arguments)};d.prototype.view=function(a,b,d){if(this._db&&this._db._views){if(this._db._views[a])throw"Cannot create a view using this collection because one with this name already exists: "+
a;a=(new e(a,b,d)).db(this._db)._addCollection(this);this._views=this._views||[];this._views.push(a);return a}};d.prototype._addView=CollectionGroup.prototype._addView=function(a){void 0!==a&&this._views.push(a);return this};d.prototype._removeView=CollectionGroup.prototype._removeView=function(a){void 0!==a&&(a=this._views.indexOf(a),-1<a&&this._views.splice(a,1));return this};h.prototype.init=function(){this._views={};g.apply(this,arguments)};h.prototype.view=function(a){this._views[a]||(this.debug()||
this._db&&this._db.debug())&&console.log("Core.View: Creating view "+a);this._views[a]=this._views[a]||(new e(a)).db(this);return this._views[a]};h.prototype.viewExists=function(a){return Boolean(this._views[a])};h.prototype.views=function(){var a=[],b;for(b in this._views)this._views.hasOwnProperty(b)&&a.push({name:b,count:this._views[b].count()});return a};k.finishModule("View");n.exports=e},{"./ActiveBucket":2,"./Collection":3,"./CollectionGroup":4,"./ReactorIO":21,"./Shared":22}]},{},[1])(1)});
