dexie / Dexie.js

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

orderBy silently creating a collection with unique keys #1957

Open 1toddlewis opened 2 months ago

1toddlewis commented 2 months ago

I have a table full of board games owned by my friends and details about them. The primary key for the table is a collection ID that is unique to each copy. There is also an objectId that correlates to the game, that is indexed.

Some versions of the same game have different names. For example, Carabande (a game where you flick disks around a racetrack) is also called PitchCar, but they share the same objectId -- they are two different names for the same game.

When I order the table by objectId, orderBy is quietly creating a collection that only has one document per objectId. A subsequent filter looks for a portion of a game title, and PitchCar is completely missing. It's not even in the collection to be found. orderBy selected the document for Carabande to represent that objectId.

I can order by the primary key as a workaround, but then I lose the ability to use eachUniqueKey later. I could write my own collection.each function that removes duplicates, but it feels like orderBy is doing more work than it should be.

dfahlander commented 2 months ago

orderBy is basically iterating the index. But it sounds like the unique flag is set on the collection when you do the ordering. Try whether the call to eachUniqueKey() could be done on another collection instance to not affect the one you do orderBy on. I think the problem city could be the collection instance being reused

1toddlewis commented 2 months ago

Interesting. Okay, I'll give it a try. I quickly implemented the workaround mentioned above, removing orderBy and writing my own de-duping function for collection.each(). But I'll check out using a different collection instance.