mongoose-smart-query 0.1.6 → 1.0.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.
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
1
25
|
import { Schema } from 'mongoose';
|
|
2
26
|
interface PluginOptions {
|
|
3
27
|
/**
|
|
@@ -62,7 +86,7 @@ interface TObject {
|
|
|
62
86
|
* @param $project The $project's pipeline that can contains possible $lookup.
|
|
63
87
|
* @returns An array with the fields with a possible $lookup.
|
|
64
88
|
*/
|
|
65
|
-
export declare
|
|
89
|
+
export declare const getListOfPossibleLookups: ($project: any) => string[];
|
|
66
90
|
/**
|
|
67
91
|
* Converts a string to object. Example:
|
|
68
92
|
* 'name age friends { name }' => { name: 1, age: 1, friends: { name: 1 } }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r
|
|
1
|
+
"use strict";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,s,a,c){return new(a=a||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 a?t:new a(function(e){e(t)})).then(n,i)}o((c=c.apply(e,s||[])).next())})},__generator=this&&this.__generator||function(n,i){var o,s,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},u={next:e(0),throw:e(1),return:e(2)};return"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,s&&(a=2&t[0]?s.return:t[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,t[1])).done)return a;switch(s=0,(t=a?[2&t[0],a.value]:t)[0]){case 0:case 1:a=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,s=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(a=0<(a=c.trys).length&&a[a.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!a||t[1]>a[0]&&t[1]<a[3]))c.label=t[1];else if(6===t[0]&&c.label<a[1])c.label=a[1],a=t;else{if(!(a&&c.label<a[2])){a[2]&&c.ops.pop(),c.trys.pop();continue}c.label=a[2],c.ops.push(t)}}t=i.call(n,c)}catch(e){t=[6,e],s=0}finally{o=a=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.removeKeys=exports.stringToQuery=exports.getListOfPossibleLookups=void 0,require("mongoose")),getListOfPossibleLookups=function(r){var e,n=[];for(e in r)!function(t){var e;if("object"!=typeof r[t])return t.includes(".")&&(e=t.split(/\.(.+)/),n.push(e[0]));n.push(t),e=(0,exports.getListOfPossibleLookups)(r[t]),n=n.concat(e.map(function(e){return t+"."+e}))}(e);return n};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){var t=e.protectedFields,m=void 0===t?"":t,t=e.defaultFields,$=void 0===t?"_id":t,t=e.defaultSort,O=void 0===t?"-_id":t,t=e.defaultLimit,x=void 0===t?20:t,t=e.pageQueryName,k=void 0===t?"$page":t,t=e.limitQueryName,A=void 0===t?"$limit":t,t=e.fieldsQueryName,Q=void 0===t?"$fields":t,t=e.sortQueryName,P=void 0===t?"$sort":t,t=e.queryName,L=void 0===t?"$q":t,t=e.unwindName,N=void 0===t?"$unwind":t,t=e.fieldsForDefaultQuery,S=void 0===t?"":t,t=e.allFieldsQueryName,T=void 0===t?"$getAllFields":t,t=e.getAllFieldsByDefault,I=void 0!==t&&t,F=stringToQuery(m);j.statics.smartQuery=function(){return __awaiter(this,arguments,void 0,function(t){var r,n,o,i,s,a,c,u;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,o=r.lookupsConfirmados,i=t.business?{business:new mongoose_1.Types.ObjectId(t.business)}:{},[4,this.aggregate(n)];case 2:return s=e.sent(),[4,Promise.all(o.map(function(t){var e=s.filter(function(e){return e[t.field]}).map(function(e){return e[t.field]});return mongoose_1.connection.collection(t.from).find(__assign({_id:{$in:e}},i)).project(t.project).toArray()}))];case 3:for(u in a=e.sent(),c=function(e){for(var r=a[e],n=o[e],t=0,i=s;t<i.length;t++)!function(t){t[n.field]=r.find(function(e){return e._id.equals(t[n.field])})}(i[t])},o)c(u);return[2,s]}})})},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,r){var _,n,i,o,h,s,a,c,u,l,f,p,d,g,y,v,b=this;return void 0===r&&(r=!1),__generator(this,function(e){switch(e.label){case 0:return _=w.business?{business:new mongoose_1.Types.ObjectId(w.business)}:{},n=JSON.parse(JSON.stringify(w)),i=parseInt(w[k])||1,o=parseInt(w[A])||x,h=function(){return __awaiter(b,void 0,void 0,function(){var b,m,$,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:for(r in b={},m={},$=[],t=function(e){var t,r=j.path(e);if(!r&&!e.includes("."))return"continue";var n,i,o=e,s=void 0,a=w[o],e=(e.includes(".")&&(p=(e=e.split("."))[0],e=e[1],n=j.path(p))&&n.options.ref&&(o=e,m[s=p]={collection:n.options.ref,$match:{}}),s?m[s].$match:b),c=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,u=[];if("string"==typeof a){var l={},f=a,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],y=h[2];switch(g){case"$exists":l[o]={$exists:"false"!==y};break;case"$includes":l[o]={$regex:RegExp(y.replace(/[^\w]/g,"."),"i")};break;case"$in":case"$nin":var v=y.split(",").map(function(e){return parseValue(e.trim(),null==r?void 0:r.instance)});l[o]=((t={})[g]=v,t);break;default:v=parseValue(y,null==r?void 0:r.instance);g?"object"==typeof l[o]?l[o][g]=v:l[o]=((t={})[g]=v,t):"string"==typeof y&&y.includes("$exists")?l[o]={$exists:!0,$ne:[]}:l[o]=v}}p?$.push(l):Object.assign(e,l)}else e[o]=parseValue(a,null==r?void 0:r.instance)},w)t(r);return 0!==$.length&&(b.$or=$),0===Object.keys(m).length?[3,2]:[4,Promise.all(Object.entries(m).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),_)).project({_id:1}).toArray()];case 1:return t=e.sent(),b[r]={$in:t.map(function(e){return e._id})},[2]}})})}))];case 1:e.sent(),e.label=2;case 2:return[2,b]}})})},s=function(){return w[N]?[{$unwind:{path:"$".concat(w[N]),preserveNullAndEmptyArrays:!0}}]:[]},[4,function(){return __awaiter(b,void 0,void 0,function(){var o,t,r,n,i,s,a,c,u,l,f,p,d=this;return __generator(this,function(e){switch(e.label){case 0:if(o={},t={},!w[L]||!S)return[3,2];for(l=S.split(" "),u=w[L].replace(/[()[\\\]*]/g,"."),r={$regex:RegExp(u,"i")},n=0,i=l;n<i.length;n++)s=i[n],j.path(s)?(o.$or||(o.$or=[]),o.$or.push(((f={})[s]=r,f))):s.includes(".")&&(f=s.split("."),s=f[0],a=f[1],c=j.path(s))&&c.options.ref&&(t[s]?t[s].$match.$or.push(((p={})[a]=r,p)):t[s]={collection:c.options.ref,$match:{$or:[((p={})[a]=r,p)]}});return 0===Object.keys(t).length?[3,2]:[4,Promise.all(Object.entries(t).map(function(e){return __awaiter(d,[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),_)).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,h()];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(v=e.sent(),!(t=w)[Q]&&$&&(t[Q]=$),a=removeKeys(stringToQuery(t[Q]),F),c=(0,exports.getListOfPossibleLookups)(a).reduce(function(e,r){var t,n=j.path(r);return n&&n.options.ref?(t=Object.keys(a||{}).reduce(function(e,t){return t.startsWith(r+".")?(e[t.split(".")[1]]=1,e):t.startsWith(r)&&"object"==typeof a[r]?a[r]:e},{}),e.concat([{field:r,from:n.options.ref,project:t}])):e},[]),r)u=__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(v).length?[{$match:v}]:[],!0),s(),!0),[{$count:"size"}],!1);else if(l=function(){if(!w[P]){if(!O)return{};w[P]=O}for(var e=/(-)?([\w.]+)/g,t={},r={};null!==(i=e.exec(w[P]));){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}}(),u=__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(v).length?[{$match:v}]:[],!0),l.$localSort?[{$sort:l.$localSort}]:[],!0),s(),!0),[{$skip:(i-1)*o},{$limit:o}],!1),!n[Q]&&!0===I||"true"===(null==(v=w[T])?void 0:v.toString()))m&&u.push({$project:stringToQuery(m,"0")});else{for(f=__assign({},a),p=0,d=c;p<d.length;p++){for(y in g=d[p],f)y.includes(g.field+".")&&delete f[y];f[g.field]=1}u.push({$project:f})}return[2,{pipeline:u,lookupsConfirmados:c}]}var t})})}}exports.getListOfPossibleLookups=getListOfPossibleLookups,exports.stringToQuery=stringToQuery,exports.removeKeys=removeKeys,exports.default=default_1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose-smart-query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Optimize rest requests",
|
|
5
5
|
"main": "./dist/mongoose-smart-query.js",
|
|
6
6
|
"types": "./dist/mongoose-smart-query.d.ts",
|
|
@@ -31,20 +31,22 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/mgyugcha/mongoose-smart-query#readme",
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@types/jest": "^29.5.
|
|
35
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
36
|
-
"@typescript-eslint/parser": "^
|
|
37
|
-
"eslint": "^8.
|
|
38
|
-
"eslint-config-prettier": "^9.
|
|
34
|
+
"@types/jest": "^29.5.12",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^7.5.0",
|
|
36
|
+
"@typescript-eslint/parser": "^7.5.0",
|
|
37
|
+
"eslint": "^8.57.0",
|
|
38
|
+
"eslint-config-prettier": "^9.1.0",
|
|
39
39
|
"eslint-config-standard": "^17.1.0",
|
|
40
|
-
"eslint-plugin-import": "^2.
|
|
41
|
-
"eslint-plugin-jest": "^27.
|
|
40
|
+
"eslint-plugin-import": "^2.29.1",
|
|
41
|
+
"eslint-plugin-jest": "^27.9.0",
|
|
42
42
|
"eslint-plugin-node": "^11.1.0",
|
|
43
43
|
"eslint-plugin-promise": "^6.1.1",
|
|
44
44
|
"jest": "^29.7.0",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"typescript": "^5.2.2",
|
|
45
|
+
"ts-jest": "^29.1.2",
|
|
46
|
+
"typescript": "^5.4.3",
|
|
48
47
|
"uglify-js": "^3.17.4"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"mongoose": "^7"
|
|
49
51
|
}
|
|
50
52
|
}
|