Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper place for selector utilities #141

Open
LinusU opened this issue Dec 13, 2016 · 1 comment
Open

Proper place for selector utilities #141

LinusU opened this issue Dec 13, 2016 · 1 comment

Comments

@LinusU
Copy link
Contributor

LinusU commented Dec 13, 2016

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 :)

@jrf0110
Copy link
Member

jrf0110 commented Dec 13, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants