Javascript: Searching Indexeddb Using Multiple Indexes
I want to change from WebSql to Indexeddb. However, how would one do SQL queries like SELECT * FROM customers WHERE ssn = '444-44-4444' and emal = 'bill@bill@company.com' SELECT *
Solution 1:
For your example, compound index still work, but requires two compound indexes
objectStore.createIndex('ssn, email, age', ['ssn', 'email', 'age']); // corrected
objectStore.createIndex('ssn, email, name', ['ssn', 'email', 'name'])
And query like this
keyRange = IDBKeyRange.bound(
['444-44-4444', 'bill@bill@company.com'],
['444-44-4444', 'bill@bill@company.com', ''])
objectStore.index('ssn, email, age').get(keyRange)
objectStore.index('ssn, email, age').get(['444-44-4444', 'bill@bill@company.com', 30])
objectStore.index('ssn, email, name').get(['444-44-4444', 'bill@bill@company.com', 'Bill'])
Indexes can be arranged in any order, but it is most efficient if most specific come first.
Alternatively, you can also use key joining. Key joining requires four (single) indexes. Four indexes take less storage space and more general. For example, the following query require another compound index
SELECT * FROM customers WHEREssn='444-44-4444'andname='Bill'andage=30
Key joining still work for that query.
Post a Comment for "Javascript: Searching Indexeddb Using Multiple Indexes"