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. maven 常用插件总结

    maven-javadoc-plugin (1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中. (2) 默认用法: pom.xml配置 & ...

  2. js四舍五入的bug和方法

    简单来说js使用原生toFixed(x)截取小数的时候会有误差,出现在比如var o = 0.3303;o.toFixed(3);//0.330 toFixed(x)本来也是一个获取四舍五入的截取方法 ...

  3. JSP中用include标签动态引入其它文件报错

    <jsp:include page="<%=path %>/include.jsp"></jsp:include> 报错:attribute f ...

  4. ecshop 嵌入地图加载不了问题

    在ecshop 添加一个标识商家地理位置信息. 百度地图,加载不出来,查了下发现跟 js/transport.js 与 js/utils.js 两个文件有关在需要插入地图的地方去掉这两个文件的引用 地 ...

  5. mysql命令行导出导入数据库

    一.MYSQL的命令行模式的设置: 桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中path为MYSQL的安装路径.二. ...

  6. CSS3 box-shadow(阴影使用)

    from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...

  7. UVa1605 - Building for UN(构造法)

    UVA - 1605 Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description ...

  8. 练习2 B题 - 求绝对值

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 求实数 ...

  9. c++:参数型别的推导

    STL源码剖析--侯捷 总结 尽管现在的很多语言支持参数类型的判别,但是c/c++并不支持这一特性. 但是我们可以通过一些技巧使得c++具有自动判别参数类型的特性. 模板 我们都知道在模板类和模板函数 ...

  10. mysql 修改max_allowed_packet

    -- 查询max_allowed_packetshow VARIABLES like '%max_allowed_packet%'; 修改 my.ini 或 my.cnf [mysqld] max_a ...