I have a store in IndexedDB where the Key path is for property Id and I also have an indexed array called IndexedWords
[{ Id: 1, IndexedWords: ['Foo', 'bar'] }, { Id: 2, IndexedWords: [] } }
I am trying to find all empty rows in the store and this by finding Id of all with value in IndexedWords + all Id of the store and intersecting those arrays. The idea works but the problem is that I am receiving the Ids for the IndexedWords x number of times where x matches the number of entries in IndexedWords and when I have 5000 rows and each row have 100+ words, the array is getting large and harder to work with. Is there any way of returning only distinct Ids from IndexedDB in the query? Current code below
var trans = db.transaction(storeName, IDBTransaction.READ_ONLY);
var index = trans.objectStore(storeName).index("IndexedWords");
var getAllRequest = index.getAllKeys();
getAllRequest.onsuccess = function (evt) {
items = evt.target.result;
}
If I understand your question, you could try creating a multiple entry index on IndexedWords and then iterating over the unique keys of the index.
I hope this helps, without the overhead of the IndexedDB API, since I was not able to get your IndexedDB sample code working.
Iterate over the words and create a new object containing a word as the key, with an array of Ids as the value if the word is in the IndexedWords for the item.
Create an empty object and some sample data
let index = [{ Id: 1, IndexedWords: ['Foo', 'bar'] }, { Id: 2, IndexedWords: ['blue', 'cat', 'mouse'] }, {Id: 242, IndexedWords: ['salmon', 'blue', 'bar']} ];
let wordz = {};
Create a flat array of all the unique words from the sample data, and remove the duplicates from the array of all of the words to get a list of distinct words.
let allWords = index.map((z) => (z.IndexedWords)).flat()
allWords = [...new Set(allWords)]
Iterate over each unique word and create a new object with the word as the key, with an array of Ids as the value
allWords.forEach( (word) => {
// create an empty array for the new wordz[word]
wordz[word] = []
// push the item Id onto wordz[word] //
index.forEach((item) => {
if(item.IndexedWords.indexOf(word)>=0) {
wordz[word].push(item.Id);
}
})
})
console.log(wordz)
Foo: [1]
bar: (2) [1, 242]
blue: (2) [2, 242]
cat: [2]
mouse: [2]
salmon: [242]