From 443d6a4e8a7c0b0c13d417a8264a7e5adbd02a8a Mon Sep 17 00:00:00 2001 From: Alex Gola Date: Tue, 9 Jul 2024 15:56:15 +0200 Subject: [PATCH] chore(PO-4636): fix query parser on the adapter --- lib/adapters/mongodb.js | 5 +++-- lib/querytree.js | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/adapters/mongodb.js b/lib/adapters/mongodb.js index 650efc1b..9eb0d197 100644 --- a/lib/adapters/mongodb.js +++ b/lib/adapters/mongodb.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const RSVP = require('rsvp'); const _ = require('lodash'); +const { ensureInArray } = require('../querytree'); const Promise = RSVP.Promise; const adapter = {}; @@ -584,9 +585,9 @@ adapter.parseQuery = function (model, query) { }, {}, ); - } else if (_.isString(val.in || val.$in)) { + } else if (val.in || val.$in) { query[key] = { - $in: (val.in || val.$in).split(','), + $in: ensureInArray(val.in || val.$in), }; } else if (_.isObject(val) && _.isString(val.regex)) { //regex diff --git a/lib/querytree.js b/lib/querytree.js index b92a334a..cef7b3fd 100644 --- a/lib/querytree.js +++ b/lib/querytree.js @@ -8,6 +8,15 @@ function isSpecial(query) { }); } +function ensureInArray(inValue) { + if (_.isArray(inValue)) return inValue; + if (_.isString(inValue)) return inValue.split(','); + if (_.isObject(inValue)) return _.values(inValue); + + console.log('ensureInArray cannot handle the input value', inValue); + throw new Error('ensureInArray cannot handle the input value'); +} + /** * registers fortune instance * expects that all resources have been defined @@ -30,15 +39,6 @@ exports.init = function (fortune) { this.query = parseQuery(request, query, resource); } - function ensureInArray(inValue) { - if (_.isArray(inValue)) return inValue; - if (_.isString(inValue)) return inValue.split(','); - if (_.isObject(inValue)) return _.values(inValue); - - console.log('ensureInArray cannot handle the input value', inValue); - throw new Error('ensureInArray cannot handle the input value'); - } - /** * Iterates first level of query detecting filtering by referenced resources fields * @param query @@ -154,3 +154,5 @@ exports.init = function (fortune) { return instance; }; + +exports.ensureInArray = ensureInArray;