express-query-params
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -35,5 +35,7 @@ const format = require('../lib/format') | ||
else if (format.isRange(value)) { | ||
const [ from, to ] = value.split('...') | ||
mongo[key] = { $lte: cast(from), $gte: cast(to) } | ||
const [ from, to ] = insertValue.split('...') | ||
mongo[key] = { $gte: cast(from), $lte: cast(to) } | ||
} else mongo[key] = insertValue | ||
if (format.negated(value)) mongo[key] = { $not: mongo[key] } | ||
} | ||
@@ -40,0 +42,0 @@ |
@@ -26,11 +26,10 @@ const format = require('../lib/format') | ||
const value = rawQuery[key] | ||
let insertValue = cast(trimOperators(value)) | ||
if (format.isRange(value)) { | ||
const [ from, to ] = value.split('...') | ||
const [ from, to ] = insertValue.split('...') | ||
const clause = `${key} >= $${values.length + 1} AND ${key} <= $${values.length + 2}` | ||
values.push(cast(from), cast(to)) | ||
clauses.push( | ||
`${key} >= $${values.length - 1}`, | ||
`${key} <= $${values.length}` | ||
) | ||
clauses.push(format.negated(value) ? `NOT (${clause})` : clause) | ||
@@ -51,3 +50,2 @@ continue | ||
let clause = '' | ||
let insertValue = cast(trimOperators(value)) | ||
const index = values.length + 1 | ||
@@ -65,3 +63,3 @@ | ||
clauses.push(clause) | ||
clauses.push(format.negated(value) ? `NOT ${clause}` : clause) | ||
values.push(insertValue) | ||
@@ -68,0 +66,0 @@ } |
@@ -9,2 +9,3 @@ const stringContains = raw => raw[0] === '*' || raw[raw.length - 1] === '*' | ||
const oneOf = raw => raw.indexOf(',') > -1 | ||
const negated = raw => raw[0] === '!' | ||
@@ -19,3 +20,4 @@ module.exports = { | ||
isLesserThanOrEqual(raw) { return isLesserThanOrEqual(String(raw)) }, | ||
oneOf(raw) { return oneOf(String(raw)) } | ||
oneOf(raw) { return oneOf(String(raw)) }, | ||
negated(raw) { return negated(String(raw)) } | ||
} |
@@ -8,3 +8,3 @@ /* | ||
const ISO8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/ | ||
const OPERATOR_TRIM_REGEX = /(\*|>=?|<=?|\^)/g | ||
const OPERATOR_TRIM_REGEX = /(\*|>=?|<=?|\^|!)/g | ||
@@ -11,0 +11,0 @@ exports.typeCast = ({ dateFormat = 'iso8601', returnJSDate } = {}) => ( |
{ | ||
"name": "express-query-params", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Express.js middleware implementing the API Query Spec, converting the params to SQL or a Mongo query", | ||
@@ -40,4 +40,4 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">8" | ||
"node": ">=8" | ||
} | ||
} |
@@ -11,8 +11,8 @@ /* global test expect */ | ||
age: { | ||
$lte: 18, | ||
$gte: 25 | ||
$gte: 18, | ||
$lte: 25 | ||
}, | ||
boughtSomethingOn: { | ||
$lte: new Date('2014-07-01'), | ||
$gte: new Date('2014-09-01') | ||
$lte: new Date('2014-09-01'), | ||
$gte: new Date('2014-07-01') | ||
}, | ||
@@ -27,3 +27,10 @@ createdAt: { $gt: new Date('2014-01-01') }, | ||
accountBalance: 25.22, | ||
favouriteColours: { $in: [ 'red', 'green', 'blue' ] } | ||
favouriteColours: { $in: [ 'red', 'green', 'blue' ] }, | ||
hairStyle: { $not: 'bald' }, | ||
income: { | ||
$not: { | ||
$lte: 50000, | ||
$gte: 30000 | ||
} | ||
} | ||
} | ||
@@ -30,0 +37,0 @@ |
@@ -24,6 +24,27 @@ /* global test expect */ | ||
'green', | ||
'blue' | ||
'blue', | ||
'bald', | ||
30000, | ||
50000 | ||
] | ||
const correctQuery = 'username = $1 AND email LIKE $2 AND age >= $3 AND age <= $4 AND boughtSomethingOn >= $5 AND boughtSomethingOn <= $6 AND createdAt > $7 AND updatedAt < $8 AND friends >= $9 AND followers <= $10 AND banned = $11 AND activated = $12 AND firstName ILIKE $13 AND accountBalance = $14 AND favouriteColours IN ($15, $16, $17)' | ||
const correctQuery = [ | ||
'username = $1', | ||
'email LIKE $2', | ||
'age >= $3', | ||
'age <= $4', | ||
'boughtSomethingOn >= $5', | ||
'boughtSomethingOn <= $6', | ||
'createdAt > $7', | ||
'updatedAt < $8', | ||
'friends >= $9', | ||
'followers <= $10', | ||
'banned = $11', | ||
'activated = $12', | ||
'firstName ILIKE $13', | ||
'accountBalance = $14', | ||
'favouriteColours IN ($15, $16, $17)', | ||
'NOT hairStyle = $18', | ||
'NOT (income >= $19 AND income <= $20)' | ||
].join(' AND ') | ||
@@ -30,0 +51,0 @@ test('req.query -> SQL', () => { |
@@ -21,3 +21,3 @@ /* global test expect */ | ||
for (const param in query) { | ||
const isCorrect = [ 'boughtSomethingOn', 'age' ].indexOf(param) > -1 | ||
const isCorrect = [ 'boughtSomethingOn', 'age', 'income' ].indexOf(param) > -1 | ||
expect(format.isRange(query[param])).toBe(isCorrect) | ||
@@ -56,1 +56,7 @@ } | ||
}) | ||
test('negated', () => { | ||
for (const param in query) { | ||
expect(format.negated(query[param])).toBe([ 'hairStyle', 'income' ].indexOf(param) > -1) | ||
} | ||
}) |
module.exports = { | ||
username: 'steve', | ||
email: '*@gmail.com*', | ||
email: '*@gmail.com', | ||
age: '18...25', | ||
@@ -15,3 +15,5 @@ boughtSomethingOn: '2014-07-01...2014-09-01', | ||
favouriteColours: 'red,green,blue', | ||
hairStyle: '!bald', | ||
income: '!30000...!50000', | ||
limit: 10 | ||
} |
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
139273
366