dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Needing the `$eq` operator when doing a Boolean query? #195

Closed bighitbiker3 closed 7 years ago

bighitbiker3 commented 7 years ago

Thanks so much for this library. Question though:

When doing a query like /products?digitalShip=true we're having to format our search like the below with the operator. Without the operator it returns nothing

search:{
  operator: '$eq', 
  param: 'digitalShip', 
  attributes: ['isDigitalShip']
}

Is this by design? I feel like this is occurring because it's automatically inserting the $like operator when there isn't an operator passed in the search, but I could be wrong. I see in the tests you don't pass the operator and it seems to work. Any ideas?

mbroadst commented 7 years ago

@bighitbiker3 yes, the default operator is $like. Another thing you might run into here is that true is going to be interpreted as a string - not sure how that plays out, but it might just always coerce to the boolean value true? You'll have to tell us :smile:

bighitbiker3 commented 7 years ago

@mbroadst Well 0fff476ac179418efbe58fdbe32eeff39c70aab5 fixed the string interpretation :). I can confirm that the safeishParse function is working correctly. Since the $like operator is trying to match character expressions though, when used with Boolean values it's going to return nothing (from my somewhat limited SQL knowledge and postgreSQL queries I just did)

However in your test you're not using the $eq operator from what I can tell. Which is why i'm wondering if this issue is specific to me, or if I'm reading the test wrong, or if the test isn't testing what I think it is....

I guess what I'm asking is, would you rather have this laid out in the docs, or have a PR to change the logic for default operator if checking for a Boolean value

mbroadst commented 7 years ago

@bighitbiker3 ah! yeah I think the test is probably getting away with murder there. My preference would be to be as explicit as possible where possible. So to that end, while we do default to $like (for legacy reasons, but also because thats probably most people's default), I don't think we should be in the habit of magically selecting operators based on filter value - that could get messy quickly.

bighitbiker3 commented 7 years ago

@mbroadst Ok cool. I think in the future not selecting a default operator/defaulting to $eq would make the most sense, but let me submit a PR to get this in the docs for current and future users.

Again, thanks a lot for this library :)