@sliit-foss/mongoose-filter-query
Version:
Middleware which implements a standardized format and maps an incoming http request's query params to a format which is supported by mongoose
3 lines (2 loc) • 2.15 kB
JavaScript
var f=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(t,s)=>f(t,"name",{value:s,configurable:!0});var O=(t,s)=>{for(var r in s)f(t,r,{get:s[r],enumerable:!0})},j=(t,s,r,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let e of m(s))!d.call(t,e)&&e!==r&&f(t,e,{get:()=>s[e],enumerable:!(i=x(s,e))||i.enumerable});return t};var q=t=>j(f({},"__esModule",{value:!0}),t);var N={};O(N,{mapFilters:()=>W,mapValue:()=>$});module.exports=q(N);const h=["and","or"],o=p((t,s)=>t.replace(`${s}(`,"").slice(0,-1),"replaceOperator"),n=p((t,s)=>{if(s&&(t=o(t,s)),isNaN(t)){if(!isNaN(Date.parse(t)))t=new Date(t);else if(/^[0-9a-fA-F]{24}$/.test(t)){const r=require("bson").ObjectId;t=new r(t)}}else t=Number(t);return t},"parseOperatorValue"),$=p(t=>{if(t.startsWith("eq("))return t=n(t,"eq"),t==="true"||t==="false"?{$eq:t==="true"}:{$eq:t};if(t.startsWith("ne("))return t=n(t,"ne"),t==="true"||t==="false"?{$ne:t==="true"}:{$ne:t};if(t.startsWith("gt("))return{$gt:n(t,"gt")};if(t.startsWith("gte("))return{$gte:n(t,"gte")};if(t.startsWith("lt("))return{$lt:n(t,"lt")};if(t.startsWith("lte("))return{$lte:n(t,"lte")};if(t.startsWith("in("))return{$in:o(t,"in").split(",").map(s=>n(s))};if(t.startsWith("nin("))return{$nin:o(t,"nin").split(",").map(s=>n(s))};if(t.startsWith("any(")){const s=o(t,"any"),[r,...i]=s.split("=");return{$elemMatch:i.length?W({[r]:i.join("=")}):$(s)}}else if(t.startsWith("reg(")){const[s,r]=o(t,"reg").split("...[");return{$regex:new RegExp(s,r==null?void 0:r.slice(0,-1))}}else if(t.startsWith("exists("))return{$exists:n(t,"exists")==="true"};return t==="true"||t==="false"?t==="true":t},"mapValue"),W=p((t={})=>(t&&Object.keys(t).forEach(s=>{var i;const r=t[s];if(h.includes(s))t[`$${s}`]=r.split(",").map(e=>{const[c,...g]=e.split("=");return{[c]:$(g.join("="))}}),delete t[s];else{const e=h.find(c=>r.startsWith(`${c}(`));if(e){const c=(i=n(r,e))==null?void 0:i.split(",");t[`$${e}`]=c.map(g=>({[s]:$(g)})),delete t[s]}else t[s]=$(r)}}),t),"mapFilters");0&&(module.exports={mapFilters,mapValue});
//# sourceMappingURL=utils.js.map