zoukankan      html  css  js  c++  java
  • [PWA] 13. New db and object store

    Create a db:

    import idb from 'idb';
    
    var dbPromise = idb.open('test-db', 2, 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'});
        }
    });

    The oldVersion switch between old db and new db. So here we create a new people db.

    ReadWrite:

    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');
        var peopleStore = tx.objectStore('people');
        return peopleStore.getAll();
    }).then(function (people) {
        console.table(people);
    });

    Group By:
    TO do gourp by we need to create index:

    import idb from 'idb';
    
    var dbPromise = idb.open('test-db', 3, 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');
        }
    });

    Group by animal:

    dbPromise.then(function (db) {
        var tx = db.transaction('people');
        var peopleStore = tx.objectStore('people');
        var animalIndex = peopleStore.index('animal');
        //return animalIndex.getAll(); // all the animals
        return animalIndex.getAll('cat'); // only cat
    }).then(function (people) {
        console.table(people);
    });

  • 相关阅读:
    jenkins
    Nexus5安装PostmarketOS(Alpine Linux)并装上Docker
    Abp Abp.AspNetZeroCore 2.0.0 2.1.1 Path
    音速启动 Vstart 5.7 win10手动移除后台设置主页
    Win10 20h2 19041 任务管理器 性能 蓝屏
    PostMan 在请求中自动添加Header
    LINQPad_6.9.15_Premium
    Byd 字段清单 通过JS端点导入
    SAP B1 修改数据库中b1版本号
    event是啥
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5503850.html
Copyright © 2011-2022 走看看