Watermelondb: Support like and notLike operator

Created on 25 Oct 2018  Â·  8Comments  Â·  Source: Nozbe/WatermelonDB

What do you think about add like and notLike operator to query ?

It could be a good optimization for applications with search feature in large amount of data.

Proposal

commentCollection.query(
  Q.where('label', Q.like('a%')),
)

Most helpful comment

Sure, you need these parts:

  • QueryDescription/index.js is where operators are defined
  • adapters/sqlite/encodeQuery is where the query is converted to SQL (react native)
  • adapters/lokijs/encodeQuery is the same but for LokiJS (web)
  • observation/encodeMatcher is where the query is encoded into a matcher function, which can tell whether a Model object matches a query or not. This is necessary for observing queries. More details here: https://github.com/Nozbe/WatermelonDB/issues/15#issuecomment-419713849
  • Tests! The first three items have low-level unit tests to see if the right output is generated from the query language
  • … and then there's the integration test in __tests__/databaseTests that checks every single operator on every supported database to see if it actually works

All 8 comments

this should be possible, not super difficult, but not trivial either. If someone wants to implement this, I can give guidance as for where to start

I'm ok to implement it. Can you give me some advice ?

Sure, you need these parts:

  • QueryDescription/index.js is where operators are defined
  • adapters/sqlite/encodeQuery is where the query is converted to SQL (react native)
  • adapters/lokijs/encodeQuery is the same but for LokiJS (web)
  • observation/encodeMatcher is where the query is encoded into a matcher function, which can tell whether a Model object matches a query or not. This is necessary for observing queries. More details here: https://github.com/Nozbe/WatermelonDB/issues/15#issuecomment-419713849
  • Tests! The first three items have low-level unit tests to see if the right output is generated from the query language
  • … and then there's the integration test in __tests__/databaseTests that checks every single operator on every supported database to see if it actually works

@radex My first PR to WatermelonDB has been sent 🙂. I'm waiting for your feedback

Hello, great work guys.

While this is not approved yet, is there any alternative way of doing a LIKE query ? I really need this feature to implement a filter by Customer Name in my App.

Thanks.

@sonikro if it needs to be a fuzzy search… no good solution comes to mind, sorry. I suggest you temporarily use the Watermelon fork @ieschalier has. His PR is almost ready, should work fine — but might take a week or two until it's realeased

Thanks @radex , that's exactly what Im doing right now.
EDIT: Also, thanks @ieschalier for developing it :+1:

merged and shipped. cc @sonikro

Was this page helpful?
0 / 5 - 0 ratings

Related issues

K4stor picture K4stor  Â·  7Comments

giniedp picture giniedp  Â·  4Comments

impactcolor picture impactcolor  Â·  4Comments

raza2022 picture raza2022  Â·  7Comments

ScreamZ picture ScreamZ  Â·  8Comments