Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

The difference between 'Put' and 'Add' is unclear

Open mattgreenfield opened this issue 5 years ago • 3 comments

Both db.friends.put() and db.friends.add() appear to do the same thing and it's unclear what the difference is or when I should use each.

I think it would be handy to add this info to the docs and maybe link to / suggest the alternative in the description.

I'm using bulkAdd to add 6,000 rows and am wondering which is more performant. I do not need to edit any existing rows as the database is always empty as this point.

Thanks for your help

mattgreenfield avatar Aug 14 '19 09:08 mattgreenfield

Ok, thanks for the feedback. bulkAdd() is probably a bit more performant than bulkPut() but I haven't checked. If subscribing to the CRUD hooks, bulkAdd is definitely more performant than bulkPut() since the latter will need to query existing values to provide for the hook listener before performing the actual put operations.

Please feel free to edit the docs using the Edit button and it will generate a pullrequest. Otherwise I'll see what I can do to improve the docs. I'll definitely have a look.

dfahlander avatar Aug 14 '19 11:08 dfahlander

Is there any update on this? You say the hooks have an impact?

If I have 5000 objects and only want to add those to the DB that have no key conflict, what's the most important? In my case the object will not differ from the one in the db if it's already there, so I could delete all 5000 by their ID and add them but I currently use bulkPut assuming it achieves the same thing but with hooks involved ... I'm confused.

Edit: More confusion ... I'm using Svelte with liveQuery and actually don't know if that uses CRUD hooks or a new api that doesn't have that issue.

Giszmo avatar Sep 22 '22 04:09 Giszmo

Is there any update on this? You say the hooks have an impact?

If I have 5000 objects and only want to add those to the DB that have no key conflict, what's the most important? In my case the object will not differ from the one in the db if it's already there, so I could delete all 5000 by their ID and add them but I currently use bulkPut assuming it achieves the same thing but with hooks involved ... I'm confused.

bulkPut() is definitely the best and much more efficient than bulkDelete().then(()=>bulkAdd()).

Edit: More confusion ... I'm using Svelte with liveQuery and actually don't know if that uses CRUD hooks or a new api that doesn't have that issue.

liveQuery does not use the CRUD hooks. It uses middleware. So it does NOT slow down bulkPut(). It is actually designed to omit detailed change detection when bulkPutting or bulkAdding more than 50 objects - just to speed up the bulkPut/bulkAdd and upon transaction complete, trigger the changes on all live queries where the table is involved (rather than looking into each object and trying to match it to all detailed queries). The limit of 50 is for this case, to optimize for large bulkPut/bulkAdd operations. (if bulkPutting less than 50 objects, it will go through each of them and match them with ongoing live-queries to know which ones to trigger based on queried indexes and ranges + the IDs from previous results).

dfahlander avatar Sep 22 '22 06:09 dfahlander