pixiedb
Version:
A tiny in-memory javascript database with indexing and SQL like filters.
2 lines (1 loc) • 14.7 kB
JavaScript
var kt=Object.defineProperty,yt=(i,t,e)=>t in i?kt(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,bt=(i,t,e)=>(yt(i,typeof t!="symbol"?t+"":t,e),e);class A extends Error{constructor(t,e){super(e),bt(this,"name"),this.name=`${t}Error`}}function H(i){return Object.freeze(i)}function ut(i){return i==null}function T(i){return Array.isArray(i)}function Z(i){return Array.isArray(i)?i:i instanceof Set?Array.from(i):[i]}function Rt(i,t){i.splice(i.indexOf(t),1)}function rt(i){if(!T(i))throw new A("Value","Invalid values")}function pt(i){if(!T(i)||i.length!==2)throw new A("Value","Invalid values")}function O(i,t){return Object.hasOwnProperty.call(i,t)}function xt(i){return JSON.parse(JSON.stringify(i))}function St(i,t=1e3,...e){let s;return function(){clearTimeout(s),s=setTimeout((...r)=>{i(...r),s=void 0},t,...e)}}var Et=Object.defineProperty,Wt=(i,t,e)=>t in i?Et(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,dt=(i,t,e)=>(Wt(i,typeof t!="symbol"?t+"":t,e),e);class Mt{constructor(t){dt(this,"events",H({L:[],I:[],U:[],D:[],C:[],Q:[]})),dt(this,"cEv"),this.setCEVTime(t)}setCEVTime(t){this.cEv=St(()=>this.emit("C"),t)}on(t,e){return Z(t).forEach(s=>{O(this.events,s)&&this.events[s].push(e)}),e}offAll(){this.events=H({L:[],I:[],U:[],D:[],C:[],Q:[]})}off(t,e){return Z(t).forEach(s=>O(this.events,s)&&Rt(this.events[s],e)),e}emit(...t){const[e,...s]=t;O(this.events,e)&&setTimeout(()=>this.events[e].forEach(r=>r(e,s)),10),"IUD".includes(e)&&this.cEv()}}var ht=(i,t,e)=>{if(!t.has(i))throw TypeError("Cannot "+e)},n=(i,t,e)=>(ht(i,t,"read from private field"),e?e.call(i):t.get(i)),f=(i,t,e)=>{if(t.has(i))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(i):t.set(i,e)},u=(i,t,e,s)=>(ht(i,t,"write to private field"),s?s.call(i,e):t.set(i,e),e),l=(i,t,e)=>(ht(i,t,"access private method"),e),z,g,D,R,x,P,_,I,y,S,J,V,L,q,G,nt,Q,X,U,B,Y,at,j,C,tt,lt,E,N,F,K,ot,gt,m,k;class $t{constructor(t,e,s,r,h,c="select"){f(this,G),f(this,Q),f(this,U),f(this,Y),f(this,j),f(this,tt),f(this,E),f(this,F),f(this,ot),f(this,m),f(this,z,[]),f(this,g,[]),f(this,D,!1),f(this,R,0),f(this,x,-1),f(this,P,!0),f(this,_,void 0),f(this,I,void 0),f(this,y,void 0),f(this,S,void 0),f(this,J,void 0),f(this,V,[]),f(this,L,[]),f(this,q,void 0),u(this,q,t),u(this,I,e),u(this,y,s),u(this,S,r),u(this,_,c),u(this,J,h??[])}delete(){return l(this,E,N).call(this,"where"),u(this,P,!1),l(this,j,C).call(this),l(this,tt,lt).call(this)}update(t){return O(t,n(this,I))&&delete t[n(this,I)],l(this,E,N).call(this,"where"),l(this,j,C).call(this),l(this,tt,lt).call(this,t)}data(){return l(this,E,N).call(this,"select"),l(this,j,C).call(this),l(this,F,K).call(this,n(this,g)),n(this,g).map(t=>l(this,U,B).call(this,l(this,Y,at).call(this,t)))}single(){return l(this,E,N).call(this,"select"),l(this,j,C).call(this),l(this,F,K).call(this,n(this,g)),l(this,U,B).call(this,l(this,Y,at).call(this,n(this,g)[0]))}range(t=0,e=-1){return t>=0&&u(this,R,t),e>=0&&u(this,x,e),n(this,L).push("R"),this}count(){return u(this,P,!1),n(this,z).length?l(this,j,C).call(this):n(this,y).size}orderBy(t){return rt(t),u(this,V,t.map(e=>{const s=T(e)?e:[e,"asc"];if(s.length!==2||typeof s[0]!="string"||!["asc","desc"].includes(s[1]))throw new A("Value","Incorrect values for orderBy");return s})),n(this,L).push("S"),l(this,j,C).call(this),l(this,ot,gt).call(this),this}eq(t,e){return l(this,m,k).call(this,"eq",t,e)}in(t,e){rt(e);const s=new Set(e);return l(this,m,k).call(this,"in",t,s)}nin(t,e){rt(e);const s=new Set(e);return l(this,m,k).call(this,"nin",t,s)}between(t,e){return pt(e),l(this,m,k).call(this,"btw",t,e[0],e[1])}nbetween(t,e){return pt(e),l(this,m,k).call(this,"nbtw",t,e[0],e[1])}neq(t,e){return l(this,m,k).call(this,"neq",t,e)}gt(t,e){return l(this,m,k).call(this,"gt",t,e)}gte(t,e){return l(this,m,k).call(this,"gte",t,e)}lt(t,e){return l(this,m,k).call(this,"lt",t,e)}lte(t,e){return l(this,m,k).call(this,"lte",t,e)}toJSON(t=!1){return l(this,E,N).call(this,"select"),u(this,P,t),this.data()}}z=new WeakMap,g=new WeakMap,D=new WeakMap,R=new WeakMap,x=new WeakMap,P=new WeakMap,_=new WeakMap,I=new WeakMap,y=new WeakMap,S=new WeakMap,J=new WeakMap,V=new WeakMap,L=new WeakMap,q=new WeakMap,G=new WeakSet,nt=function(i,t,e="eq",s){let r,h;switch(e){case"eq":r=n(this,S)[i].get(t);break;case"in":h=n(this,S)[i].in(t);break;case"btw":if(s===void 0)break;h=n(this,S)[i].btw(t,s);break;case"gt":case"lt":case"gte":case"lte":const c=/^gt/.test(e);h=n(this,S)[i][c?"gt":"lt"](t,/e$/.test(e)),c||h.reverse();break}h&&(n(this,q).isUniqIdx(i)?r=h:h.forEach(c=>c instanceof Set&&c.forEach(a=>l(this,Q,X).call(this,a)))),r?.forEach(c=>l(this,Q,X).call(this,c))},Q=new WeakSet,X=function(i){const t=n(this,y).get(i);t&&n(this,g).push(l(this,U,B).call(this,t))},U=new WeakSet,B=function(i,t){try{return(n(this,P)||t)&&i?n(this,q).cloneMethod(i):i}catch{}return i},Y=new WeakSet,at=function(i){return n(this,J).length?n(this,J).reduce((t,e)=>(t[e]=i[e],t),{}):i},j=new WeakSet,C=function(){try{if(n(this,D))throw"";let i=!1,t=n(this,R),e=n(this,x);const s=n(this,z),r=s[0];if(!r){if(t===0&&e===1){const a=n(this,y).values().next().value;u(this,g,a?[a]:[])}else l(this,F,K).call(this);throw u(this,D,!0),""}if(r[1]===n(this,I)&&/^(eq|in)/.test(r[0])?((r[2]instanceof Set?r[2]:[r[2]]).forEach(a=>l(this,Q,X).call(this,a)),i=!0):O(n(this,S),r[1])&&/btw|^(eq|gt|in|lt)/.test(r[0])&&(r[0]==="nbtw"&&(l(this,G,nt).call(this,r[1],r[2],"lt"),r[2]=r[3],r[0]="gt"),l(this,G,nt).call(this,r[1],r[2],r[0],r[3]),i=!0),i&&s.shift(),!s.length)throw i||u(this,g,l(this,F,K).call(this)),u(this,D,!0),"";const h=i?n(this,g):n(this,y).values();u(this,g,[]);let c;for(const a of h)if(c=!s.some(([v,o,d,p])=>{switch(v){case"btw":return!(a[o]>=d&&a[o]<=p);case"eq":return a[o]!==d;case"gt":return a[o]<=d;case"gte":return a[o]<d;case"in":return!d.has(a[o]);case"lt":return a[o]>=d;case"lte":return a[o]>d;case"nbtw":return a[o]>=d&&a[o]<=p;case"neq":return a[o]===d;case"nin":return d.has(a[o]);default:throw new A("Filter","Unsupported filter: "+v)}}),c&&--t<0&&(n(this,g).push(a),e!==-1&&--e<=0))break;u(this,D,!0),u(this,R,0),u(this,x,-1)}catch(i){if(i)throw i}return n(this,g).length},tt=new WeakSet,lt=function(i){l(this,E,N).call(this,"where");const t=[];let e;const s=n(this,S),r=n(this,I);let h,c=Object.keys(s);return i&&(c=c.filter(a=>O(i,a))),n(this,g).forEach(a=>{c.forEach(o=>{if(h=n(this,q).isUniqIdx(o),i){if(a[o]===i[o])return;if(h){s[o].set(i[o],a[r]);return}e=s[o].get(i[o]),e||s[o].set(i[o],e=new Set),e.add(a[r])}if(h)s[o].delete(a[o]);else{const d=s[o].getNode(a[o]);d&&d.value instanceof Set&&(e=d.value,e.delete(a[r]),e.size||s[o].deleteNode(d))}});let v=a;if(i){Object.assign(a,i),n(this,y).set(a[r],a);const o=l(this,U,B).call(this,v);v=l(this,U,B).call(this,a,!0),n(this,q).emit("U",v,o)}else n(this,y).delete(a[r]),n(this,q).emit("D",a);t.push(v)}),t},E=new WeakSet,N=function(i){if(n(this,_)!==i)throw new A("Action",`unable to perform this action in "${i}"`)},F=new WeakSet,K=function(i=[...n(this,y).values()]){return u(this,g,n(this,R)!==0||n(this,x)!==-1?i.slice(n(this,R),n(this,x)===-1?void 0:n(this,R)+n(this,x)):i),u(this,R,0),u(this,x,-1),n(this,g)},ot=new WeakSet,gt=function(){const i=n(this,V).length;i&&(l(this,E,N).call(this,"select"),n(this,g).sort((t,e)=>{let s=0;for(let r=0;r<i;r++){const h=n(this,V)[r],c=i-r,[a,v]=h,o=ut(t[a])?0:t[a],d=ut(e[a])?0:e[a];let p=o===d?0:o<d?-1:1;p*=c,v==="desc"&&(p*=-1),s+=p}return s}),u(this,V,[]))},m=new WeakSet,k=function(...i){const t=n(this,L).at(-1);if(t&&t!=="F")throw new A("Action","Unable to add filter");return n(this,z).push(i),this};var Ot=Object.defineProperty,qt=(i,t,e)=>t in i?Ot(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,W=(i,t,e)=>(qt(i,typeof t!="symbol"?t+"":t,e),e);class jt{constructor(){W(this,"root",null),W(this,"mn",null),W(this,"mx",null)}set(t,e,s=!0){const r=new Nt(t,e);let h=this.root;if(h){for(;;)if(t===h.key){if(s){h.value=e;break}return!1}else if(t<h.key)if(h.left)h=h.left;else{h.left=r,r.parent=h;break}else if(h.right)h=h.right;else{h.right=r,r.parent=h;break}this.fixTree(r)}else this.root=r,this.root.isRed=!1;return this.setMinMax(r),!0}setMinMax(t){(!this.mx||this.mx.key<t.key)&&(this.mx=t),(!this.mn||this.mn.key>t.key)&&(this.mn=t)}get(t){return this.getNode(t)?.value}btw(t,e){let s=this.root;if(!s)return[];const r=[];let h=[];for(;;)if(s)h.push(s),s=s.left;else if(h.length){if(s=h.pop(),s.key>=t&&s.key<=e)r.push(s.value);else if(s.key>e)break;s=s.right}else break;return r}in(t){const e=[];for(const s of t){const r=this.getNode(s);r&&e.push(r.value)}return e}trav(t){let e=this.root,s;for(;e;)if(s=t(e.key,e.value),s>0)e=e.right;else if(s<0)e=e.left;else return}lt(t,e){const s=[],r=[];let h=this.root;for(;h;)h.key<t||e&&h.key===t?(r.push(h),h=h.right):h=h.left;for(;r.length>0;)for(h=r.shift(),s.push(h.value),h=h.left;h;)r.push(h),h=h.right;return s}gt(t,e){const s=[];let r=this.root;if(!r||t>=this.maxNode().key)return s;const h=[];for(;r;)r.key>t||e&&r.key===t?(h.push(r),r=r.left):r=r.right;for(;h.length>0;)for(r=h.pop(),s.push(r.value),r=r.right;r;)h.push(r),r=r.left;return s}delete(t){const e=this.getNode(t);if(e)return this.deleteNode(e),e.value}fixTree(t){for(;t&&t.parent&&t.parent.isRed;){let e;if(t.parent===t.parent.parent.left?e=t.parent.parent.right:e=t.parent.parent.left,e&&e.isRed)t.parent.isRed=!1,e.isRed=!1,t=t.parent.parent;else{t===t.parent.right&&t.parent===t.parent.parent.left?(t=t.parent,this.leftRotate(t)):t===t.parent.left&&t.parent===t.parent.parent.right&&(t=t.parent,this.rightRotate(t)),t.parent.isRed=!1;const s=t.parent.parent;s&&(s.isRed=!0,t===t.parent.left?this.rightRotate(s):this.leftRotate(s))}}this.root.isRed=!1}leftRotate(t){const e=t.right;t.right=e.left,e.left&&(e.left.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this.root=e,e.left=t,t.parent=e}rightRotate(t){const e=t.left;t.left=e.right,e.right&&(e.right.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this.root=e,e.right=t,t.parent=e}getNode(t){let e=this.mn;if(e){if(e.key===t)return e;if(e.key>t)return}if(e=this.mx,e){if(e.key===t)return e;if(e.key<t)return}for(e=this.root;e;){if(t===e.key)return e;t<e.key?e=e.left:e=e.right}}minFrom(t){let e=t;for(;e.left;)e=e.left;return e}maxNode(){if(this.mx)return this.mx;let t=this.root;for(;t&&t.right;)t=t.right;return this.mx=t}minNode(){return this.mn?this.mn:this.root?this.mn=this.minFrom(this.root):null}min(){const t=this.minNode();if(t)return[t?.key,t?.value]}max(){const t=this.maxNode();if(t)return[t?.key,t?.value]}deleteNode(t){let e=null,s=null;t.left?t.right?(s=this.minFrom(t.right),s!==t.right&&(e=s,this.transplant(s,s.right),s.right=t.right,s.right.parent=s),this.transplant(t,s),s.left=t.left,s.left.parent=s,s.isRed=t.isRed):s=t.left:s=t.right,t===this.root?this.root=s:e?e.parent=t.parent:this.transplant(t,s),t.isRed||this.fixDeletion(s===null?t:s),this.mx===t&&(this.mx=null),this.mn===t&&(this.mn=null)}transplant(t,e){t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this.root=e,e&&(e.parent=t.parent)}fixDeletion(t){for(;t!==this.root&&t&&!t.isRed;)if(t===t.parent?.left){let e=t.parent.right;e?.isRed&&(e.isRed=!1,t.parent.isRed=!0,this.leftRotate(t.parent),e=t.parent.right),e?(!e.left||!e.left.isRed)&&(!e.right||!e.right.isRed)?(e.isRed=!0,t=t.parent):((!e.right||!e.right.isRed)&&(e.left.isRed=!1,e.isRed=!0,this.rightRotate(e),e=t.parent.right),e.isRed=t.parent.isRed,t.parent.isRed=!1,e.right.isRed=!1,this.leftRotate(t.parent),t=this.root):t=t.parent}else{let e=t.parent?.left;e?.isRed&&(e.isRed=!1,t.parent.isRed=!0,this.rightRotate(t.parent),e=t.parent?.left),e?(!e.left||!e.left.isRed)&&(!e.right||!e.right.isRed)?(e.isRed=!0,t=t.parent):((!e.left||!e.left.isRed)&&(e.right.isRed=!1,e.isRed=!0,this.leftRotate(e),e=t.parent?.left),e.isRed=t.parent.isRed,t.parent.isRed=!1,e.left.isRed=!1,this.rightRotate(t.parent),t=this.root):t=t.parent}t&&(t.isRed=!1)}clear(){this.root=this.mx=this.mn=null}}let Nt=class{constructor(t,e,s=!0){W(this,"key"),W(this,"value"),W(this,"isRed"),W(this,"left"),W(this,"right"),W(this,"parent"),this.key=t,this.value=e,this.isRed=s,this.left=this.right=this.parent=null}};var At=Object.defineProperty,It=(i,t,e)=>t in i?At(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,wt=(i,t,e)=>(It(i,typeof t!="symbol"?t+"":t,e),e),ct=(i,t,e)=>{if(!t.has(i))throw TypeError("Cannot "+e)},w=(i,t,e)=>(ct(i,t,"read from private field"),e?e.call(i):t.get(i)),et=(i,t,e)=>{if(t.has(i))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(i):t.set(i,e)},vt=(i,t,e,s)=>(ct(i,t,"write to private field"),s?s.call(i,e):t.set(i,e),e),mt=(i,t,e)=>(ct(i,t,"access private method"),e),M,b,it,st,ft;class Ut extends Mt{constructor(t,e=[],s){super(200),et(this,st),wt(this,"key"),et(this,M,H(new Map)),wt(this,"cloneMethod",xt),et(this,b,void 0),et(this,it,void 0),this.key=t;const r=new Set([this.key]);e.push(t);const h=Object.create(null);e.forEach(c=>{typeof c=="object"&&c.unique&&r.add(c.name),h[typeof c=="object"?c.name:c]=new jt}),vt(this,it,new RegExp(`^(${Z(r).map(c=>c.toString().replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|")})$`)),vt(this,b,H(h)),s&&this.load(s)}select(t){return mt(this,st,ft).call(this,T(t)?t:[])}where(){return mt(this,st,ft).call(this,[],"where")}insert(t,{silent:e,clone:s,upsert:r}={}){if(typeof t!="object")throw new A("Value","Value must be an object or object array");let h;const c=Z(t),a=[],v=w(this,b),o=this.key,d=this.indexes;return c.forEach(p=>{try{if(w(this,M).get(p[o])){r&&this.where().eq(this.key,p[this.key]).update(p).length&&a.push(p);return}d.forEach($=>{if(O(v,$)){if(this.isUniqIdx($)){v[$].set(p[$],p[o]);return}h=v[$].get(p[$]),h||v[$].set(p[$],h=new Set),h.add(p[o])}}),w(this,M).set(p[o],s===!1?p:this.cloneMethod(p)),e||this.emit("I",p),a.push(p)}catch{}}),T(t)?a:a[0]}load(t,e=!1){e&&(w(this,M).clear(),Object.values(w(this,b)).forEach(s=>s.clear())),this.insert(t,{silent:!0,clone:!1}),this.emit("L")}get(t){return w(this,M).get(t)}data(t=!0){const e=[...w(this,M).values()];return t?this.cloneMethod(e):e}get indexes(){return Object.keys(w(this,b))}isUniqIdx(t){return w(this,it).test(t.toString())}close(t=!1){w(this,M).clear(),Object.keys(w(this,b)).forEach(e=>{O(w(this,b),e)&&w(this,b)[e].clear()}),t||this.emit("Q"),this.offAll()}toJSON(){return{key:this.key,indexes:this.indexes.map(t=>this.isUniqIdx(t)?{name:t,unique:!0}:t),data:this.data()}}}M=new WeakMap,b=new WeakMap,it=new WeakMap,st=new WeakSet,ft=function(i=[],t){return new $t(this,this.key,w(this,M),w(this,b),i,t)};export{Ut as PixieDb};