My Dexie Database db.playground
(with the id audioFiles
) holds infos about audio files. Those audio files have to be analysed by a user, who adds markers (so it is an analog process, not a programmatic one), and once that is done, the user clicks a save button and the audio file's attribute "analysed" must be set to "true".
The Dexie object looks like this:
// db.playground
{
id: 'audioFiles',
audio_file_data: {
0: {id: 0, analysed: false, …}
1: {id: 1, analysed: false, …}
2: {id: 2, analysed: false, …}
}
}
The amount of files in "audio_file_data" changes, of course, and the ids change. I get the current id of such an audio file via a variable this.fileData.id
(this is a reactive element in Vue).
My attempts to update the "analysed" property of a specific audio file have failed completely so far. This is what i tried to do (according to the Dexie docu, dot notation should be used with nested elements):
const updatedItem = await db.playground.update('audioFiles', {
`audio_file_data.${this.fileData.id}.analysed`: true
})
but this sets hell loose because of being so incredibly wrong.
This solution, however, which retrieves, manipulates and then reassigns the complete collection, does work:
const dbFileEntry = await db.playground.get({ id: 'audioFiles' })
const fileCollection = dbFileEntry.audio_file_data
const file = fileCollection.find(item => {
if (item.id === this.fileData.id) {
item.analysed = true
return item
}
return false
})
const updated = await db.playground.update('audioFiles', {
'audio_file_data': fileCollection
})
It feels very clumpsy, though. Is there a shorter, more concise, more professional way to accomplish that?
UPDATE
The real problem here is my approach. Therefore, even though the accepted answer does not directly solve my problem (and stackoverflow is extremely strict when it comes to things like this), I decided to mark it as "accepted" anyway, because it explains the path to a proper data structure setup. So just in case you find yourself in such a situation, consider rethinking your setup.
You're updating the entire object just to update one entry in an array. Perhaps it is easier to store each nested object as its own object in an object store? Then you can add, remove, edit, and reorder as you see fit. You can store the parent object's details redundantly within each subobject. Store parent id within each child object, then create an index on the parent id, then do getAll on the child object store using the parent id index to load all of the child objects (and sort).