tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.39k stars 67 forks source link

Ensure that automatically added row IDs always increment #62

Closed aretrace closed 1 year ago

aretrace commented 1 year ago

Is your feature request related to a problem? Please describe. When adding rows with store.addRow() or useRowCallback() after removing rows in reverse order (based on sorted row IDs), the row IDs do not increment continuously. Instead, the first added row ID is one greater than the previous greatest ID, and subsequent row IDs decrease until they form a continuous sequence from 0 to the first incremented ID after row removals.

Describe the solution you'd like Ensure that the row IDs generated by store.addRow() and useRowCallback() always increment based on the current greatest ID value.

Describe alternatives you've considered As changing the behavior of store.addRow() and useRowCallback() could affect existing implementations, consider introducing a new method, store.appendRow(), that generates auto-incrementing row IDs based on the current greatest ID value. This new method would provide a more intuitive and expected behavior without disrupting existing code that relies on the current behavior.

jamesgpearce commented 1 year ago

Something about your description doesn't sound quite right (taking a new, higher Id before then re-using previous Ids). But anyway, that's not necessary to resolve in the context of your overall requirement, which, as I understand it, is simply to have no Id reuse at all - and just increment where you can.

TinyBase goes to some effort to reuse Ids (so that high frequency add/remove operations don't run out of Ids) but I don't mind exploring a new flag on those methods that disables reuse. Would that work?

jamesgpearce commented 1 year ago

And thanks for reporting and using TinyBase! Hope it's working out for you otherwise.

jamesgpearce commented 1 year ago

OK, this is enabled in v3.1.1.

It adds a reuseRowIds parameter to the addRow method and the useAddRowCallback hook. It defaults to true, for backwards compatibility, but if set to false, new Row Ids will not be reused unless the whole Table is deleted.