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

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

  1. Ambry: LinkedIn’s Scalable Geo-Distributed Object Store

    https://github.com/linkedin/ambry http://www.open-open.com/lib/view/open1464828607502.html

  2. libhiredis.so.0.13: cannot open shared object file: No such file or director

    Hiredis安装步骤: tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip cd antirez-hiredis-3cc6a7f make 解决办法: m ...

  3. object store in javascript

  4. libhiredis.so.0.13: cannot open shared object file: No such file or directory in Unknown on line

    vim /etc/ld.so.conf添加 /usr/local/lib (此处为动态链接库的目录) ldconfig

  5. [PWA] 18. Clean the photo cache

    We cannot let photo always keep caching new data without clean the old data. If message is not displ ...

  6. [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 wil ...

  7. truncate table很慢之enq: RO - fast object reuse和local write wait等待分析

    使用ASSM表空间(默认模式)的时候,在dss系统中确实会出现truncate很慢的现象,但是他不会100%重现,得看概率.通过sql trace(对任何v$sysstat看起来资源消耗很低的情况,都 ...

  8. ODBC、OLE DB、 ADO的区别

    转自:http://blog.csdn.net/yinjingjing198808/article/details/7665577 一.ODBC ODBC的由来 1992年Microsoft和Syba ...

  9. 【转载】OLE DB, ADO, ODBC关系与区别

    原文:OLE DB, ADO, ODBC关系与区别 OLE DB, ADO, ODBC 一. ODBC(Open Database Connectivity,开放数据库互连)是微软公司开放服务结构(W ...

随机推荐

  1. python3 读写excel

    一直认为python3可以很快的实现很多简单的功能,今天要读excel表格数据,想来很简单,网上一搜,用xlrd即可,然后很多人给出了不同的版本,号称xlrd3,实际上官网一看,xlrd0.9.4兼容 ...

  2. 关于CenttOS的防火墙问题

    Fix “Unit iptables.service failed to load: No such file or directory” Error In CentOS7 最近在升级CentOS7遇 ...

  3. 小记搭建WAPM运行ThinkPHP时所需要的配置

    最近因为项目而接触到了Thinkphp,正在上手中.但昨天遇到几个问题,一下子牵连出之前搭建WAPM(windows+apache+PHP+MySQL)遗留的配置问题. aphache\conf目录下 ...

  4. delphi中DLL编程详解

    10.1 Windows的动态链接库原理 动态链接库(DLLs)是从C语言函数库和Pascal库单元的概念发展而来的.所有的C语言标准库函数都存放在某一函数库中,同时用户也可以用LIB程序创建自己的函 ...

  5. EasyUI篇のico

    所有图标位置: /themes/icons css引用位置: /themes/icon.css 可自行添加16*16的小图片放在icons中,icon.css代码添加即可 例如: .icon-logo ...

  6. spart快速大数据分析学习提纲(一)

    Spart是什么 Spart是一个用来实现快速而而通用的集群计算平台. 在速度方面,Spart扩展了广泛使用的Mapreduce计算模型,而且高效的支持更多的计算模式,包括交互式查询和流处理.Spar ...

  7. 剖析并利用Visual Studio Code在Mac上编译、调试c#程序【转】

    0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...

  8. COJ 0046 20701除法

    20701除法 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有满足表达式abcd ...

  9. MongoDB 任意代码执行漏洞(CVE-2013-4142)

    漏洞版本: MongoDB 2.4.0-2.4.4 漏洞描述: CVE ID:CVE-2013-4142 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种 ...

  10. 【转】java参数传递(超经典)

    原文网址:http://blog.sina.com.cn/s/blog_4b622a8e0100c1bo.html Java中的参数传递机制一直以来大家都争论不休,究竟是“传值”还是“传址(传引用)” ...