Filtering in a compound Index (IndexedDB)

31 Views Asked by At

I believe I must have read every question on how to implement filtering to IndexedDB, but it seems I cannot implement it into my code. In the end I followed those posts: this example for defining filtervalues this is a working example

I tryed to reduced the complexity for the post as much as I could, but I already "lost" an error in this shorter example. But still the main issue remains, the filtering cursor does not work. Is it because I define the var cursor twice?

code to build the DB:

    function getObjectStore(store_name, mode) {
        var tx = db.transaction(store_name, mode);
        return tx.objectStore(store_name);
    }

    open.onsuccess = function() {
        // Start a new transaction
        db = open.result;
        store = getObjectStore("testsStore", 'readwrite');

        // Initial data
        const initialtestData = [ //in if schleife ziehen
            { id: 'kurca_ll', testName: {sanskrit: 'kūrca', de: 'Kurcha'} , filter: {F_bodyRegion: 'linkes Bein', F_bodySide: 'l', F_testGrp: 'snayu'} },
            { id: 'kurca_rl', testName: {sanskrit: 'kūrca', de: 'Kurcha'} , filter: {F_bodyRegion: 'rechtes Bein', F_bodySide: 'r', F_testGrp: 'snayu'} },
        ];

        // Store values
        // initial DB filling 
        let i = null;
        function recurse() {
            i++;
            var key = document.getElementById('marmaID').value;
            store = getObjectStore("testsStore", 'readwrite');
                var testInfo = store.get(key);
                testInfo.onsuccess = function(evt) {
                    var req = store.openCursor(key);
                    var record = evt.target.result;
                    req.onsuccess = function(e) {
                        var cursor = e.target.result; 
                          if (cursor !== null) { // key already exist
                            //do things
                          } else { // key not exist
                            initialtestData.forEach((test) => {
                                var request = store.put(test);
                                request.onsuccess = (event) => {
                                    console.log(event.target.result + " initial filling DB DONE");
                                };
                            });
                              if (i > 2) {
                                return;
                              }
                            recurse()
                          }
                    };
                };
                testInfo.onerror = function(evt) {
                    console.log("ERROR");
                    i++;    
                }
        };
        recurse()
    }

Here is the filtering part

function filter() {
    console.log ("Filtering...")
    var value1 = "rechtes Bein"
    var value2 = "r"
    var value3 = "snayu"
    store = getObjectStore("testsStore", 'readonly');
    cursor = store.index('filter').openCursor(window.IDBKeyRange.only([value1, value2, value3]));
    cursor.onsuccess = function (event) {
        var cursor = event.target.result;
        if (cursor) {
            console.log ("Test")
            console.log (cursor.value)
            console.log (JSON.stringify(cursor.value))
            cursor.continue();
        }
        
    };
}

0

There are 0 best solutions below