mongoose-query-parser
Advanced tools
Comparing version 1.1.7 to 1.1.8
export interface ParserOptions { | ||
dateFormat?: any; | ||
dateFormat?: string | string[]; | ||
blacklist?: string[]; | ||
@@ -27,3 +27,2 @@ casters?: { | ||
private options; | ||
private readonly defaultDateFormat; | ||
private readonly builtInCaster; | ||
@@ -30,0 +29,0 @@ private readonly operators; |
@@ -5,4 +5,4 @@ "use strict"; | ||
var qs = require("querystring"); | ||
var Moment = require("moment"); | ||
var _ = require("lodash"); | ||
var utils_1 = require("./utils"); | ||
var MongooseQueryParser = /** @class */ (function () { | ||
@@ -13,9 +13,8 @@ function MongooseQueryParser(options) { | ||
this.options = options; | ||
this.defaultDateFormat = [Moment.ISO_8601]; | ||
this.builtInCaster = { | ||
string: function (val) { return String(val); }, | ||
date: function (val) { | ||
var m = Moment(val, _this.options.dateFormat); | ||
if (m.isValid()) { | ||
return m.toDate(); | ||
var dt = utils_1.toDate(val, _this.options.dateFormat); | ||
if (dt.isValid) { | ||
return dt.toJSDate(); | ||
} | ||
@@ -35,4 +34,2 @@ else { | ||
]; | ||
// add default date format as ISO_8601 | ||
this.options.dateFormat = options.dateFormat || this.defaultDateFormat; | ||
// add builtInCaster | ||
@@ -117,5 +114,5 @@ this.options.casters = Object.assign(this.builtInCaster, options.casters); | ||
// Match dates | ||
var m = Moment(value, options.dateFormat); | ||
if (m.isValid()) { | ||
return m.toDate(); | ||
var dt = utils_1.toDate(value, this.options.dateFormat); | ||
if (dt.isValid) { | ||
return dt.toJSDate(); | ||
} | ||
@@ -122,0 +119,0 @@ return value; |
@@ -65,2 +65,22 @@ "use strict"; | ||
}; | ||
Tester.prototype.customDateFormatParse = function () { | ||
var parser = new _1.MongooseQueryParser({ dateFormat: ['yyyyMMdd', 'yyyy-MM-dd'] }); | ||
var qry = "d1=date(20201001)&d2=2020-01-01&d3=09:20&d4=2020"; | ||
var parsed = parser.parse(qry); | ||
chai_1.assert.exists(parsed.filter); | ||
chai_1.assert.isTrue(parsed.filter.d1 instanceof Date); | ||
chai_1.assert.isTrue(parsed.filter.d2 instanceof Date); | ||
chai_1.assert.isNotTrue(parsed.filter.d3 instanceof Date); | ||
chai_1.assert.isTrue(typeof parsed.filter.d4 === 'number'); | ||
}; | ||
Tester.prototype.dateParse = function () { | ||
var parser = new _1.MongooseQueryParser(); | ||
var qry = "d1=2020-10-01&d2=2020-01&d3=09:20&d4=2020"; | ||
var parsed = parser.parse(qry); | ||
chai_1.assert.exists(parsed.filter); | ||
chai_1.assert.isTrue(parsed.filter.d1 instanceof Date); | ||
chai_1.assert.isTrue(parsed.filter.d2 instanceof Date); | ||
chai_1.assert.isTrue(parsed.filter.d3 instanceof Date); | ||
chai_1.assert.isTrue(typeof parsed.filter.d4 === 'number'); | ||
}; | ||
Tester.prototype.generalParse2 = function () { | ||
@@ -152,2 +172,14 @@ var parser = new _1.MongooseQueryParser(); | ||
__decorate([ | ||
mocha_1.test('should parse dates with custom formats'), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], Tester.prototype, "customDateFormatParse", null); | ||
__decorate([ | ||
mocha_1.test('should parse dates'), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], Tester.prototype, "dateParse", null); | ||
__decorate([ | ||
mocha_1.test('should parse query with string templates'), | ||
@@ -154,0 +186,0 @@ __metadata("design:type", Function), |
{ | ||
"name": "mongoose-query-parser", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"description": "Convert url query string to MongooseJs friendly query object including advanced filtering, sorting, population, string template, type casting and many more...", | ||
@@ -32,18 +32,19 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@testdeck/mocha": "^0.1.0", | ||
"@types/chai": "^4.2.11", | ||
"@types/lodash": "^4.14.157", | ||
"@types/mocha": "^8.0.0", | ||
"@types/node": "^14.0.23", | ||
"@testdeck/mocha": "^0.1.2", | ||
"@types/chai": "^4.2.13", | ||
"@types/lodash": "^4.14.161", | ||
"@types/luxon": "^1.25.0", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.11.5", | ||
"chai": "^4.2.0", | ||
"mocha": "^8.0.1", | ||
"mocha": "^8.1.3", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^8.10.2", | ||
"tslint": "^6.1.2", | ||
"typescript": "^3.9.7" | ||
"ts-node": "^9.0.0", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.0.3" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.19", | ||
"moment": "^2.27.0" | ||
"lodash": "^4.17.20", | ||
"luxon": "^1.25.0" | ||
} | ||
} |
@@ -75,16 +75,16 @@ # mongoose-query-parser | ||
| MongoDB | URI | Example | Result | | ||
| ------- | --- | ------- | ------ | | ||
| `$eq` | `key=val` | `type=public` | `{filter: {type: 'public'}}` | | ||
| `$gt` | `key>val` | `count>5` | `{filter: {count: {$gt: 5}}}` | | ||
| `$gte` | `key>=val` | `rating>=9.5` | `{filter: {rating: {$gte: 9.5}}}` | | ||
| `$lt` | `key<val` | `createdAt<2017-10-01` | `{filter: {createdAt: {$lt: 2017-09-30T14:00:00.000Z}}}` | | ||
| `$lte` | `key<=val` | `score<=-5` | `{filter: {score: {$lte: -5}}}` | | ||
| `$ne` | `key!=val` | `status!=success` | `{filter: {status: {$ne: 'success'}}}` | | ||
| `$in` | `key=val1,val2` | `country=GB,US` | `{filter: {country: {$in: ['GB', 'US']}}}` | | ||
| `$nin` | `key!=val1,val2` | `lang!=fr,en` | `{filter: {lang: {$nin: ['fr', 'en']}}}` | | ||
| `$exists` | `key` | `phone` | `{filter: {phone: {$exists: true}}}` | | ||
| `$exists` | `!key` | `!email` | `{filter: {email: {$exists: false}}}` | | ||
| `$regex` | `key=/value/<opts>` | `email=/@gmail\.com$/i` | `{filter: {email: /@gmail.com$/i}}` | | ||
| `$regex` | `key!=/value/<opts>` | `phone!=/^06/` | `{filter: {phone: { $not: /^06/}}}` | | ||
| MongoDB | URI | Example | Result | | ||
| --------- | -------------------- | ----------------------- | -------------------------------------------------------- | | ||
| `$eq` | `key=val` | `type=public` | `{filter: {type: 'public'}}` | | ||
| `$gt` | `key>val` | `count>5` | `{filter: {count: {$gt: 5}}}` | | ||
| `$gte` | `key>=val` | `rating>=9.5` | `{filter: {rating: {$gte: 9.5}}}` | | ||
| `$lt` | `key<val` | `createdAt<2017-10-01` | `{filter: {createdAt: {$lt: 2017-09-30T14:00:00.000Z}}}` | | ||
| `$lte` | `key<=val` | `score<=-5` | `{filter: {score: {$lte: -5}}}` | | ||
| `$ne` | `key!=val` | `status!=success` | `{filter: {status: {$ne: 'success'}}}` | | ||
| `$in` | `key=val1,val2` | `country=GB,US` | `{filter: {country: {$in: ['GB', 'US']}}}` | | ||
| `$nin` | `key!=val1,val2` | `lang!=fr,en` | `{filter: {lang: {$nin: ['fr', 'en']}}}` | | ||
| `$exists` | `key` | `phone` | `{filter: {phone: {$exists: true}}}` | | ||
| `$exists` | `!key` | `!email` | `{filter: {email: {$exists: false}}}` | | ||
| `$regex` | `key=/value/<opts>` | `email=/@gmail\.com$/i` | `{filter: {email: /@gmail.com$/i}}` | | ||
| `$regex` | `key!=/value/<opts>` | `phone!=/^06/` | `{filter: {phone: { $not: /^06/}}}` | | ||
@@ -270,7 +270,8 @@ For more advanced usage (`$or`, `$type`, `$elemMatch`, etc.), pass any MongoDB query filter object as JSON string in the `filter` query parameter, ie: | ||
#### Date format | ||
- `dateFormat`: set date format for auto date casting. Default is ISO_8601 format | ||
- Allows multiple formats. Works with [moment](https://momentjs.com/docs/#/parsing/string/) | ||
- `dateFormat`: set date format for auto date casting. Default is ISO_8601 format if no custom datetime format provided | ||
- Allows multiple formats. Works with [luxon](https://github.com/moment/luxon) | ||
- For the supported format tokens, please refer to [luxon document](https://moment.github.io/luxon/docs/manual/parsing.html#table-of-tokens) | ||
```js | ||
const parser = new MongooseQueryParser({dateFormat: ['YYYYMMDD', 'YYYY-MM-DD']}); | ||
const parser = new MongooseQueryParser({dateFormat: ['yyyyMMdd', 'yyyy-MM-dd']}); | ||
parser.parse('date1=20171001&date2=2017-10-01'); | ||
@@ -277,0 +278,0 @@ // { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45475
12
802
331
12
+ Addedluxon@^1.25.0
+ Addedluxon@1.28.1(transitive)
- Removedmoment@^2.27.0
- Removedmoment@2.30.1(transitive)
Updatedlodash@^4.17.20