• [PWA] 14. Loop cursor


    import idb from 'idb';
    
    var dbPromise = idb.open('test-db', 4, function (upgradeDb) {
        switch (upgradeDb.oldVersion) {
            case 0:
                // keyval store is already created at version 1
                var keyValStore = upgradeDb.createObjectStore('keyval');
                keyValStore.put("world", "hello");
            case 1:
                // new version
                upgradeDb.createObjectStore('people', {keyPath: 'name'});
            case 2:
                var peopleStore = upgradeDb.transaction.objectStore('people');
                peopleStore.createIndex('animal', 'favoriteAnimal');
            case 3:
                var peopleStore = upgradeDb.transaction.objectStore('people');
                peopleStore.createIndex('age', 'age');
        }
    });
    
    // read "hello" in "keyval"
    dbPromise.then(function (db) {
        var tx = db.transaction('keyval'); // Open a transaction
        var keyValStore = tx.objectStore('keyval'); // read the store
        return keyValStore.get('hello'); // get value by key
    }).then(function (val) {
        console.log('The value of "hello" is:', val);
    });
    
    
    dbPromise.then(function (db) {
        var tx = db.transaction('people', 'readwrite');
        var peopleStore = tx.objectStore('people');
    
        peopleStore.put({
            name: "John", // name is the key
            age: 23,
            favoriteAnimal: 'cat'
        });
        peopleStore.put({
            name: "Joe", // name is the key
            age: 21,
            favoriteAnimal: 'cat'
        });
        peopleStore.put({
            name: "Jie", // name is the key
            age: 22,
            favoriteAnimal: 'dog'
        });
        peopleStore.put({
            name: "Jay", // name is the key
            age: 24,
            favoriteAnimal: 'dog'
        });
        return tx.complete;
    }).then(function () {
        console.log("People are added");
    });
    
    dbPromise.then(function (db) {
        var tx = db.transaction('people', 'readwrite');
        var peopleStore = tx.objectStore('people');
        var ageIndex = peopleStore.index('age');
        return ageIndex.openCursor();
    }).then(function (cursor) {
        if (!cursor) return;
        return cursor.advance(1); // skip the first person
    }).then(function logPerson(cursor) {
        if(cursor.value.name == "Jie"){
            cursor.delete(); // delete
        }
        if(cursor.value.name == "Jay"){
            console.log("Cursor at:", cursor.value.name);
            var joe = cursor.value;
            joe.favoriteAnimal = "Bird";
            cursor.update(joe); // update
        }
        return cursor.continue().then(logPerson);
    }).then(function () {
        console.log("DONE");
    });
  • 相关阅读:
    SQL 2008 死锁进程查看。
    [读书]35前要掌握的66种基本能力第10、11节
    [读书]35前要掌握的66种基本能力第13、14节
    [读书]35前要掌握的66种基本能力第8节
    [读书]35前要掌握的66种基本能力第15、16、17、18节
    近日小节
    [读书]35前要掌握的66种基本能力第26、27、28、29节
    C# aspx 数据绑定集中(待修改)
    [读书]35前要掌握的66种基本能力第6节
    [读书]35前要掌握的66种基本能力第9节
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5503854.html
Copyright © 2020-2023  润新知