From dfd2f306dd9ac666b048f8f35661aef447cc3f8f Mon Sep 17 00:00:00 2001 From: Alvaro Rios Date: Tue, 27 Jul 2021 19:27:36 -0300 Subject: [PATCH 1/5] Allow url encode spaces --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f652630..b59557f 100644 --- a/index.js +++ b/index.js @@ -109,7 +109,7 @@ class SequelizeQueryStringParser { const lhs = '[\\w|.]+' const op = '\\w+' const rhs = `[A-Za-z0-9.+@:/()%_\\s\\-\\xAA\\xB5\\xBA${utf8letters}]+` - const expressionRegExp = new RegExp(`(${lhs})\\s+(${op})\\s+(${rhs})`) + const expressionRegExp = new RegExp(`(${lhs})(\\s|%20)+(${op})(\\s|%20)+(${rhs})`) if (parts[i].match(expressionRegExp)) { let prop = RegExp.$1 let op = RegExp.$2 From 5cd2106d3420bb9a577e897cc3635b1c18cd2284 Mon Sep 17 00:00:00 2001 From: Alvaro Rios Date: Wed, 28 Jul 2021 00:34:25 -0300 Subject: [PATCH 2/5] Fiz first expression --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b59557f..1c725ae 100644 --- a/index.js +++ b/index.js @@ -101,7 +101,7 @@ class SequelizeQueryStringParser { */ find (expression) { let where = {} - if (expression.match(/(([\w|.]+)\s(\w+)\s([\w|\s|%|_]+),?)+/)) { + if (expression.match(/(([\w|.]+)(\s|%20)(\w+)(\s|%20)([\w|\s|%|_]+)?)+/)) { let parts = (expression).split(',') const operators = this.operators() for (let i = 0; i < parts.length; i++) { @@ -112,8 +112,8 @@ class SequelizeQueryStringParser { const expressionRegExp = new RegExp(`(${lhs})(\\s|%20)+(${op})(\\s|%20)+(${rhs})`) if (parts[i].match(expressionRegExp)) { let prop = RegExp.$1 - let op = RegExp.$2 - let value = RegExp.$3 + let op = RegExp.$3 + let value = RegExp.$5 if (!operators[op]) { throw new Error(`Invalid operator ${op}`) } From 74a77dda602c7eb514c2bcd1841236c11699fad3 Mon Sep 17 00:00:00 2001 From: Alvaro Rios Date: Fri, 1 Oct 2021 09:41:42 -0300 Subject: [PATCH 3/5] Order nest --- index.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 1c725ae..f175440 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ const deepPropSet = (obj, dotPath, key, val) => { * @description Used to parse query strings and produce sort / filter objects * for sequelize querying */ -class SequelizeQueryStringParser { +class SequelizeQueryStringParser { /** @constructor * * @param {Object} sequelize - the user's `require('sequelize')` @@ -31,7 +31,7 @@ class SequelizeQueryStringParser { * @param {Boolean} opts.symbolic - whether to use symbolic Ops * or (deprecated) string Ops */ - constructor (sequelize, opts={}) { + constructor(sequelize, opts = {}) { this.sequelize = sequelize this.symbolic = opts.symbolic || false if (opts.symbolic && !sequelize) { @@ -50,9 +50,9 @@ class SequelizeQueryStringParser { * @param {Boolean} opts.symbolic - will be set to true regardless of passed value * @returns an instance of `SequelizeQueryStringParser` with symbolic=true. */ - withSymbolicOps (sequelize, opts={}) { - return new SequelizeQueryStringParser(sequelize, - Object.assign(opts, {symbolic: true})) + withSymbolicOps(sequelize, opts = {}) { + return new SequelizeQueryStringParser(sequelize, + Object.assign(opts, { symbolic: true })) } /** @method @@ -70,8 +70,8 @@ class SequelizeQueryStringParser { operators() { const identityOps = { valFunc: identity, - ops: ['gt', 'gte', 'lt', 'lte', 'ne', 'eq', 'not', 'like', - 'notLike', 'iLike', 'notILike'] + ops: ['gt', 'gte', 'lt', 'lte', 'ne', 'eq', 'not', 'like', + 'notLike', 'iLike', 'notILike'] } const arrayHaveOps = { valFunc: arrayHave, @@ -81,7 +81,7 @@ class SequelizeQueryStringParser { for (var opSet of [identityOps, arrayHaveOps]) { for (var op of opSet.ops) { resultMap[op] = { - 'op': (this.symbolic ? this.sequelize.Op[op] : `$${op}`), + 'op': (this.symbolic ? this.sequelize.Op[op] : `$${op}`), 'valFunc': opSet.valFunc } } @@ -99,7 +99,7 @@ class SequelizeQueryStringParser { * @returns {Object} the where clause for building the corresponding * Sequelize query */ - find (expression) { + find(expression) { let where = {} if (expression.match(/(([\w|.]+)(\s|%20)(\w+)(\s|%20)([\w|\s|%|_]+)?)+/)) { let parts = (expression).split(',') @@ -141,7 +141,8 @@ class SequelizeQueryStringParser { * @returns {Array} the order clause for building the corresponding Sequelize * query */ - sort (expression, sequelize) { + sort(expression, sequelize) { + // maintain backward-compatibility with v0.x.x, where users supply // their own sequelize object as the second param of sort() sequelize = sequelize || this.sequelize @@ -152,7 +153,16 @@ class SequelizeQueryStringParser { if (exp.length === 2) { let prop = exp[0] let ord = exp[1].toUpperCase() - if (ord.match(/ASC|DESC/i)) { + + let nests = prop.split('.') + + if (nests.length > 1) { + prop = nests + if (ord.match(/ASC|DESC/i)) { + prop.push(ord.toUpperCase()) + order.push(prop) + } + } else if (ord.match(/ASC|DESC/i)) { order.push([prop, ord.toUpperCase()]) } } From aa40447f995fcf64cff92115f2e5f980fc8c639d Mon Sep 17 00:00:00 2001 From: Alvaro Rios Date: Fri, 8 Oct 2021 15:33:49 -0300 Subject: [PATCH 4/5] Update Eager loading filter to sequelize 6 --- index.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f175440..07b6133 100644 --- a/index.js +++ b/index.js @@ -10,12 +10,9 @@ const arrayHave = (v) => { return v.split(' ').map(v => { return isNaN(v) ? `{${ * (replacing _.set() to avoid importing all of underscore) */ const deepPropSet = (obj, dotPath, key, val) => { - const props = dotPath.split('.') - let here = obj - props.forEach((prop, i) => { - here = (here[prop] = here[prop] || {}) - }) - here[key] = val + obj[`$${dotPath}$`] = { + [key]: val + } } /** @class From 9960efb6ed2baf962b83c0398ff4832e7e744692 Mon Sep 17 00:00:00 2001 From: ninobaldo Date: Wed, 10 Nov 2021 12:27:26 -0300 Subject: [PATCH 5/5] Do not put $ if there is no dot --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 07b6133..2e0470b 100644 --- a/index.js +++ b/index.js @@ -10,8 +10,15 @@ const arrayHave = (v) => { return v.split(' ').map(v => { return isNaN(v) ? `{${ * (replacing _.set() to avoid importing all of underscore) */ const deepPropSet = (obj, dotPath, key, val) => { - obj[`$${dotPath}$`] = { - [key]: val + + if(dotPath.split('.').length > 1) { + obj[`$${dotPath}$`] = { + [key]: val + } + } else { + obj[`${dotPath}`] = { + [key]: val + } } }