tommybananas / finale

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

How to use AND in search ? #2

Closed jbeaudoin11 closed 6 years ago

jbeaudoin11 commented 6 years ago

I'm looking to do something like localhost:8080/api/v0/media?campaign_id=7&name=09. Which should result in every media with campaign_id == 7 and where the name contains "09" (%09%)..

But i'm not able to find how to do it with "epilogue".. when i log the sequelize call i get : image

What i'm looking to get is :

where: {
    campaign_id: 7,
    name: {
        $like: "%09%"
    }
}
tommybananas commented 6 years ago

You can use this feature to search by a single field:

search: {
    param: 'searchName',
    attributes: [ 'name' ]
}

As mentioned here: https://github.com/tommybananas/finale#search

By default, the substring search is performed using a {field} LIKE '%{query}%'

jbeaudoin11 commented 6 years ago

It's exactly what i've done ! But i wanna use multiple filter at once.

So i just look a little closer to the code and the problem is with extraSearchCriteria. For some reason it's adding param into the criteria/where field. We can see it well in my first post, there is the 2 fields + the symbol line. It work perfectly if i remove these extra criteria.

https://github.com/tommybananas/finale/blob/e8d4151b6089106f56b58896055d0aa4d8742499/lib/Controllers/list.js#L139-L146

tommybananas commented 6 years ago

Why can't you just add all of your search queries individually and use them all using this method?

var userResource = finale.resource({
    model: User,
    endpoints: ['/users', '/users/:id'],
    search: [
      {operator: '$eq', param: 'emailVerified', attributes: [ 'email_verified' ]},
      {param: 'searchOnlyUsernames', attributes: [ 'username' ]}
    ] 
});
jbeaudoin11 commented 6 years ago

Sadly i'm not working on this project anymore so it's hard for me to reply..

Maybe it would work the way you are describing it, but i'm not sure and can't realy test it.

What I did is a patch and i'm not sure if it broke something.. but it was looking good at the moment..