Google News
logo
JavaScript IndexedDB - Interview Questions
What are the two main types of searches in an Object store?
There are two ways of searching in an object store : searching by key-value or key range or another object field. This process requires an additional data structure named “index”.

Code Example :

Here's an example of both types of searches :
let request = indexedDB.open("myDB");
request.onsuccess = function(e) {
    let db = e.target.result;
    let tx = db.transaction("ObjectStore");
    let store = tx.objectStore("ObjectStore");
 
    // Get operation
    let getRequest = store.get(1);
    getRequest.onsuccess = function() {
        console.log("Get operation:", getRequest.result);
    };

    // Cursor operation
    let cursorRequest = store.openCursor();
    cursorRequest.onsuccess = function() {
        let cursor = cursorRequest.result;
        if (cursor) {
            console.log("Cursor operation:", cursor.value);
            cursor.continue();
        }
    };
};?

In this example, the "get" operation retrieves the object with key 1, and the "cursor" operation iterates over all the objects in the store.
Advertisement