UNPKG

lokijs

Version:

A document oriented javascript in-memory database

1 lines 15.2 kB
var loki=function(){"use strict";function t(t,i){var e,s=i||"parse-stringify";return"parse-stringify"===s&&(e=JSON.parse(JSON.stringify(t))),e}function i(t){this.filename=t||"loki.db",this.collections=[];var i=function(){return"undefined"!=typeof module&&module.exports?"NODEJS":void 0!==document?-1===document.URL.indexOf("http://")&&-1===document.URL.indexOf("https://")?"CORDOVA":"BROWSER":"CORDOVA"};this.ENV=i(),"NODEJS"===this.ENV&&(this.fs=require("fs"))}function e(t,i,e){return this.collection=t,this.searchIsChained=!i&&!e,this.filteredrows=[],this.filterInitialized=!1,null!=i?this.find(i):null!=e?this.where(e):this}function s(t,i,s){this.collection=t,this.name=i,this.persistent=!1,"undefined"!=typeof s&&(this.persistent=s),this.resultset=new e(t),this.resultdata=[],this.resultsdirty=!1,this.cachedresultset=null,this.filterPipeline=[],this.sortFunction=null,this.sortColumn=null,this.sortColumnDesc=!1,this.sortDirty=!1}function n(t,i,e,s){this.name=t,this.data=[],this.indices={},this.idIndex={},this.objType=i||"",this.transactional=s||!1,this.cachedIndex=null,this.cachedData=null,this.maxId=0,this.Views={},this.DynamicViews=[];for(var n=e||["id"],r=n.length;r--;)this.ensureIndex(n[r]);this.ensureIndex("id")}return e.prototype.toJSON=function(){var t=this.copy();return t.collection=null,t},e.prototype.limit=function(t){this.searchIsChained&&!this.filterInitialized&&0==this.filteredrows.length&&(this.filteredrows=Object.keys(this.collection.data));var i=this.copy();return i.filteredrows=i.filteredrows.slice(0,t),i},e.prototype.offset=function(t){this.searchIsChained&&!this.filterInitialized&&0==this.filteredrows.length&&(this.filteredrows=Object.keys(this.collection.data));var i=this.copy();return i.filteredrows=i.filteredrows.splice(t),i},e.prototype.copy=function(){var t=new e(this.collection,null,null);return t.filteredrows=this.filteredrows.slice(),t.filterInitialized=this.filterInitialized,t},e.prototype.sort=function(t){this.searchIsChained&&!this.filterInitialized&&0==this.filteredrows.length&&(this.filteredrows=Object.keys(this.collection.data));var i=function(t,i){return function(e,s){var n=i.collection.data[e],r=i.collection.data[s];return t(n,r)}}(t,this);return this.filteredrows.sort(i),this},e.prototype.simplesort=function(t,i){this.searchIsChained&&!this.filterInitialized&&0==this.filteredrows.length&&(this.filteredrows=Object.keys(this.collection.data)),"undefined"==typeof i&&(i=!1);var e=function(t,i,e){return function(s,n){var r=e.collection.data[s],o=e.collection.data[n];if(r[t]==o[t])return 0;if(i){if(r[t]<o[t])return 1;if(r[t]>o[t])return-1}else{if(r[t]>o[t])return 1;if(r[t]<o[t])return-1}}}(t,i,this);return this.filteredrows.sort(e),this},e.prototype.find=function(t){function i(t,i){return t===i}function e(t,i){return t>i}function s(t,i){return t>=i}function n(t,i){return i>t}function r(t,i){return i>=t}function o(t,i){return t!==i}var h,l,a,c,u,d,f,p,y=t||"getAll",w={$eq:i,$gt:e,$gte:s,$lt:n,$lte:r,$ne:o},m=!1,g=[],v=null;if("getAll"===y)return this;for(c in y)if(y.hasOwnProperty(c)){if(h=c,"object"!=typeof y[c])a="$eq",l=y[c];else{if("object"!=typeof y[c])throw"Do not know what you want to do.";for(u in y[c])y[c].hasOwnProperty(u)&&(a=u,l=y[c][u])}break}if(null===this.collection.data)throw new TypeError;if(this.collection.indices.hasOwnProperty(h)&&(m=!0,v=this.collection.indices[h]),d=w[a],this.searchIsChained){if(this.filterInitialized){if(m)for(f=v,p=this.filteredrows.length;p--;)d(f[this.filteredrows[p]],l)&&g.push(this.filteredrows[p]);else for(f=this.collection.data,p=this.filteredrows.length;p--;)d(f[this.filteredrows[p]][h],l)&&g.push(this.filteredrows[p]);return this.filteredrows=g,this}if(m)for(f=v,p=f.length;p--;)d(f[p],l)&&g.push(p);else for(f=this.collection.data,p=f.length;p--;)d(f[p][h],l)&&g.push(p);return this.filteredrows=g,this.filterInitialized=!0,this}if(m)for(f=v,p=v.length;p--;)d(f[p],l)&&g.push(this.collection.data[p]);else for(f=this.collection.data,p=f.length;p--;)d(f[p][h],l)&&g.push(f[p]);return g},e.prototype.where=function(t){var i,e=[];if("string"==typeof t&&"function"==typeof this.collection.Views[t])i=this.collection.Views[t];else{if("function"!=typeof t)throw"Argument is not a stored view or a function";i=t}try{if(this.searchIsChained){if(this.filterInitialized){for(var s=this.filteredrows.length;s--;)i(this.collection.data[this.filteredrows[s]])===!0&&e.push(this.filteredrows[s]);return this.filteredrows=e,this}for(var s=this.collection.data.length;s--;)i(this.collection.data[s])===!0&&e.push(s);return this.filteredrows=e,this.filterInitialized=!0,this}for(var s=this.collection.data.length;s--;)i(this.collection.data[s])===!0&&e.push(this.collection.data[s]);return e}catch(n){throw n}},e.prototype.data=function(){var t=[];if(this.searchIsChained&&!this.filterInitialized){if(0==this.filteredrows.length)return this.collection.data;this.filterInitialized=!0}for(var i in this.filteredrows)t.push(this.collection.data[this.filteredrows[i]]);return t},s.prototype.toJSON=function(){var t=new s(this.collection,this.name,this.persistent);return t.resultset=this.resultset,t.resultdata=this.resultdata,t.resultsdirty=this.resultsdirty,t.filterPipeline=this.filterPipeline,t.sortFunction=this.sortFunction,t.sortColumn=this.sortColumn,t.sortColumnDesc=this.sortColumnDesc,t.sortDirty=this.sortDirty,t.collection=null,t},s.prototype.applySort=function(t){return this.sortFunction=t,this.sortColumn=null,this.sortColumnDesc=!1,this.resultset.sort(t),this.sortDirty=!1,this},s.prototype.applySimpleSort=function(t,i){return"undefined"==typeof i&&(i=!1),this.sortColumn=t,this.sortColumnDesc=i,this.sortFunction=null,this.resultset.simplesort(t,i),this.sortDirty=!1,this},s.prototype.startTransaction=function(){this.cachedresultset=this.resultset.copy()},s.prototype.commit=function(){this.cachedresultset=null},s.prototype.rollback=function(){this.resultset=this.cachedresultset,this.persistent&&(this.resultdata=this.resultset.data())},s.prototype.applyFind=function(t){return this.filterPipeline.push({type:"find",val:t}),this.resultset.find(t),this.sortDirty=!0,this.persistent&&(this.resultsdirty=!0),this},s.prototype.applyWhere=function(t){return this.filterPipeline.push({type:"where",val:t}),this.resultset.where(t),this.sortDirty=!0,this.persistent&&(this.resultsdirty=!0),this},s.prototype.data=function(){return this.sortDirty&&(this.sortFunction&&this.resultset.sort(this.sortFunction),this.sortColumn&&this.resultset.simplesort(this.sortColumn,this.sortColumnDesc),this.sortDirty=!1),this.persistent?(this.resultsdirty&&(this.resultdata=this.resultset.data(),this.resultsdirty=!1),this.resultdata):this.resultset.data()},s.prototype.evaluateDocument=function(t){var i=this.resultset.filteredrows,s=i.indexOf(t),n=i.length,r=new e(this.collection);r.filteredrows=[t],r.filterInitialized=!0;for(var o=0;o<this.filterPipeline.length;o++)switch(this.filterPipeline[o].type){case"find":r.find(this.filterPipeline[o].val);break;case"where":r.where(this.filterPipeline[o].val)}var h=0==r.filteredrows.length?-1:0;return-1!=s||-1!=h?-1==s&&-1!=h?(i.push(t),this.persistent&&this.resultdata.push(this.collection.data[t]),void 0):-1!=s&&-1==h?(n-1>s?(i[s]=i[n-1],i.length=n-1,this.persistent&&(this.resultdata[s]=this.resultdata[n-1],this.resultdata.length=n-1)):(i.length(n-1),this.persistent&&(this.resultdata.length=n-1)),void 0):-1!=s&&-1!=h?(this.persistent&&(this.resultdata[s]=this.collection.data[t]),void 0):void 0:void 0},s.prototype.removeDocument=function(t){var i=this.resultset.filteredrows,e=i.indexOf(t),s=i.length;-1!=e&&(s-1>e?(i[e]=i[s-1],i.length=s-1,this.resultdata[e]=this.resultdata[s-1],this.resultdata.length=s-1):(i.length=s-1,this.resultdata.length=s-1))},i.prototype.addCollection=function(t,i,e,s){var r=new n(t,i,e,s);return this.collections.push(r),r},i.prototype.loadCollection=function(t){this.collections.push(t)},i.prototype.getCollection=function(t){var i,e=!1,s=this.collections.length;for(i=0;s>i;i+=1)if(this.collections[i].name===t)return e=!0,this.collections[i];if(!e)throw"No such collection"},i.prototype.listCollections=function(){for(var t=this.collections.length,i=[];t--;)i.push({name:this.collections[t].name,type:this.collections[t].objType,count:this.collections[t].data.length});return i},i.prototype.removeCollection=function(t){var i=0,e=this.collections.length;for(i;e>i;i+=1)if(this.collections[i].name===t){this.collections.splice(i,1);break}},i.prototype.getName=function(){return this.name},i.prototype.serialize=function(){return JSON.stringify(this)},i.prototype.toJson=i.prototype.serialize,i.prototype.loadJSON=function(t){var i,e,s,n,r=JSON.parse(t),o=0,h=r.collections.length;for(this.name=r.name,this.collections=[],o;h>o;o+=1){for(i=r.collections[o],e=this.addCollection(i.name,i.objType),s=i.data.length,n=0;s>n;n++)e.data[n]=i.data[n];if(e.maxId=0==i.data.length?0:i.data.maxId,e.indices=i.indices,e.idIndex=i.indices.id,e.transactional=i.transactional,e.ensureAllIndexes(),"undefined"!=typeof i.DynamicViews)for(var l=0;l<i.DynamicViews.length;l++){var a=i.DynamicViews[l],c=e.addDynamicView(a.name,a.persistent);c.resultdata=a.resultdata,c.resultsdirty=a.resultsdirty,c.filterPipeline=a.filterPipeline,c.sortColumn=a.sortColumn,c.sortColumnDesc=a.sortColumnDesc,c.sortFunction=a.sortFunction,c.sortDirty=a.sortDirty,c.resultset.filteredrows=a.resultset.filteredrows,c.resultset.searchIsChained=a.resultset.searchIsChained,c.resultset.filterInitialized=a.resultset.filterInitialized}}},i.prototype.loadDatabase=function(t){var i=t||function(){},e=this;"NODEJS"===this.ENV&&this.fs.readFile(this.filename,{encoding:"utf8"},function(t,s){if(t)throw t;e.loadJSON(s),i(s)})},i.prototype.saveToDisk=function(t){var i=t||function(){},e=this;"NODEJS"===this.ENV&&this.fs.exists(this.filename,function(t){t&&e.fs.unlink(e.filename),e.fs.writeFile(e.filename,e.serialize(),function(t){if(t)throw t;i()})})},i.prototype.save=i.prototype.saveToDisk,n.prototype.ensureIndex=function(t){if(null===t||void 0===t)throw"Attempting to set index without an associated property";var i,e=this.data.length,s=0;for(this.indices.hasOwnProperty(t)?i=this.indices[t]:(this.indices[t]=[],i=this.indices[t]),s;e>s;s+=1)i.push(this.data[s][t]);"id"===t&&(this.idIndex=i)},n.prototype.ensureIndexAsync=function(t,i){this.async(function(){this.ensureIndex(t)},i)},n.prototype.ensureAllIndexes=function(){for(var t=this.indices.length;t--;)this.ensureIndex(this.indices[t].name);0===t&&this.ensureIndex("id")},n.prototype.ensureAllIndexesAsync=function(t){this.async(function(){this.ensureAllIndexes()},t)},n.prototype.addDynamicView=function(t,i){var e=new s(this,t,i);return this.DynamicViews.push(e),e},n.prototype.removeDynamicView=function(t){for(var i=0;i<this.DynamicViews.length;i++)this.DynamicViews[i].name==t&&this.DynamicViews.splice(i,1)},n.prototype.getDynamicView=function(t){for(var i=0;i<this.DynamicViews.length;i++)if(this.DynamicViews[i].name==t)return this.DynamicViews[i]},n.prototype.findAndUpdate=function(t,i){var e,s=this.view(t),n=0;try{for(n;n<s.length;n++)e=i(s[n]),this.update(e)}catch(r){this.rollback()}},n.prototype.insert=function(t){return t.id=null,t.objType=this.objType,this.add(t),t},n.prototype.clear=function(){this.data=[],this.indices={},this.idIndex={},this.cachedIndex=null,this.cachedData=null,this.maxId=0,this.Views={}},n.prototype.update=function(t){if(!t.hasOwnProperty("id"))throw"Trying to update unsynced document. Please save the document first by using add() or addMany()";try{this.startTransaction();var i,e=this.get(t.id,!0),s=e[0],n=e[1];this.data[n]=t;for(var r=0;r<this.DynamicViews.length;r++)this.DynamicViews[r].evaluateDocument(n);for(i in this.indices)this.indices.hasOwnProperty(i)&&(this.indices[i][n]=s[i]);this.commit()}catch(o){this.rollback()}},n.prototype.add=function(t){if("object"!=typeof t)throw"Object being added needs to be an object";if(""===this.objType&&0===this.data.length)this.objType=t.objType;else{if(this.objType!==t.objType)throw"Object type ["+t.objType+"] is incongruent with collection type ["+this.objType+"]";if(""===this.objType)throw"Object is not a model";if(null!==t.id&&t.id>0)throw"Document is already in collection, please use update()";try{this.startTransaction(),this.maxId++;var i;isNaN(this.maxId)&&(this.maxId=this.data[this.data.length-1].id+1),t.id=this.maxId,this.data.push(t);for(var e=0;e<this.DynamicViews.length;e++)this.DynamicViews[e].evaluateDocument(this.data.length-1);for(var e=0;e<this.DynamicViews.length;e++)this.DynamicViews[e].evaluateDocument(this.data.length-1);for(i in this.indices)this.indices.hasOwnProperty(i)&&this.indices[i].push(t[i]);return this.commit(),t}catch(s){this.rollback()}}},n.prototype.addMany=function(){for(var t=arguments.length;t--;)this.add(arguments[t])},n.prototype.remove=function(t){if("object"!=typeof t)throw"Parameter is not an object";if(!t.hasOwnProperty("id"))throw"Object is not a document stored in the collection";try{this.startTransaction();for(var i,e=this.get(t.id,!0),s=e[1],n=0;n<this.DynamicViews.length;n++)this.DynamicViews[n].removeDocument(s);this.data.splice(s,1);for(i in this.indices)this.indices.hasOwnProperty(i)&&this.indices[i].splice(s,1);this.commit()}catch(r){this.rollback()}},n.prototype.get=function(t,i){var e=i||!1,s=this.indices.id,n=s.length-1,r=0,o=Math.floor(r+(n-r)/2);if(isNaN(t)&&(t=parseInt(t),isNaN(t)))throw"Passed id is not an integer";for(;s[r]<s[n];)o=Math.floor((r+n)/2),s[o]<t?r=o+1:n=o;return n===r&&s[r]===t?e?[this.data[r],r]:this.data[r]:null},n.prototype.findOne=function(t,i){var e,s,n=!1,r=null,o=this.indices.length;for(o in this.indices)if(this.indices.hasOwnProperty(o)&&o===t){n=!0,r=this.indices[o];break}if(!n)return this.findOneUnindexed(t,i);for(e=r.data.length;e--;)if(r.data[e]===i)return s=this.data[e];return null},n.prototype.chain=function(){return new e(this,null,null)},n.prototype.find=function(t){return new e(this,t,null)},n.prototype.findOneUnindexed=function(t,i){for(var e,s=this.data.length;s--;)if(this.data[s][t]===i)return e=this.data[s];return null},n.prototype.startTransaction=function(){if(this.transactional){this.cachedData=t(this.data,this.transactional),this.cachedIndex=this.indices;for(var i=0;i<this.DynamicViews.length;i++)this.DynamicViews[i].startTransaction()}},n.prototype.commit=function(){if(this.transactional){this.cachedData=null,this.cachedIndex=null;for(var t=0;t<this.DynamicViews.length;t++)this.DynamicViews[t].commit()}},n.prototype.rollback=function(){if(this.transactional){null!==this.cachedData&&null!==this.cachedIndex&&(this.data=this.cachedData,this.indices=this.cachedIndex);for(var t=0;t<this.DynamicViews.length;t++)this.DynamicViews[t].rollback()}},n.prototype.async=function(t,i){setTimeout(function(){if("function"!=typeof t)throw"Argument passed for async execution is not a function";t(),i()},0)},n.prototype.view=function(t){return new e(this,null,t)},n.prototype.storeView=function(t,i){"function"==typeof i&&(this.Views[t]=i)},n.prototype.mapReduce=function(t,i){try{return i(this.data.map(t))}catch(e){throw e}},n.prototype.no_op=function(){},i}();"undefined"!=typeof module&&module.exports&&(module.exports=loki);