-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlocalDB.min.js
2 lines (2 loc) · 4.26 KB
/
localDB.min.js
1
2
// 小程序本地数据库 https://github.com/jin-yufeng/MpLocalDB
function t(){e=!0,setTimeout(function(){e&&(e=!1,wx.setStorage({data:i,key:"localDB"}))},200)}function n(t,n){for(var r in n)t[r]="function"==typeof n[r]?n[r](t[r]):n[r]}function r(t,n,r){this.data=t,this.options=r||{orderBy:[]},this.root=n||t}function o(t){this.exec=t}var i,e=!1;r.prototype.add=function(n){var r=n._id||"";if("object"!=(void 0===n?"undefined":typeof(n))||this.data[r])return null;for(n=JSON.parse(JSON.stringify(n)),n._id=void 0;!r||this.data[r];)for(var o=0;o<4;o++)r+="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[parseInt(62*Math.random())];return this.data[r]=n,t(),r},r.prototype.doc=function(r){var o=this,i=this.data[r];return{get:function(){var t=JSON.parse(JSON.stringify(i));return t._id=r,t},update:function(r){return"object"==(void 0===r?"undefined":typeof(r))&&(n(i,r),t(),!0)},remove:function(){return o.data[r]=void 0,!0}}},r.prototype.where=function(t){var n=this,i=function(t,r){for(var i in t){var e="_id"==i?r:n.data[r][i];if(t[i]instanceof o||t[i]instanceof RegExp){if(!t[i].exec(e))return!1}else if(t[i]!=e)return!1}return!0},e={};for(var u in this.data)if(this.data[u])if("or"==t.type){for(var f=0;f<t.arr.length;f++)if(i(t.arr[f],u)){e[u]=this.data[u];break}}else i(t,u)&&(e[u]=this.data[u]);return new r(e,this.root,this.options)},r.prototype.limit=function(t){var n=JSON.parse(JSON.stringify(this.options));return n.limit=t,new r(this.data,this.root,n)},r.prototype.skip=function(t){var n=JSON.parse(JSON.stringify(this.options));return n.skip=t,new r(this.data,this.root,n)},r.prototype.orderBy=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"asc",o=JSON.parse(JSON.stringify(this.options));return o.orderBy.push({field:t,order:n}),new r(this.data,this.root,o)},r.prototype.count=function(){var t=0;for(var n in this.data)this.data[n]&&t++;return t},r.prototype.get=function(){var t=this,n=[],r=function(r){var o=t.data[r];o&&(o=JSON.parse(JSON.stringify(o)),o._id=r,n.push(o))};if(this.options.orderBy.length){for(var o in this.data)r(o);n.sort(function(n,r){for(var o,i=0;(o=t.options.orderBy[i]);i++)if(n[o.field]!=r[o.field])return("desc"==o.order?-1:1)*(n[o.field]-r[o.field]);return 0}),this.options.skip&&(n=n.slice(this.options.skip)),this.options.limit&&(n=n.slice(0,this.options.limit))}else{var i;for(var e in this.data){if((!this.options.skip||i>=this.options.skip)&&(r(e),i+1-(this.options.skip||0)==this.options.limit))break;i++}}return n},r.prototype.update=function(r){if("object"!=(void 0===r?"undefined":typeof(r)))return!1;for(var o in this.data)n(this.data[o],r);return t(),!0},r.prototype.remove=function(){for(var n in this.data)this.root[n]=void 0;t()},o.prototype.and=function(t){var n=this;return new o(function(r){return n.exec(r)&&t.exec(r)})},o.prototype.or=function(t){var n=this;return new o(function(r){return n.exec(r)||t.exec(r)})},module.exports={init:function(){i||(i=wx.getStorageSync("localDB")||{})},createCollection:function(n){return i?!i[n]&&(i[n]={},t(),new r(i[n])):(console.warn("请先初始化"),!1)},removeCollection:function(n){if(!i)return console.warn("请先初始化");i[n]=void 0,t()},collection:function(t){return i?i[t]?new r(i[t]):null:(console.warn("请先初始化"),!1)},command:{eq:function(t){return new o(function(n){return n==t})},neq:function(t){return new o(function(n){return n!=t})},lt:function(t){return new o(function(n){return n<t})},lte:function(t){return new o(function(n){return n<=t})},gt:function(t){return new o(function(n){return n>t})},gte:function(t){return new o(function(n){return n>=t})},in:function(t){return new o(function(n){return t.includes(n)})},nin:function(t){return new o(function(n){return!t.includes(n)})},exists:function(t){return new o(function(n){return void 0==n?!t:t})},or:function(){for(var t=arguments.length,n=Array(t),r=0;r<t;r++)n[r]=arguments[r];return{type:"or",arr:n[0]instanceof Array?n[0]:n}},set:function(t){return JSON.parse(JSON.stringify(t))},remove:function(){},inc:function(t){return function(n){return n+=t}},mul:function(t){return function(n){return n*=t}},push:function(t){return function(n){return n.push(t),n}},pop:function(){return function(t){return t.pop(),t}},shift:function(){return function(t){return t.shift(),t}},unshift:function(t){return function(n){return n.unshift(t),n}}}}