mongoose-smart-query 0.0.14 → 0.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.
package/.eslintrc.json CHANGED
@@ -4,9 +4,7 @@
4
4
  "node": true,
5
5
  "jest/globals": true
6
6
  },
7
- "extends": [
8
- "standard"
9
- ],
7
+ "extends": ["standard", "prettier"],
10
8
  "parser": "@typescript-eslint/parser",
11
9
  "parserOptions": {
12
10
  "ecmaVersion": 12,
@@ -0,0 +1,3 @@
1
+ dist
2
+ node_modules
3
+ *.json
@@ -0,0 +1,19 @@
1
+ {
2
+ "arrowParens": "always",
3
+ "bracketSameLine": false,
4
+ "bracketSpacing": true,
5
+ "embeddedLanguageFormatting": "auto",
6
+ "htmlWhitespaceSensitivity": "css",
7
+ "insertPragma": false,
8
+ "jsxSingleQuote": false,
9
+ "printWidth": 80,
10
+ "proseWrap": "preserve",
11
+ "quoteProps": "as-needed",
12
+ "requirePragma": false,
13
+ "semi": false,
14
+ "singleQuote": true,
15
+ "tabWidth": 2,
16
+ "trailingComma": "all",
17
+ "useTabs": false,
18
+ "vueIndentScriptAndStyle": false
19
+ }
@@ -1 +1 @@
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,a,s,u){return new(s=s||Promise)(function(t,r){function n(e){try{i(u.next(e))}catch(e){r(e)}}function o(e){try{i(u.throw(e))}catch(e){r(e)}}function i(e){var r;e.done?t(e.value):((r=e.value)instanceof s?r:new s(function(e){e(r)})).then(n,o)}i((u=u.apply(e,a||[])).next())})},__generator=this&&this.__generator||function(t,n){var o,i,a,s={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},e={next:r(0),throw:r(1),return:r(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function r(r){return function(e){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,i&&(a=2&r[0]?i.return:r[0]?i.throw||((a=i.return)&&a.call(i),0):i.next)&&!(a=a.call(i,r[1])).done)return a;switch(i=0,(r=a?[2&r[0],a.value]:r)[0]){case 0:case 1:a=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,i=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(a=0<(a=s.trys).length&&a[a.length-1])&&(6===r[0]||2===r[0])){s=0;continue}if(3===r[0]&&(!a||r[1]>a[0]&&r[1]<a[3])){s.label=r[1];break}if(6===r[0]&&s.label<a[1]){s.label=a[1],a=r;break}if(a&&s.label<a[2]){s.label=a[2],s.ops.push(r);break}a[2]&&s.ops.pop(),s.trys.pop();continue}r=n.call(t,s)}catch(e){r=[6,e],i=0}finally{o=a=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,e])}}},__spreadArray=this&&this.__spreadArray||function(e,r){for(var t=0,n=r.length,o=e.length;t<n;t++,o++)e[o]=r[t];return e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.removeKeys=exports.stringToQuery=exports.getListOfPossibleLookups=void 0;var mongoose_1=require("mongoose"),ObjectId=mongoose_1.Types.ObjectId;function getListOfPossibleLookups(t){var e,n=[];for(e in t)!function(r){if("object"!=typeof t[r]){if(r.includes(".")){var e=r.split(/\.(.+)/);return n.push(e[0])}return}n.push(r);e=getListOfPossibleLookups(t[r]);n=n.concat(e.map(function(e){return r+"."+e}))}(e);return n}function stringToQuery(e,i){void 0===i&&(i="1");e=(e=void 0===e?"":e).replace(/(})(?!$| *})|([\w.]+)(?= *{)|([\w.]+)(?=$| *})|([\w.]+)/g,function(e,r,t,n,o){return r?r+",":t?'"'+t+'":':n?'"'+n+'": '+i:o?'"'+o+'": '+i+",":""});return JSON.parse("{ "+e+" }")}function parseValue(e,r){switch(r){case"ObjectID":return 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]):delete n[t]);return n}function default_1(g,e){var r=e.protectedFields,f=void 0===r?"":r,r=e.defaultFields,p=void 0===r?"_id":r,r=e.defaultSort,d=void 0===r?"-_id":r,r=e.defaultLimit,h=void 0===r?20:r,r=e.pageQueryName,_=void 0===r?"$page":r,r=e.limitQueryName,v=void 0===r?"$limit":r,r=e.fieldsQueryName,$=void 0===r?"$fields":r,r=e.sortQueryName,b=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,O=void 0===r?"":r,r=e.allFieldsQueryName,x=void 0===r?"$getAllFields":r,e=e.getAllFieldsByDefault,k=void 0!==e&&e,j=stringToQuery(f);g.statics.smartQuery=function(e){e=this.__smartQueryGetPipeline(__assign({},e=void 0===e?{}:e));return this.aggregate(e)},g.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]}})})},g.statics.__smartQueryGetPipeline=function(y,e){void 0===e&&(e=!1);var r=JSON.parse(JSON.stringify(y)),t=parseInt(y[_])||1,n=parseInt(y[v])||h;function o(){return y[w]?[{$unwind:{path:"$"+y[w],preserveNullAndEmptyArrays:!0}}]:[]}var i,a,s,u=function(){var e,r={};if(y[m]&&O)for(var t=O.split(" "),n={$regex:RegExp(y[m].replace(/[()[\]]/g,"."),"i")},o=0,i=t;o<i.length;o++){var a=i[o];!!g.path(a)?(r.$or||(r.$or=[]),r.$or.push(((e={})[""+a]=n,e))):(r.$or||(r.$or=[]),r.$or.push(((e={})[""+a]=n,e)))}return t=function(){var e,r,t={},n={};for(r in y){var o=g.path(r),i=o?t:n;if(o||r.includes(".")){var a,s=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,u=[];if("string"==typeof y[r]){for(;null!==(a=s.exec(y[r]));)u.push([a[0],a[1],a[2]]);for(var l=0,c=u;l<c.length;l++){var f=c[l],p=f[1],d=f[2];switch(p){case"$exists":i[r]={$exists:"false"!==d};break;case"$includes":i[r]={$regex:RegExp(d.replace(/[^\w]/g,"."),"i")};break;default:var h=parseValue(d,null==o?void 0:o.instance);p?"object"==typeof i[r]?i[r][p]=h:i[r]=((e={})[p]=h,e):"string"==typeof d&&d.includes("$exists")?i[r]={$exists:!0,$ne:[]}:i[r]=h}}}else i[r]=parseValue(y[r],null==o?void 0:o.instance)}}return{$localMatch:t,$foreignMatch:n}}(),{$queryMatch:__assign(__assign({},{}),t.$localMatch),$foreignMatch:__assign(__assign({},r),t.$foreignMatch)}}(),l=(!(i=y)[$]&&p&&(i[$]=p),removeKeys(stringToQuery(i[$]),j)),c=(a=[],getListOfPossibleLookups(l).forEach(function(e){var r=g.path(e);r&&r.options.ref&&(r=r.options.ref,a.push({$lookup:{from:r,localField:e,foreignField:"_id",as:e}},{$unwind:{path:"$"+e,preserveNullAndEmptyArrays:!0}}))}),a);return e?s=__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(u.$queryMatch).length?[{$match:u.$queryMatch}]:[]),c),o()),0!==Object.keys(u.$foreignMatch).length?[{$match:u.$foreignMatch}]:[]),[{$count:"size"}]):(e=function(){if(!y[b]){if(!d)return{};y[b]=d}for(var e=/(-)?([\w.]+)/g,r={},t={};null!==(o=e.exec(y[b]));){var n=o[1],o=o[2];!!g.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}}(),s=__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([],0!==Object.keys(u.$queryMatch).length?[{$match:u.$queryMatch}]:[]),e.$localSort?[{$sort:e.$localSort}]:[]),c),o()),0!==Object.keys(u.$foreignMatch).length?[{$match:u.$foreignMatch}]:[]),[{$skip:(t-1)*n},{$limit:n}]),!r[$]&&!0===k||"true"===(null===(r=y[x])||void 0===r?void 0:r.toString())?f&&s.push({$project:stringToQuery(f,"0")}):s.push({$project:l})),s}}exports.getListOfPossibleLookups=getListOfPossibleLookups,exports.stringToQuery=stringToQuery,exports.removeKeys=removeKeys,exports.default=default_1;
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 o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,i,s,u){return new(s=s||Promise)(function(r,t){function n(e){try{a(u.next(e))}catch(e){t(e)}}function o(e){try{a(u.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?r(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(n,o)}a((u=u.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(n,o){var a,i,s,u={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},e={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(r){return function(e){var t=[r,e];if(a)throw new TypeError("Generator is already executing.");for(;u;)try{if(a=1,i&&(s=2&t[0]?i.return:t[0]?i.throw||((s=i.return)&&s.call(i),0):i.next)&&!(s=s.call(i,t[1])).done)return s;switch(i=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return u.label++,{value:t[1],done:!1};case 5:u.label++,i=t[1],t=[0];continue;case 7:t=u.ops.pop(),u.trys.pop();continue;default:if(!(s=0<(s=u.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){u=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))u.label=t[1];else if(6===t[0]&&u.label<s[1])u.label=s[1],s=t;else{if(!(s&&u.label<s[2])){s[2]&&u.ops.pop(),u.trys.pop();continue}u.label=s[2],u.ops.push(t)}}t=o.call(n,u)}catch(e){t=[6,e],i=0}finally{a=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,o=0,a=t.length;o<a;o++)!n&&o in t||((n=n||Array.prototype.slice.call(t,0,o))[o]=t[o]);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"));function getListOfPossibleLookups(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=getListOfPossibleLookups(r[t]),n=n.concat(e.map(function(e){return t+"."+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,t,r,n,o){return t?t+",":r?'"'.concat(r,'":'):n?'"'.concat(n,'": ').concat(a):o?'"'.concat(o,'": ').concat(a,","):""});return JSON.parse("{ ".concat(e," }"))}function parseValue(e,t){switch(t){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(g,e){var t=e.protectedFields,p=void 0===t?"":t,t=e.defaultFields,f=void 0===t?"_id":t,t=e.defaultSort,y=void 0===t?"-_id":t,t=e.defaultLimit,d=void 0===t?20:t,t=e.pageQueryName,_=void 0===t?"$page":t,t=e.limitQueryName,v=void 0===t?"$limit":t,t=e.fieldsQueryName,$=void 0===t?"$fields":t,t=e.sortQueryName,m=void 0===t?"$sort":t,t=e.queryName,b=void 0===t?"$q":t,t=e.unwindName,w=void 0===t?"$unwind":t,t=e.fieldsForDefaultQuery,x=void 0===t?"":t,t=e.allFieldsQueryName,O=void 0===t?"$getAllFields":t,t=e.getAllFieldsByDefault,A=void 0!==t&&t,k=stringToQuery(p);g.statics.smartQuery=function(e){e=this.__smartQueryGetPipeline(__assign({},e=void 0===e?{}:e));return this.aggregate(e)},g.statics.smartCount=function(r){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:return t=this.__smartQueryGetPipeline(__assign({},r),!0),[4,this.aggregate(t)];case 1:return[2,0===(t=e.sent()).length?0:t[0].size]}})})},g.statics.__smartQueryGetPipeline=function(h,e){void 0===e&&(e=!1);var t=JSON.parse(JSON.stringify(h)),r=parseInt(h[_])||1,n=parseInt(h[v])||d;function u(){var e,y={},d={};for(e in h)!function(e){var t,r=g.path(e),n=r?y:d;if(!r&&!e.includes("."))return;var o,a=/(?:\{(\$?[\w ]+)\})?([^{}\n]+)/g,i=[];if("string"==typeof h[e]){for(;null!==(o=a.exec(h[e]));)i.push([o[0],o[1],o[2]]);for(var s=0,u=i;s<u.length;s++){var c=u[s],l=c[1],p=c[2];switch(l){case"$exists":n[e]={$exists:"false"!==p};break;case"$includes":n[e]={$regex:RegExp(p.replace(/[^\w]/g,"."),"i")};break;case"$in":var f=p.split(",").map(function(e){return parseValue(e.trim(),null==r?void 0:r.instance)});n[e]={$in:f};break;default:f=parseValue(p,null==r?void 0:r.instance);l?"object"==typeof n[e]?n[e][l]=f:n[e]=((t={})[l]=f,t):"string"==typeof p&&p.includes("$exists")?n[e]={$exists:!0,$ne:[]}:n[e]=f}}}else n[e]=parseValue(h[e],null==r?void 0:r.instance)}(e);return{$localMatch:y,$foreignMatch:d}}function o(){return h[w]?[{$unwind:{path:"$".concat(h[w]),preserveNullAndEmptyArrays:!0}}]:[]}var a,i,s=function(){var e,t={};if(h[b]&&x)for(var r=x.split(" "),n=h[b].replace(/[()[\\\]]/g,".").replace(" ","|"),o={$regex:RegExp(n,"i")},a=0,i=r;a<i.length;a++){var s=i[a];!!g.path(s)?(t.$or||(t.$or=[]),t.$or.push(((e={})["".concat(s)]=o,e))):(t.$or||(t.$or=[]),t.$or.push(((e={})["".concat(s)]=o,e)))}return n=u(),{$queryMatch:__assign(__assign({},{}),n.$localMatch),$foreignMatch:__assign(__assign({},t),n.$foreignMatch)}}(),c=(!(c=h)[$]&&f&&(c[$]=f),removeKeys(stringToQuery(c[$]),k)),l=(a=[],getListOfPossibleLookups(c).forEach(function(e){var t=g.path(e);t&&t.options.ref&&(t=t.options.ref,a.push({$lookup:{from:t,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(!h[m]){if(!y)return{};h[m]=y}for(var e=/(-)?([\w.]+)/g,t={},r={};null!==(o=e.exec(h[m]));){var n=o[1],o=o[2];!!g.path(o)?t[o]=n?-1:1:r[o]=n?-1:1}return{$localSort:0!==Object.keys(t).length?t:void 0,$foreignSort:0!==Object.keys(r).length?r: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:(r-1)*n},{$limit:n}],!1),!t[$]&&!0===A||"true"===(null==(e=h[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.0.14",
3
+ "version": "0.1.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,19 +31,20 @@
31
31
  },
32
32
  "homepage": "https://github.com/mgyugcha/mongoose-smart-query#readme",
33
33
  "devDependencies": {
34
- "@types/jest": "^26.0.22",
35
- "@typescript-eslint/eslint-plugin": "^4.20.0",
36
- "@typescript-eslint/parser": "^4.20.0",
37
- "eslint": "^7.23.0",
38
- "eslint-config-standard": "^16.0.2",
39
- "eslint-plugin-import": "^2.22.1",
40
- "eslint-plugin-jest": "^24.3.2",
34
+ "@types/jest": "^29.0.3",
35
+ "@typescript-eslint/eslint-plugin": "^5.38.1",
36
+ "@typescript-eslint/parser": "^5.38.1",
37
+ "eslint": "^8.24.0",
38
+ "eslint-config-prettier": "^8.5.0",
39
+ "eslint-config-standard": "^17.0.0",
40
+ "eslint-plugin-import": "^2.26.0",
41
+ "eslint-plugin-jest": "^27.0.4",
41
42
  "eslint-plugin-node": "^11.1.0",
42
- "eslint-plugin-promise": "^4.3.1",
43
- "jest": "^26.6.3",
44
- "mongoose": "^5.12.2",
45
- "ts-jest": "^26.5.4",
46
- "typescript": "^4.2.3",
47
- "uglify-js": "^3.13.3"
43
+ "eslint-plugin-promise": "^6.0.1",
44
+ "jest": "^29.0.3",
45
+ "mongoose": "^6.6.2",
46
+ "ts-jest": "^29.0.2",
47
+ "typescript": "^4.8.3",
48
+ "uglify-js": "^3.17.2"
48
49
  }
49
50
  }