zoukankan      html  css  js  c++  java
  • [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");
    });
  • 相关阅读:
    作业
    js判断数组对象属性是否含有某个值
    js去除对象数组中的空值
    js根据含有对象的数组中某一个属性进行排序
    import re
    1
    pycharm2020.3安装激活教程,pycharm2020激活教程
    Elasticsearch(安装篇):Windows下安装和运行Elasticsearch
    Spring Boot (日志篇):Log4j整合ELK,搭建实时日志平台
    Spring Boot(日志篇):Logback集成ELK,处理日志实例
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5503854.html
Copyright © 2011-2022 走看看