mongo-sql icon indicating copy to clipboard operation
mongo-sql copied to clipboard

Proper place for selector utilities

Open LinusU opened this issue 8 years ago • 1 comments

One quite common use case that I have run into, is to combine two different selectors. This is not as easy at it might first seem, to do Object.assign(selector1, selector2). But rather you must look for any overlap in the keys, and if there are some, use the $and helper. (or always use $and but that can yield large objects)

Would there be an interest of keeping a few utilities somewhere, maybe within this package, or maybe as a separate package, that deals with modifying the objects that goes into mongo-sql?

I'm not sure what would be the most cleanest, just throwing ideas out there :)

LinusU avatar Dec 13 '16 14:12 LinusU

Definitely prefer to keep some utility functions within mongo-sql. I've definitely been in that situation.

Something like mergeConditions(conditionA, conditionB[, conditionC[, ...[, resolverFn]]])

mergeConditions(
 { a: 1 },
 { a: { $gt: 10 } },
  // On column conflict, the resolver function returns a new value
  // for the conflicting key
 (column, table, valueA, valueB) => {
    return { a: { $or: [valueA, valueB] } }
  }
) // => a = 1 or a > 10

I could see common resolvers being used:

mergeConditions(
 { a: 1 },
 { a: { $gt: 10 } },
 mergeConditions.resolvers.or
) // => a = 1 or a > 10

jrf0110 avatar Dec 13 '16 15:12 jrf0110