dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.68k stars 641 forks source link

WhereClause utility matcher? #1100

Open Nerdinacan opened 4 years ago

Nerdinacan commented 4 years ago

Is there a way to take an instance of a collection or WhereClause and determine whether an individual document object matches the criteria defined by that clause?

This seems like a basic necessary feature of having a where clause at all but I'm having a hard time nailing down where the comparison is actually evaluated in your source.

dfahlander commented 4 years ago

Yes but there's no helper for it implemented. I suppose this should be a built-in functionality somehow since implementing this algorithm in a third part library could easily break in future versions. It would be safer to implement it in a Dexie PR and add unit tests for it so that it will be continously maintained. Could also be made as an addon but add tests for it in the Dexie CI (integration part), which would also safe maintainability. I can just go through the steps needed to perform it by inspecting the _ctx property of Collection.

  1. Find keyPath to the property or properties representing the index or primary key
    • If _ctx.index !== null:
    • Find the property path for the index through _ctx.table.schema.idxByName[_ctx.index].keyPath.
    • If _ctx.index === null:
    • Find the primary key path through _ctx.table.schema.primKey.keyPath.
  2. Use Dexie.getByKeyPath() to read that property on the object you want to compare.
  3. Check if that value is in the range defined by _ctx.range. Use db.core.cmp() to compare bounds.
  4. Apply any filters (if _ctx.isMatch is not null, also run the object through that function)
  5. Apply or: If _ctx.or is truthy, do the same algorithm for that one also. Return true if anyone of those are true.
Nerdinacan commented 4 years ago

Thanks for the clues, man.

I am indeed going to implement it as an add-on for what I need to accomplish right now, but if whatever I cobble together looks good enough to turn into a PR, I'll put one together after my immediate fire is out.

Thanks!