With Dexie, how to update multiple objects with a non-primary index?
Asked Answered
V

1

5

I want to do something like, myTable.update({ location: 'Paris'}, { location: '' } and have all the objects in myTable that have a location of 'Paris' get changed to have location set to empty string.

location is not a primary key, so there may be any number of objects in myTable that have location = 'Paris' before executing the command, but there should be none with location = 'Paris' after executing the command.

I come from a SQL background, so this seems like a very simple, basic function for a database. But the Dexie docs for Table.update() indicate that it only supports using a primary key, and consequently can only update at most one object. Surely, there's some way to do this other than looping to make multiple calls to update the database? Some command I haven't seen? Or am I failing to understand something about NoSql databases that I should know?

Vigilance answered 4/2, 2020 at 22:16 Comment(0)
L
6
myTable.where({location: 'Paris'}).modify({location: ''})

https://dexie.org/docs/Collection/Collection.modify()

Lemuellemuela answered 4/2, 2020 at 23:46 Comment(1)
I didn't realize modify would write to the db - I thought Collections were in memory only. Thanks!Vigilance

© 2022 - 2024 — McMap. All rights reserved.