balderdashy / waterline-sequel

A SQL generator for use in Waterline Adapters
MIT License
16 stars 61 forks source link

Add support for array-ish `and` clause #68

Open anotherpit opened 8 years ago

anotherpit commented 8 years ago

...which also gives you a support for nested and mixed and and or clauses. Like these:

// WHERE color="blue" AND color="red"
Pencil.find({
  where: {
    and: [
      {color: 'blue'},
      {color: 'red'}
    ]
  }
});

// WHERE color="red" AND (color="green" AND color="blue")
Pencil.find({
  where: {
    color: 'red',
    and: [
      {color: 'green'},
      {color: 'blue'}
    ]
  }
});

// WHERE color="red" AND (color="green" AND (color="blue" AND color="yellow"))
Pencil.find({
  where: {
    and: [
      {color: 'red'},
      {
        and: [
          {color: 'green'},
          {
            and: [
              {color: 'blue'},
              {color: 'yellow'}
            ]
          }
        ]
      }
    ]
  }
});

// WHERE (color="red" OR color="green") AND (color="blue" OR color="yellow")
Pencil.find({
  where: {
    and: [
      {
        or: [
          {color: 'red'},
          {color: 'green'}
        ]
      },
      {
        or: [
          {color: 'blue'},
          {color: 'yellow'}
        ]
      }
    ]
  }
});

// WHERE (color="red" AND color="green") OR (color="blue" AND color="yellow")
Pencil.find({
  where: {
    or: [
      {
        and: [
          {color: 'red'},
          {color: 'green'}
        ]
      },
      {
        and: [
          {color: 'blue'},
          {color: 'yellow'}
        ]
      }
    ]
  }
});

Effectively closes https://github.com/balderdashy/waterline/issues/828

anotherpit commented 8 years ago

Moreover, according to its tests the fellow waterline-criteria library already supports this syntax for and clause.

anotherpit commented 8 years ago

Any thoughts on this PR, guys?

particlebanana commented 8 years ago

@anotherpit I dig it, sorry haven't had time to get into it. We also need some tests in waterline-adapter-tests to show it works in adapters not using wl-sequel such as mongo, etc.

particlebanana commented 8 years ago

@anotherpit also this is supported in the updated criteria/query syntax I'm working on.

jgeurts commented 8 years ago

This would be nice to see get merged. Though, we get around this issue, with the mongo adapter, by using $and for now.

cyrilchapon commented 7 years ago

Any status on this.. ?

And btw thanks @jgeurts, you made my day with

we get around this issue, with the mongo adapter, by using $and for now.

AmirTugi commented 7 years ago

@anotherpit Oh my god! This is beautifully done! This is exactly what I was looking for for over 2 months! It is so banal that I can't believe this wasn't done from the beginning. When will this be merged already? It is one of the most basic features for a SQL adapter.

appdevdesigns commented 7 years ago

Yes! We needed this for some of our queries as well. I was about to post a PR with this fix, but @anotherpit did a great job and included tests!

it seems { and:[] } is supported in Sails and waterline-criteria, but just dies here.

I'd love to see this merged soon.