mongoose-smart-query 0.1.3 → 0.1.5
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,4 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
1
2
|
interface PluginOptions {
|
|
2
3
|
/**
|
|
3
4
|
* Fields that should not be included in the query results. Default: `''`.
|
|
@@ -77,5 +78,5 @@ export declare function stringToQuery(query?: string, value?: string): object;
|
|
|
77
78
|
* @returns The object without the keys
|
|
78
79
|
*/
|
|
79
80
|
export declare function removeKeys(initial: TObject, toRemove: TObject): TObject;
|
|
80
|
-
export default function (schema:
|
|
81
|
+
export default function (schema: Schema, { protectedFields, defaultFields, defaultSort, defaultLimit, pageQueryName, limitQueryName, fieldsQueryName, sortQueryName, queryName, unwindName, fieldsForDefaultQuery, allFieldsQueryName, getAllFieldsByDefault, }: PluginOptions): void;
|
|
81
82
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t
|
|
1
|
+
"use strict";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 o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,i,s,c){return new(s=s||Promise)(function(t,r){function n(e){try{a(c.next(e))}catch(e){r(e)}}function o(e){try{a(c.throw(e))}catch(e){r(e)}}function a(e){var r;e.done?t(e.value):((r=e.value)instanceof s?r:new s(function(e){e(r)})).then(n,o)}a((c=c.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(n,o){var a,i,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[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(t){return function(e){var r=[t,e];if(a)throw new TypeError("Generator is already executing.");for(;c=u&&r[u=0]?0:c;)try{if(a=1,i&&(s=2&r[0]?i.return:r[0]?i.throw||((s=i.return)&&s.call(i),0):i.next)&&!(s=s.call(i,r[1])).done)return s;switch(i=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++,i=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=o.call(n,c)}catch(e){r=[6,e],i=0}finally{a=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,o=0,a=r.length;o<a;o++)!n&&o in r||((n=n||Array.prototype.slice.call(r,0,o))[o]=r[o]);return e.concat(n||Array.prototype.slice.call(r))},mongoose_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.removeKeys=exports.stringToQuery=exports.getListOfPossibleLookups=void 0,require("mongoose"));function getListOfPossibleLookups(t){var e,n=[];for(e in t)!function(r){var e;if("object"!=typeof t[r])return r.includes(".")&&(e=r.split(/\.(.+)/),n.push(e[0]));n.push(r),e=getListOfPossibleLookups(t[r]),n=n.concat(e.map(function(e){return r+"."+e}))}(e);return n}function stringToQuery(e,a){void 0===a&&(a="1");e=(e=void 0===e?"":e).replace(/(})(?!$| *})|([\w.]+)(?= *{)|([\w.]+)(?=$| *})|([\w.]+)/g,function(e,r,t,n,o){return r?r+",":t?'"'.concat(t,'":'):n?'"'.concat(n,'": ').concat(a):o?'"'.concat(o,'": ').concat(a,","):""});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(b,e){var r=e.protectedFields,p=void 0===r?"":r,r=e.defaultFields,f=void 0===r?"_id":r,r=e.defaultSort,y=void 0===r?"-_id":r,r=e.defaultLimit,h=void 0===r?20:r,r=e.pageQueryName,d=void 0===r?"$page":r,r=e.limitQueryName,g=void 0===r?"$limit":r,r=e.fieldsQueryName,$=void 0===r?"$fields":r,r=e.sortQueryName,_=void 0===r?"$sort":r,r=e.queryName,m=void 0===r?"$q":r,r=e.unwindName,w=void 0===r?"$unwind":r,r=e.fieldsForDefaultQuery,A=void 0===r?"":r,r=e.allFieldsQueryName,O=void 0===r?"$getAllFields":r,r=e.getAllFieldsByDefault,x=void 0!==r&&r,j=stringToQuery(p);b.statics.smartQuery=function(e){e=this.__smartQueryGetPipeline(__assign({},e=void 0===e?{}:e));return this.aggregate(e)},b.statics.smartCount=function(t){return void 0===t&&(t={}),__awaiter(this,void 0,void 0,function(){var r;return __generator(this,function(e){switch(e.label){case 0:return r=this.__smartQueryGetPipeline(__assign({},t),!0),[4,this.aggregate(r)];case 1:return[2,0===(r=e.sent()).length?0:r[0].size]}})})},b.statics.__smartQueryGetPipeline=function(v,e){void 0===e&&(e=!1);var r=JSON.parse(JSON.stringify(v)),t=parseInt(v[d])||1,n=parseInt(v[g])||h,u=function(){var e,g={},$={},_=[];for(e in v)!function(e){var r,t=b.path(e);if(!t&&!e.includes("."))return;var n,o=t?g:$,a=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,i=[];if("string"==typeof v[e]){var s={},c=v[e],u=c.startsWith("{$or}");for(u&&(c=c.replace("{$or}",""));null!==(n=a.exec(c));)i.push([n[0],n[1],n[2]]);for(var l=0,p=i;l<p.length;l++){var f=p[l],y=f[1],h=f[2];switch(y){case"$exists":s[e]={$exists:"false"!==h};break;case"$includes":s[e]={$regex:RegExp(h.replace(/[^\w]/g,"."),"i")};break;case"$in":case"$nin":var d=h.split(",").map(function(e){return parseValue(e.trim(),null==t?void 0:t.instance)});s[e]=((r={})[y]=d,r);break;default:d=parseValue(h,null==t?void 0:t.instance);y?"object"==typeof s[e]?s[e][y]=d:s[e]=((r={})[y]=d,r):"string"==typeof h&&h.includes("$exists")?s[e]={$exists:!0,$ne:[]}:s[e]=d}}u?_.push(s):Object.assign(o,s)}else o[e]=parseValue(v[e],null==t?void 0:t.instance)}(e);return 0!==_.length&&(g.$or=_),{$localMatch:g,$foreignMatch:$}};function o(){return v[w]?[{$unwind:{path:"$".concat(v[w]),preserveNullAndEmptyArrays:!0}}]:[]}var a,i,s=function(){var e,r={},t={};if(v[m]&&A)for(var n=A.split(" "),o=v[m].replace(/[()[\\\]*]/g,"."),a={$regex:RegExp(o,"i")},i=0,s=n;i<s.length;i++){var c=s[i];!!b.path(c)?(t.$or||(t.$or=[]),t.$or.push(((e={})["".concat(c)]=a,e))):(t.$or||(t.$or=[]),t.$or.push(((e={})["".concat(c)]=a,e)))}o=u(),n=o.$localMatch.$or||o.$foreignMatch.$or;return n&&(delete o.$localMatch.$or,delete o.$foreignMatch.$or,Array.isArray(r.$or)?r.$or=r.$or.concat(n):Array.isArray(t.$or)?t.$or=t.$or.concat(n):r.$or=n),{$queryMatch:__assign(__assign({},r),o.$localMatch),$foreignMatch:__assign(__assign({},t),o.$foreignMatch)}}(),c=(!(c=v)[$]&&f&&(c[$]=f),removeKeys(stringToQuery(c[$]),j)),l=(a=[],getListOfPossibleLookups(c).forEach(function(e){var r=b.path(e);r&&r.options.ref&&(r=r.options.ref,a.push({$lookup:{from:r,localField:e,foreignField:"_id",as:e}},{$unwind:{path:"$".concat(e),preserveNullAndEmptyArrays:!0}}))}),a);return e?i=__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(s.$queryMatch).length?[{$match:s.$queryMatch}]:[],!0),l,!0),o(),!0),0!==Object.keys(s.$foreignMatch).length?[{$match:s.$foreignMatch}]:[],!0),[{$count:"size"}],!1):(e=function(){if(!v[_]){if(!y)return{};v[_]=y}for(var e=/(-)?([\w.]+)/g,r={},t={};null!==(o=e.exec(v[_]));){var n=o[1],o=o[2];!!b.path(o)?r[o]=n?-1:1:t[o]=n?-1:1}return{$localSort:0!==Object.keys(r).length?r:void 0,$foreignSort:0!==Object.keys(t).length?t:void 0}}(),i=__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(s.$queryMatch).length?[{$match:s.$queryMatch}]:[],!0),e.$localSort?[{$sort:e.$localSort}]:[],!0),l,!0),o(),!0),0!==Object.keys(s.$foreignMatch).length?[{$match:s.$foreignMatch}]:[],!0),[{$skip:(t-1)*n},{$limit:n}],!1),!r[$]&&!0===x||"true"===(null==(e=v[O])?void 0:e.toString())?p&&i.push({$project:stringToQuery(p,"0")}):i.push({$project:c})),i}}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.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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,20 @@
|
|
|
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": "^5.
|
|
36
|
-
"@typescript-eslint/parser": "^5.
|
|
37
|
-
"eslint": "^8.
|
|
38
|
-
"eslint-config-prettier": "^8.
|
|
34
|
+
"@types/jest": "^29.5.1",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^5.59.6",
|
|
36
|
+
"@typescript-eslint/parser": "^5.59.6",
|
|
37
|
+
"eslint": "^8.40.0",
|
|
38
|
+
"eslint-config-prettier": "^8.8.0",
|
|
39
39
|
"eslint-config-standard": "^17.0.0",
|
|
40
40
|
"eslint-plugin-import": "^2.27.5",
|
|
41
41
|
"eslint-plugin-jest": "^27.2.1",
|
|
42
42
|
"eslint-plugin-node": "^11.1.0",
|
|
43
43
|
"eslint-plugin-promise": "^6.1.1",
|
|
44
44
|
"jest": "^29.5.0",
|
|
45
|
-
"mongoose": "^7.
|
|
46
|
-
"ts-jest": "^29.0
|
|
47
|
-
"typescript": "^5.0.
|
|
45
|
+
"mongoose": "^7.1.1",
|
|
46
|
+
"ts-jest": "^29.1.0",
|
|
47
|
+
"typescript": "^5.0.4",
|
|
48
48
|
"uglify-js": "^3.17.4"
|
|
49
49
|
}
|
|
50
50
|
}
|