[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);
});

时间: 2024-12-15 06:55:51

[PWA] 13. New db and object store的相关文章

翻译 - 【Dojo Tutorials】Dojo Object Store

关注分离是良好编程的基础.保持展示与数据的分离是关键.受到HTML5存储API的启发,Dojo对象存储架构为数据交互建立了统一的接口. 为什么要使用Dojo对象存储? 关注分离是有组织,可管理程序的基础,在web应用中分离点主要是指数据与用户接口(在MVC架构中用户接口通常是指试图和控制器).受到HTML5存储API的启发,Dojo对象存储架构为数据交互建立了统一的接口.这些API是为促进松耦合的开发为存在的,可以让挂件和用户接口从多种源以一致的的方式与数据交互. Dojo对象存储允许你开发和使

[PWA] 15. Using The IDB Cache And Display Entries

We want to use IDB to store the wittr messages. The logic is when the page start: service worker will read the skeleton from the cache and show to the interface. read the message data from the IDB first instead of going to network. Show the data from

Java Object Model(一)

Java作为OOP语言,抽象性不言而喻.如果需要深入了解Java语言的实现机制,则不得不对Java语言中基础的概念有清晰的了解.今天是我在cnblog上写博客的第一天,希望 今天的博客可以是我成为未来"大牛"跨出的第一步. 面向对象语言中,对象概念其实挺抽象的,对于初学者甚至有开发经验的同志来说都不太容易弄明白.最近看到这篇牛人写的文章,觉得蛮受益的,和大家共同分享吧.翻译有 些拙劣,"大牛"请忽略我直接看原文,嘻嘻~. 原文出处链接:http://www.prog

[PWA] 18. Clean the photo cache

We cannot let photo always keep caching new data without clean the old data. If message is not display on the page anymore, we want to clean it. And also every 5 mins we want to clean the photo data. export default function IndexController(container)

翻译 - 【Dojo Tutorials】Connecting a Store to a Tree

原文:Connecting a Store to a Tree Dojo Tree组件是一个强大的展示层级数据的工具.该教程将展示如何连接tree与store来快速有效的展示层级数据. 介绍 Dojo Tree组件为展示层级数据提供了一种综合的,熟悉的,直观的方式.它将展示与数据分离.本文将提到为驱动一个tree提供数据的多种方法. 第一个例子使用一个静态tree,其数据从一个JSON文件中获取.这可以用于通过数据提供导航.第二个例子在这个设计的基础上扩展新的强大功能,如拖拽,维护一个动态的tr

讲的很详细的一篇关于object equals() & hashCode() 的文章

转: 讲的很详细的一篇关于object equals() & hashCode() 的文章 哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: 1 public native int hashCode(); 根据这个方法的声明可知,该方法返回一个int类型的数值,并且是本地方法,因此在Object类中并没有给出具体的实现. 为何Object类需要这样一个方法?它有什么作用呢?今天我们就来具体探讨一下hashCode方

(13)JavaScript之[HTML DOM元素][JS对象]

元素 1 /** 2 * HTML DOM 元素(节点)*/ 3 4 //创建新的HTML元素 5 var para = document.createElement('p'); 6 var node = document.createTextNode('这是一个新段落'); 7 para.appendChild(node); 8 9 var box = document.getElementById('box'); 10 box.appendChild(para); 11 12 //删除已有的

DAC Usage2:通过Extract,Register 和 Upgrade DAC,实现DB Schema的Migration

一,Introduce Extract DAC 是从现存的DB中创建DAC,抽取DB Object的definition 和 与之相关的实例级别的元素,比如Login,以及Login 和User之间的关系. The extraction process creates a DAC package file that contains definitions of the database objects and their related instance-level elements. For

Java提高学习之Object(2)

Equality 问:euqals()函数是用来做什么的? 答:equals()函数可以用来检查一个对象与调用这个equals()的这个对象是否相等. 问:为什么不用“==”运算符来判断两个对象是否相等呢? 答:虽然“==”运算符可以比较两个数据是否相等,但是要来比较对象的话,恐怕达不到预期的结果.就是说,“==”通过是否引用了同一个对象来判断两个对象是否相等,这被称为“引用相等”.这个运算符不能通过比较两个对象的内容来判断它们是不是逻辑上的相等. 问:使用Object类的equals()方法可