mongoose-smart-query 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -18,6 +18,7 @@ interface PluginOptions {
|
|
|
18
18
|
defaultLimit?: number;
|
|
19
19
|
/**
|
|
20
20
|
* What fields to look for when making a query. Default: `''`.
|
|
21
|
+
* @deprecated Use `defaultFields` instead.
|
|
21
22
|
*/
|
|
22
23
|
fieldsForDefaultQuery?: string;
|
|
23
24
|
/**
|
|
@@ -38,6 +39,7 @@ interface PluginOptions {
|
|
|
38
39
|
sortQueryName?: string;
|
|
39
40
|
/**
|
|
40
41
|
* Key for search. Default: `'$q'`.
|
|
42
|
+
* @deprecated `queryName` is deprecated, use `searchQueryName` instead.
|
|
41
43
|
*/
|
|
42
44
|
queryName?: string;
|
|
43
45
|
/**
|
|
@@ -52,6 +54,14 @@ interface PluginOptions {
|
|
|
52
54
|
* Get all fields by default
|
|
53
55
|
*/
|
|
54
56
|
getAllFieldsByDefault?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Fields to search by default
|
|
59
|
+
*/
|
|
60
|
+
fieldsForDefaultSearch?: string[] | string;
|
|
61
|
+
/**
|
|
62
|
+
* Key for search. Default: `'$search'`.
|
|
63
|
+
*/
|
|
64
|
+
searchQueryName?: string;
|
|
55
65
|
}
|
|
56
66
|
interface TObject {
|
|
57
67
|
[value: string]: any;
|
|
@@ -71,5 +81,5 @@ export declare function stringToQuery(query?: string, value?: string): object;
|
|
|
71
81
|
* @returns The object without the keys
|
|
72
82
|
*/
|
|
73
83
|
export declare function removeKeys(initial: TObject, toRemove: TObject): TObject;
|
|
74
|
-
export default function (schema: Schema, { protectedFields, defaultFields, defaultSort, defaultLimit, pageQueryName, limitQueryName, fieldsQueryName, sortQueryName, queryName, unwindName, fieldsForDefaultQuery, allFieldsQueryName, getAllFieldsByDefault, }: PluginOptions): void;
|
|
84
|
+
export default function (schema: Schema, { protectedFields, defaultFields, defaultSort, defaultLimit, pageQueryName, limitQueryName, fieldsQueryName, sortQueryName, queryName, unwindName, fieldsForDefaultQuery, allFieldsQueryName, fieldsForDefaultSearch, searchQueryName, getAllFieldsByDefault, }: PluginOptions): void;
|
|
75
85
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,a,s,c){return new(s=s||Promise)(function(r,t){function n(e){try{o(c.next(e))}catch(e){t(e)}}function i(e){try{o(c.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?r(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(n,i)}o((c=c.apply(e,a||[])).next())})},__generator=this&&this.__generator||function(n,i){var o,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=e(0),u.throw=e(1),u.return=e(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function e(r){return function(e){var t=[r,e];if(o)throw new TypeError("Generator is already executing.");for(;c=u&&t[u=0]?0:c;)try{if(o=1,a&&(s=2&t[0]?a.return:t[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,t[1])).done)return s;switch(a=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,a=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))c.label=t[1];else if(6===t[0]&&c.label<s[1])c.label=s[1],s=t;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(t)}}t=i.call(n,c)}catch(e){t=[6,e],a=0}finally{o=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},__spreadArray=this&&this.__spreadArray||function(e,t,r){if(r||2===arguments.length)for(var n,i=0,o=t.length;i<o;i++)!n&&i in t||((n=n||Array.prototype.slice.call(t,0,i))[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))},mongoose_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.stringToQuery=stringToQuery,exports.removeKeys=removeKeys,exports.default=default_1,require("mongoose")),reemplazarSubdoc=function(e,t,r){var n,i;r&&(n=e,(i=t.split(".")).forEach(function(e,t){t===i.length-1?null!=n&&n[e]&&(n[e]=r):n=null==n?void 0:n[e]}))},getCampo=function(e,t){var r=e;return t.split(".").forEach(function(e){return r=null==r?void 0:r[e]}),r};function stringToQuery(e,o){void 0===o&&(o="1");e=(e=void 0===e?"":e).replace(/(})(?!$| *})|([\w.]+)(?= *{)|([\w.]+)(?=$| *})|([\w.]+)/g,function(e,t,r,n,i){return t?t+",":r?'"'.concat(r,'":'):n?'"'.concat(n,'": ').concat(o):i?'"'.concat(i,'": ').concat(o,","):""});return JSON.parse("{ ".concat(e," }"))}function parseValue(e,t){switch(t){case"ObjectID":case"ObjectId":return new mongoose_1.Types.ObjectId(e);case"Date":return new Date(e);case"Number":return Number(e);case"Boolean":return"boolean"==typeof e?e:"true"===e;default:return e}}function removeKeys(e,t){var r,n=__assign({},e);for(r in t)!n[r]||"object"==typeof t[r]&&(n[r]=removeKeys(n[r],t[r]),0!==Object.keys(n[r]).length)||delete n[r];return n}function default_1(j,e){function $(i,o){function a(t,e,r){var n=s.find(function(e){return e.field===t});n?n.project=__assign(__assign({},n.project),r):s.push({field:t,from:e,project:r})}(o=void 0===o?"":o)&&(o+=".");var e,s=[];for(e in i=void 0===i?{}:i)(e=>{var t,r,n;if("object"!=typeof i[e])return(e.includes(".")||o)&&(t=e.split(/\.(.+)/),r=j.path(o+t[0]))&&r.options.ref&&((n=s.find(function(e){return e.field===t[0]}))?n.project[t[1]]=1:s.push({field:t[0],from:r.options.ref,project:((n={})[t[1]]=1,n)}));(r=j.path(o+e))&&r.options.ref?a(o+e,r.options.ref,i[e]):$(i[e],o+e).forEach(function(e){a(e.field,e.from,e.project)})})(e);return s}var t=e.protectedFields,b=void 0===t?"":t,t=e.defaultFields,O=void 0===t?"_id":t,t=e.defaultSort,x=void 0===t?"-_id":t,t=e.defaultLimit,A=void 0===t?20:t,t=e.pageQueryName,Q=void 0===t?"$page":t,t=e.limitQueryName,k=void 0===t?"$limit":t,t=e.fieldsQueryName,S=void 0===t?"$fields":t,t=e.sortQueryName,N=void 0===t?"$sort":t,t=e.queryName,T=void 0===t?"$q":t,t=e.unwindName,I=void 0===t?"$unwind":t,t=e.fieldsForDefaultQuery,P=void 0===t?"":t,t=e.allFieldsQueryName,E=void 0===t?"$getAllFields":t,t=e.getAllFieldsByDefault,C=void 0!==t&&t,F=stringToQuery(b);j.statics.smartQuery=function(){return __awaiter(this,arguments,void 0,function(t){var r,n,i,o,a,s,c,u,l,f,p,d,_;return void 0===t&&(t={}),__generator(this,function(e){switch(e.label){case 0:return[4,this.__smartQueryGetPipeline(__assign({},t))];case 1:return r=e.sent(),n=r.pipeline,i=r.lookupsConfirmados,o=t.business?{business:new mongoose_1.Types.ObjectId(t.business)}:{},[4,this.aggregate(n)];case 2:return a=e.sent(),[4,Promise.all(i.map(function(r){var e=a.reduce(function(e,t){t=getCampo(t,r.field);return t&&e.push(t),e},[]);return 0!==e.length?mongoose_1.connection.collection(r.from).find(__assign({_id:{$in:e}},o)).project(r.project).toArray():[]}))];case 3:for(c in s=e.sent(),i)for(u=s[c],l=i[c],f=function(e){var t=getCampo(e,l.field);t&&reemplazarSubdoc(e,l.field,u.find(function(e){return e._id.equals(t)}))},p=0,d=a;p<d.length;p++)_=d[p],f(_);return[2,a]}})})},j.statics.smartCount=function(){return __awaiter(this,arguments,void 0,function(t){var r;return void 0===t&&(t={}),__generator(this,function(e){switch(e.label){case 0:return[4,this.__smartQueryGetPipeline(__assign({},t),!0)];case 1:return r=e.sent().pipeline,[4,this.aggregate(r)];case 2:return[2,0===(r=e.sent()).length?0:r[0].size]}})})},j.statics.__smartQueryGetPipeline=function(e){return __awaiter(this,arguments,void 0,function(w,s){var p,c,u,l,d,f,_,h,g,v,y,m=this;return void 0===s&&(s=!1),__generator(this,function(e){switch(e.label){case 0:return p=w.business?{business:new mongoose_1.Types.ObjectId(w.business)}:{},c=JSON.parse(JSON.stringify(w)),u=parseInt(w[Q])||1,l=parseInt(w[k])||A,d=function(){return __awaiter(m,void 0,void 0,function(){var m,$,b,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:for(r in m={},$={},b=[],t=function(e){var t,r=j.path(e);if(!r&&!e.includes("."))return"continue";var n,i,o=e,a=void 0,s=w[o],e=(e.includes(".")&&(p=(e=e.split("."))[0],e=e[1],n=j.path(p))&&n.options.ref&&(o=e,$[a=p]={collection:n.options.ref,$match:{}}),a?$[a].$match:m),c=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,u=[];if("string"==typeof s){var l={},f=s,p=f.startsWith("{$or}");for(p&&(f=f.replace("{$or}",""));null!==(i=c.exec(f));)u.push([i[0],i[1],i[2]]);for(var d=0,_=u;d<_.length;d++){var h=_[d],g=h[1],v=h[2];switch(g){case"$exists":l[o]={$exists:"false"!==v};break;case"$includes":l[o]={$regex:RegExp(v.replace(/[^\w]/g,"."),"i")};break;case"$in":case"$nin":var y=v.split(",").map(function(e){return parseValue(e.trim(),null==r?void 0:r.instance)});l[o]=((t={})[g]=y,t);break;default:y=parseValue(v,null==r?void 0:r.instance);g?"object"==typeof l[o]?l[o][g]=y:l[o]=((t={})[g]=y,t):"string"==typeof v&&v.includes("$exists")?l[o]={$exists:!0,$ne:[]}:l[o]=y}}p?b.push(l):Object.assign(e,l)}else e[o]=parseValue(s,null==r?void 0:r.instance)},w)t(r);return 0!==b.length&&(m.$or=b),0===Object.keys($).length?[3,2]:[4,Promise.all(Object.entries($).map(function(e){return __awaiter(n,[e],void 0,function(e){var t,r=e[0],n=e[1];return __generator(this,function(e){switch(e.label){case 0:return[4,mongoose_1.connection.collection(n.collection).find(__assign(__assign({},n.$match),p)).project({_id:1}).toArray()];case 1:return t=e.sent(),m[r]={$in:t.map(function(e){return e._id})},[2]}})})}))];case 1:e.sent(),e.label=2;case 2:return[2,m]}})})},f=function(){return w[I]?[{$unwind:{path:"$".concat(w[I]),preserveNullAndEmptyArrays:!0}}]:[]},[4,function(){return __awaiter(m,void 0,void 0,function(){var o,a,s,c,t,r,n,i,u,l,f=this;return __generator(this,function(e){switch(e.label){case 0:if(o={},a={},!w[T]||!P)return[3,2];for(l=P.split(" "),u=w[T].replace(/[()[\\\]]/g,".").replace(/[+]/g,"\\+").replace(/[*]/g,"\\*"),s=u.split(" "),c={$regex:RegExp(u,"i")},t=function(r){var e,t,n,i;j.path(r)?(o.$or||(o.$or=[]),0===s.length?o.$or.push(((n={})[r]=c,n)):o.$or.push({$and:s.map(function(e){var t={};return t[r]={$regex:RegExp(e,"i")},t})})):r.includes(".")&&(t=(n=r.split("."))[0],n=n[1],i=j.path(t))&&i.options.ref&&(a[t]?a[t].$match.$or.push(((e={})[n]=c,e)):a[t]={collection:i.options.ref,$match:{$or:[((e={})[n]=c,e)]}})},r=0,n=l;r<n.length;r++)i=n[r],t(i);return 0===Object.keys(a).length?[3,2]:[4,Promise.all(Object.entries(a).map(function(e){return __awaiter(f,[e],void 0,function(e){var t,r,n=e[0],i=e[1];return __generator(this,function(e){switch(e.label){case 0:return[4,mongoose_1.connection.collection(i.collection).find(__assign(__assign({},i.$match),p)).project({_id:1}).toArray()];case 1:return t=e.sent(),(t=t.map(function(e){return e._id})).length&&o.$or.push(((r={})[n]={$in:t},r)),[2]}})})}))];case 1:e.sent(),e.label=2;case 2:return[4,d()];case 3:return u=e.sent(),(l=u.$or)&&(delete u.$or,Array.isArray(o.$or)?o.$or=o.$or.concat(l):o.$or=l),[2,__assign(__assign({},o),u)]}})})}()];case 1:if(y=e.sent(),!(a=w)[S]&&O&&(a[S]=O),_=removeKeys(stringToQuery(a[S]),F),h=$(_),s)g=__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(y).length?[{$match:y}]:[],!0),f(),!0),[{$count:"size"}],!1);else if(v=(()=>{if(!w[N]){if(!x)return{};w[N]=x}for(var e=/(-)?([\w.]+)/g,t={},r={};null!==(i=e.exec(w[N]));){var n=i[1],i=i[2];!!j.path(i)?t[i]=n?-1:1:r[i]=n?-1:1}return{$localSort:0!==Object.keys(t).length?t:void 0,$foreignSort:0!==Object.keys(r).length?r:void 0}})(),g=__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(y).length?[{$match:y}]:[],!0),v.$localSort?[{$sort:v.$localSort}]:[],!0),f(),!0),[{$skip:(u-1)*l},{$limit:l}],!1),!c[S]&&!0===C||"true"===(null==(y=w[E])?void 0:y.toString()))b&&g.push({$project:stringToQuery(b,"0")});else{var t=_;void 0===t&&(t={});for(var r=0,n=h;r<n.length;r++){var i=n[r];if(i.field.includes(".")||t[i.field])reemplazarSubdoc(t,i.field,1);else for(var o in t)o.startsWith(i.field+".")&&(delete t[o],t[i.field]=1)}g.push({$project:_})}return[2,{pipeline:g,lookupsConfirmados:h}]}var a})})}}
|
|
1
|
+
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var i in r=arguments[t])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,a,s,c){return new(s=s||Promise)(function(t,r){function n(e){try{o(c.next(e))}catch(e){r(e)}}function i(e){try{o(c.throw(e))}catch(e){r(e)}}function o(e){var r;e.done?t(e.value):((r=e.value)instanceof s?r:new s(function(e){e(r)})).then(n,i)}o((c=c.apply(e,a||[])).next())})},__generator=this&&this.__generator||function(n,i){var o,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=e(0),u.throw=e(1),u.return=e(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function e(t){return function(e){var r=[t,e];if(o)throw new TypeError("Generator is already executing.");for(;c=u&&r[u=0]?0:c;)try{if(o=1,a&&(s=2&r[0]?a.return:r[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,r[1])).done)return s;switch(a=0,(r=s?[2&r[0],s.value]:r)[0]){case 0:case 1:s=r;break;case 4:return c.label++,{value:r[1],done:!1};case 5:c.label++,a=r[1],r=[0];continue;case 7:r=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===r[0]||2===r[0])){c=0;continue}if(3===r[0]&&(!s||r[1]>s[0]&&r[1]<s[3]))c.label=r[1];else if(6===r[0]&&c.label<s[1])c.label=s[1],s=r;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(r)}}r=i.call(n,c)}catch(e){r=[6,e],a=0}finally{o=s=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}}},__spreadArray=this&&this.__spreadArray||function(e,r,t){if(t||2===arguments.length)for(var n,i=0,o=r.length;i<o;i++)!n&&i in r||((n=n||Array.prototype.slice.call(r,0,i))[i]=r[i]);return e.concat(n||Array.prototype.slice.call(r))},mongoose_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.stringToQuery=stringToQuery,exports.removeKeys=removeKeys,exports.default=default_1,require("mongoose")),reemplazarSubdoc=function(e,r,t){var n,i;t&&(n=e,(i=r.split(".")).forEach(function(e,r){r===i.length-1?null!=n&&n[e]&&(n[e]=t):n=null==n?void 0:n[e]}))},getCampo=function(e,r){var t=e;return r.split(".").forEach(function(e){return t=null==t?void 0:t[e]}),t};function stringToQuery(e,o){void 0===o&&(o="1");e=(e=void 0===e?"":e).replace(/(})(?!$| *})|([\w.]+)(?= *{)|([\w.]+)(?=$| *})|([\w.]+)/g,function(e,r,t,n,i){return r?r+",":t?'"'.concat(t,'":'):n?'"'.concat(n,'": ').concat(o):i?'"'.concat(i,'": ').concat(o,","):""});return JSON.parse("{ ".concat(e," }"))}function parseValue(e,r){switch(r){case"ObjectID":case"ObjectId":return new mongoose_1.Types.ObjectId(e);case"Date":return new Date(e);case"Number":return Number(e);case"Boolean":return"boolean"==typeof e?e:"true"===e;default:return e}}function removeKeys(e,r){var t,n=__assign({},e);for(t in r)!n[t]||"object"==typeof r[t]&&(n[t]=removeKeys(n[t],r[t]),0!==Object.keys(n[t]).length)||delete n[t];return n}function default_1(j,e){function $(i,o){function a(r,e,t){var n=s.find(function(e){return e.field===r});n?n.project=__assign(__assign({},n.project),t):s.push({field:r,from:e,project:t})}(o=void 0===o?"":o)&&(o+=".");var e,s=[];for(e in i=void 0===i?{}:i)(e=>{var r,t,n;if("object"!=typeof i[e])return(e.includes(".")||o)&&(r=e.split(/\.(.+)/),t=j.path(o+r[0]))&&t.options.ref&&((n=s.find(function(e){return e.field===r[0]}))?n.project[r[1]]=1:s.push({field:r[0],from:t.options.ref,project:((n={})[r[1]]=1,n)}));(t=j.path(o+e))&&t.options.ref?a(o+e,t.options.ref,i[e]):$(i[e],o+e).forEach(function(e){a(e.field,e.from,e.project)})})(e);return s}var r=e.protectedFields,b=void 0===r?"":r,r=e.defaultFields,O=void 0===r?"_id":r,r=e.defaultSort,x=void 0===r?"-_id":r,r=e.defaultLimit,A=void 0===r?20:r,r=e.pageQueryName,Q=void 0===r?"$page":r,r=e.limitQueryName,S=void 0===r?"$limit":r,r=e.fieldsQueryName,k=void 0===r?"$fields":r,r=e.sortQueryName,N=void 0===r?"$sort":r,r=e.queryName,T=void 0===r?"$q":r,r=e.unwindName,I=void 0===r?"$unwind":r,r=e.fieldsForDefaultQuery,P=void 0===r?"":r,r=e.allFieldsQueryName,E=void 0===r?"$getAllFields":r,r=e.fieldsForDefaultSearch,C=void 0===r?[]:r,r=e.searchQueryName,F=void 0===r?"$search":r,r=e.getAllFieldsByDefault,z=void 0!==r&&r,D=stringToQuery(b);j.statics.smartQuery=function(){return __awaiter(this,arguments,void 0,function(r){var t,n,i,o,a,s,c,u,l,f,p,d,_;return void 0===r&&(r={}),__generator(this,function(e){switch(e.label){case 0:return[4,this.__smartQueryGetPipeline(__assign({},r))];case 1:return t=e.sent(),n=t.pipeline,i=t.lookupsConfirmados,o=r.business?{business:new mongoose_1.Types.ObjectId(r.business)}:{},[4,this.aggregate(n)];case 2:return a=e.sent(),[4,Promise.all(i.map(function(t){var e=a.reduce(function(e,r){r=getCampo(r,t.field);return r&&e.push(r),e},[]);return 0!==e.length?mongoose_1.connection.collection(t.from).find(__assign({_id:{$in:e}},o)).project(t.project).toArray():[]}))];case 3:for(c in s=e.sent(),i)for(u=s[c],l=i[c],f=function(e){var r=getCampo(e,l.field);r&&reemplazarSubdoc(e,l.field,u.find(function(e){return e._id.equals(r)}))},p=0,d=a;p<d.length;p++)_=d[p],f(_);return[2,a]}})})},j.statics.smartCount=function(){return __awaiter(this,arguments,void 0,function(r){var t;return void 0===r&&(r={}),__generator(this,function(e){switch(e.label){case 0:return[4,this.__smartQueryGetPipeline(__assign({},r),!0)];case 1:return t=e.sent().pipeline,[4,this.aggregate(t)];case 2:return[2,0===(t=e.sent()).length?0:t[0].size]}})})},j.statics.__smartQueryGetPipeline=function(e){return __awaiter(this,arguments,void 0,function(w,s){var _,c,u,l,h,f,p,d,g,v,y,m=this;return void 0===s&&(s=!1),__generator(this,function(e){switch(e.label){case 0:return _=w.business?{business:new mongoose_1.Types.ObjectId(w.business)}:{},c=JSON.parse(JSON.stringify(w)),u=parseInt(w[Q])||1,l=parseInt(w[S])||A,h=function(){return __awaiter(m,void 0,void 0,function(){var m,$,b,r,t,n=this;return __generator(this,function(e){switch(e.label){case 0:for(t in m={},$={},b=[],r=function(e){var r,t=j.path(e);if(!t&&!e.includes("."))return"continue";var n,i,o=e,a=void 0,s=w[o],e=(e.includes(".")&&(p=(e=e.split("."))[0],e=e[1],n=j.path(p))&&n.options.ref&&(o=e,$[a=p]={collection:n.options.ref,$match:{}}),a?$[a].$match:m),c=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,u=[];if("string"==typeof s){var l={},f=s,p=f.startsWith("{$or}");for(p&&(f=f.replace("{$or}",""));null!==(i=c.exec(f));)u.push([i[0],i[1],i[2]]);for(var d=0,_=u;d<_.length;d++){var h=_[d],g=h[1],v=h[2];switch(g){case"$exists":l[o]={$exists:"false"!==v};break;case"$includes":l[o]={$regex:RegExp(v.replace(/[^\w]/g,"."),"i")};break;case"$in":case"$nin":var y=v.split(",").map(function(e){return parseValue(e.trim(),null==t?void 0:t.instance)});l[o]=((r={})[g]=y,r);break;default:y=parseValue(v,null==t?void 0:t.instance);g?"object"==typeof l[o]?l[o][g]=y:l[o]=((r={})[g]=y,r):"string"==typeof v&&v.includes("$exists")?l[o]={$exists:!0,$ne:[]}:l[o]=y}}p?b.push(l):Object.assign(e,l)}else e[o]=parseValue(s,null==t?void 0:t.instance)},w)r(t);return 0!==b.length&&(m.$or=b),0===Object.keys($).length?[3,2]:[4,Promise.all(Object.entries($).map(function(e){return __awaiter(n,[e],void 0,function(e){var r,t=e[0],n=e[1];return __generator(this,function(e){switch(e.label){case 0:return[4,mongoose_1.connection.collection(n.collection).find(__assign(__assign({},n.$match),_)).project({_id:1}).toArray()];case 1:return r=e.sent(),m[t]={$in:r.map(function(e){return e._id})},[2]}})})}))];case 1:e.sent(),e.label=2;case 2:return[2,m]}})})},f=function(){return w[I]?[{$unwind:{path:"$".concat(w[I]),preserveNullAndEmptyArrays:!0}}]:[]},[4,function(){return __awaiter(m,void 0,void 0,function(){var o,a,s,c,r,t,n,i,u,l,f,p,d=this;return __generator(this,function(e){switch(e.label){case 0:if(o={},a={},!w[T]||!P)return[3,2];for(l=P.split(" "),u=w[T].replace(/[()[\\\]]/g,".").replace(/[+]/g,"\\+").replace(/[*]/g,"\\*"),s=u.split(" "),c={$regex:RegExp(u,"i")},r=function(t){var e,r,n,i;j.path(t)?(o.$or||(o.$or=[]),0===s.length?o.$or.push(((n={})[t]=c,n)):o.$or.push({$and:s.map(function(e){var r={};return r[t]={$regex:RegExp(e,"i")},r})})):t.includes(".")&&(r=(n=t.split("."))[0],n=n[1],i=j.path(r))&&i.options.ref&&(a[r]?a[r].$match.$or.push(((e={})[n]=c,e)):a[r]={collection:i.options.ref,$match:{$or:[((e={})[n]=c,e)]}})},t=0,n=l;t<n.length;t++)i=n[t],r(i);return 0===Object.keys(a).length?[3,2]:[4,Promise.all(Object.entries(a).map(function(e){return __awaiter(d,[e],void 0,function(e){var r,t,n=e[0],i=e[1];return __generator(this,function(e){switch(e.label){case 0:return[4,mongoose_1.connection.collection(i.collection).find(__assign(__assign({},i.$match),_)).project({_id:1}).toArray()];case 1:return r=e.sent(),(r=r.map(function(e){return e._id})).length&&o.$or.push(((t={})[n]={$in:r},t)),[2]}})})}))];case 1:e.sent(),e.label=2;case 2:return w[F]&&0!==C.length&&(u=Array.isArray(C)?C:[C],1===(l=u.map(function(e){var r={};return r[e]={$regex:w[F].toLowerCase().replace(/[^a-z0-9]/g,"")},r})).length?o=l[0]:o.$or=l),[4,h()];case 3:return f=e.sent(),(p=f.$or)&&(delete f.$or,Array.isArray(o.$or)?o.$or=o.$or.concat(p):o.$or=p),[2,__assign(__assign({},o),f)]}})})}()];case 1:if(y=e.sent(),!(a=w)[k]&&O&&(a[k]=O),p=removeKeys(stringToQuery(a[k]),D),d=$(p),s)g=__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(y).length?[{$match:y}]:[],!0),f(),!0),[{$count:"size"}],!1);else if(v=(()=>{if(!w[N]){if(!x)return{};w[N]=x}for(var e=/(-)?([\w.]+)/g,r={},t={};null!==(i=e.exec(w[N]));){var n=i[1],i=i[2];!!j.path(i)?r[i]=n?-1:1:t[i]=n?-1:1}return{$localSort:0!==Object.keys(r).length?r:void 0,$foreignSort:0!==Object.keys(t).length?t:void 0}})(),g=__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(y).length?[{$match:y}]:[],!0),v.$localSort?[{$sort:v.$localSort}]:[],!0),f(),!0),[{$skip:(u-1)*l},{$limit:l}],!1),!c[k]&&!0===z||"true"===(null==(y=w[E])?void 0:y.toString()))b&&g.push({$project:stringToQuery(b,"0")});else{var r=p;void 0===r&&(r={});for(var t=0,n=d;t<n.length;t++){var i=n[t];if(i.field.includes(".")||r[i.field])reemplazarSubdoc(r,i.field,1);else for(var o in r)o.startsWith(i.field+".")&&(delete r[o],r[i.field]=1)}g.push({$project:p})}return[2,{pipeline:g,lookupsConfirmados:d}]}var a})})}}
|