feathers-fletching icon indicating copy to clipboard operation
feathers-fletching copied to clipboard

SequelizeJoinQuery: Nested $or statement inside $and.

Open egluhbegovic opened this issue 3 years ago • 1 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

I needed to handle Sequelize nested column queries, where the nested column was inside an $or statement nested inside an $and statement.

This PR allows for the sequelizeJoinQuery to still work when the nested column queries are in the following structure:

$and : {
  $or: [
      {
        '$nested.column1$': "value"
      }
    ],
    $or: [
      {
        '$nested.column2$': "value"
      }
    ]
}

Here is the diff that solved my problem:

diff --git a/node_modules/feathers-fletching/src/hooks/sequelizeJoinQuery.js b/node_modules/feathers-fletching/src/hooks/sequelizeJoinQuery.js
index 1e64c5a..a0eaf05 100644
--- a/node_modules/feathers-fletching/src/hooks/sequelizeJoinQuery.js
+++ b/node_modules/feathers-fletching/src/hooks/sequelizeJoinQuery.js
@@ -36,11 +36,26 @@ const getColumnPaths = query => {
   const queryPaths = filterColumnQueries(query);
   const sortPaths = filterColumnQueries(query.$sort);
   const selectPaths = filterColumnQueries(query.$select);
+
   const orQueries = (query.$or || [])
     .map(Object.keys)
     .reduce((acc, val) => acc.concat(val), []);
+
   const orPaths = filterColumnQueries(orQueries);
-  return unique([...queryPaths, ...selectPaths, ...sortPaths, ...orPaths]);
+
+  const andOrQueries = (query.$and || [])
+    .map(Object.entries)
+    .filter(([[a]]) => a === '$or')
+    .reduce((acc, [[,d]]) => { 
+     d.forEach(val => {
+      acc.push(Object.keys(val)[0])
+     })
+     return acc;
+   }, []);
+
+  const andPaths = filterColumnQueries(andOrQueries);
+
+  return unique([...queryPaths, ...selectPaths, ...sortPaths, ...orPaths, ...andPaths]);
 };
 
 const getOrder = (key, value) => {

This issue body was partially generated by patch-package.

egluhbegovic avatar Jan 26 '22 12:01 egluhbegovic

Thanks for the feedback! I am working on joinQuery now to handle any $and/$or queries nested N levels deep. It also cleans up the code quite a bit by using a traverse function that recursively walks the query, so the code gets quite a bit simpler too.

I will port this over to sequelizeJoinQuery as well.

DaddyWarbucks avatar Jan 27 '22 01:01 DaddyWarbucks