From b5768cf3eaf0a88857b64d6f637ba0bd56d15bd8 Mon Sep 17 00:00:00 2001 From: Mathieu DARTIGUES Date: Mon, 7 Sep 2020 23:41:25 +0200 Subject: [PATCH] Add the root field when filtering on a object field --- src/index.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 264bc77..9bbde0d 100644 --- a/src/index.js +++ b/src/index.js @@ -155,7 +155,7 @@ class Service extends AdapterService { * @param methodKey * @param allowRefs */ - objectify (query, params, parentKey, methodKey, allowRefs) { + objectify (query, params, parentKey, methodKey, allowRefs, hierarchy = []) { if (params.$eager) { delete params.$eager; } if (params.$joinEager) { delete params.$joinEager; } if (params.$joinRelation) { delete params.$joinRelation; } @@ -169,14 +169,15 @@ class Service extends AdapterService { let value = params[key]; if (utils.isPlainObject(value)) { - return this.objectify(query, value, key, parentKey, allowRefs); + hierarchy.push(key) + return this.objectify(query, value, key, parentKey, allowRefs, hierarchy); } const column = parentKey && parentKey[0] !== '$' ? parentKey : key; const method = METHODS[methodKey] || METHODS[parentKey] || METHODS[key]; const operator = OPERATORS_MAP[key] || '='; - if (method) { + if (method && hierarchy.length === 0) { if (key === '$or') { const self = this; @@ -208,7 +209,13 @@ class Service extends AdapterService { return query[method].call(query, column, value); // eslint-disable-line no-useless-call } - const property = this.jsonSchema && (this.jsonSchema.properties[column] || (methodKey && this.jsonSchema.properties[methodKey])); + const property = + this.jsonSchema + && ( + this.jsonSchema.properties[column] + || hierarchy.length > 0 && this.jsonSchema.properties[hierarchy[0]] + || (methodKey && this.jsonSchema.properties[methodKey]) + ); let columnType = property && property.type; if (columnType) { if (Array.isArray(columnType)) { columnType = columnType[0]; } @@ -231,6 +238,13 @@ class Service extends AdapterService { if (typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { value = JSON.parse(value); } } + if (method) { + return query[method].call( + query, + NON_COMPARISON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(), + value + ); + } return query.where( NON_COMPARISON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(), operator,