[PWA] 14. Loop cursor

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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");
});

 

以上是关于[PWA] 14. Loop cursor的主要内容,如果未能解决你的问题,请参考以下文章

需要帮助理解 Cursor for loop

如何在 PL/SQL 中使用 BULK COLLECT 和 FORALL 替换 CURSOR FOR LOOP?

Oracle/PLSQL CURSOR FOR Loop

在 CURSOR FOR LOOP 中使用游标属性

CURSOR 和 LOOP 在 Oracle 数据库上无法正常工作

Oracle - 使用 CURSOR LOOP 进行批量更新