@hollax/dexie-orm
Version:
Simple ORM for dexie js
2 lines (1 loc) • 2.96 kB
JavaScript
import{filters as l}from"./filters.js";const o=function(i,t,e){return r=>t.call(r,r[i],e)},h=function(i,t,e){return()=>t.call(i,i,e)};class f{constructor(t){this._primaryQueryAdded=!1,this._filters=[],this._index=0,this._sortDesc=!1,this._tableStore=t}offset(t){return this._offset=t,this}limit(t){return this._limit=t,this}sortBy(t,e=!1){return this._sortBy=t,this._sortDesc=e,this}reverse(){return this._sortDesc=!0,this}where(t){return this._index===0&&(this._whereBulder=this._tableStore.where(t)),this._currentKeyPath=t,this}and(t){if(this._index===0)throw new Error("Can not use .and() with first where condition");return this.where(t)}above(t){return this._processFilter("above",t)}aboveOrEqual(t){return this._processFilter("aboveOrEqual",t)}anyOf(t){return this._processFilter("anyOf",t)}anyOfIgnoreCase(t){return this._processFilter("anyOfIgnoreCase",t)}below(t){return this._processFilter("below",t)}belowOrEqual(t){return this._processFilter("belowOrEqual",t)}between(t,e,r=!0,s=!0){return this._processFilter("between",[t,e,r,s])}equals(t){return this._processFilter("equals",t)}equalsIgnoreCase(t){return this._processFilter("equalsIgnoreCase",t)}like(t){var e=t&&t.toUpperCase(),r=this._currentKeyPath;return this.filter(s=>s[r]&&s[r].toUpperCase().indexOf(e)!==-1)}in(t){return this._processFilter("anyOf",t)}noneOf(t){return this._processFilter("noneOf",t)}notEqual(t){return this._processFilter("notEqual",t)}startsWith(t){return this._processFilter("startsWith",t)}startsWithAnyOf(t){return this._processFilter("startsWithAnyOf",t)}startsWithAnyOfIgnoreCase(t){return this._processFilter("startsWithAnyOfIgnoreCase",t)}startsWithIgnoreCase(t){return this._processFilter("startsWithIgnoreCase",t)}filter(t){return typeof t=="function"&&this._filters.push(t),this}count(){let t=this.build();return t.sorted?t.collection.then(e=>e.length):t.collection.count()}first(){let t=this.build();return t.sorted?t.collection.then(e=>e[0]):t.collection.first()}last(){let t=this.build();return t.sorted?t.collection.then(e=>e[e.length-1]):t.collection.last()}fetch(){return this.all()}all(){let t=this.build();return t.sorted?t.collection.then(e=>e):t.collection.toArray()}delete(){return this.build().delete()}build(){var r;let t,e={sorted:!1,collection:void 0};return this._collection?t=(r=this._collection)==null?void 0:r.call(this):this._whereBulder?t=this._whereBulder:t=this._tableStore,this._filters.length&&(this._index===0&&(t=this._tableStore),t=t.filter(s=>this._filters.findIndex(n=>!n(s))===-1)),this._offset&&(t=t.offset(this._offset)),this._limit&&(t=t.limit(this._limit)),e.collection=t,this._sortBy&&(this._sortDesc&&(t=t.reverse()),e.collection=t.sortBy(this._sortBy),e.sorted=!0),e}_processFilter(t,e){let r=l[t];if(!r)throw new Error("Invalid filter: "+t);return this._index===0?this._collection=h(this._whereBulder,r.index,e):this._filters.push(o(this._currentKeyPath,r.filter,e)),this._index++,this}}export{f as QueryBuilder};