emmett-framework / rest

REST extension for Emmett framework
BSD 3-Clause "New" or "Revised" License
14 stars 2 forks source link

About query language #17

Closed josejachuf closed 1 year ago

josejachuf commented 1 year ago

Hi @gi0baro I am trying to use where with $or and $and but it doesn't work as I hope

in javascript I have:

if (params.filter) {
    where = {
      $or: [
        {
          // id: params.filter,
          last_name: { $ilike: params.filter },
          first_name: { $ilike: params.filter }
        }
      ]
    }
    where = JSON.stringify(where)    
  }

With this I get, for example:

{"$or":[{"last_name":{"$ilike":"Fue"},"first_name":{"$ilike":"Fue"}}]}

then: http://a.com/api/user?page=1&page_size=20&sort_by=-id&where={"$or":[{"last_name":{"$ilike":"F"},"first_name":{"$ilike":"F"}}]}

but not returns data

What am I doing wrong? Jose

josejachuf commented 1 year ago

This work: where = { last_name: { $ilike: '%' + params.filter + '%' } }

the % were missing, but with $or not works:

 where = {
      $or: [
        {
          // id: params.filter,
          last_name: { $ilike: '%' + params.filter + '%' },
          first_name: { $ilike: '%' + params.filter + '%' }
        }
      ]
    }
gi0baro commented 1 year ago

@josejachuf the list argument of $or should contain the objects of the conditions:

where = {
  $or: [
    {last_name: { $ilike: '%' + params.filter + '%' }},
    {first_name: { $ilike: '%' + params.filter + '%' }}
  ]
}

while you put a single object, so a single AND condition.

Also, you probably want to swtich from $ilike to $icontains.

josejachuf commented 1 year ago

Thanks, works fine!!