Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liveQuery doesn't update for an orderBy on certain compound indexes (perhaps PK or autoincrement related?) #1946

Closed
justinsteven opened this issue Apr 2, 2024 · 1 comment

Comments

@justinsteven
Copy link

Demo: https://codesandbox.io/p/sandbox/zealous-mopsa-9qx3vp

Take for example:

const db = new Dexie("2f7b182a-672d-424d-a66a-84e835e522a3");
db.version(1).stores({
  things: "++id, colour, [colour+number], [colour+id], [colour+id+number]",
});
  • OK: A liveQuery that does a simple toArray() on the things table auto-updates when a new item is added to things
  • OK: A liveQuery that does orderBy("colour").toArray() auto-updates
  • OK: A liveQuery that does orderBy("[colour+number]") auto-updates
  • Fail: A liveQuery that does orderBy("[colour+id]") does not auto-update
  • Fail: A liveQuery that does orderBy("[colour+id+number]") does not auto-update

This reproduces under 4.0.1. I was also seeing symptoms of it from a recent release of 3, from which I updated to 4 hoping it would be resolved. When it wasn't I put the rough but honest codesandbox reproducer together.

Am I doing something wrong? Or is there a bug with the auto-updating of liveQuery, perhaps when the PK and/or an auto-incremented index is involved in a compound index?

I had a quick look and I couldn't find an existing issue for this.

@justinsteven
Copy link
Author

As an aside, I came across this problem when using a pattern similar to that described at https://stackoverflow.com/a/77329694/22644904

For a table where I have a need for a certain seldomly-touched subset of fields, and where I want liveQuery to rerender only upon a change to those fields OR upon a new row in the table, I'm doing a liveQuery on:

db.myTable.orderBy("[orderIndex+field2+field3]"). /*... SNIP ... */

(I happen to want the results ordered by orderIndex anyway, which is a number field I maintain and which I diligently ensure is distinct across all the rows)

Of course, this requires an additional compound index on the table, but I figured that's cheaper than any alternative that I know of.

My question is: If I'm scared of useMemo(), is that pattern still a clean and cheap way to limit the rerenders from useLiveQuery ? Is there a better way these days?

As an aside to the aside, my fear of useMemo() is that I expect that for every, say, 100 updates to the table in question, only 1 update is going to be the addition of a new row or the mutation of one of the "interesting" subset of fields. I figured liveQuery() would be unnecessarily spamming my component with 99 re-renders for every update that this component actually cares about, causing unnecessary work in considering the memo cache etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant