[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);
});
[PWA] 13. New db and object store的更多相关文章
- Ambry: LinkedIn’s Scalable Geo-Distributed Object Store
https://github.com/linkedin/ambry http://www.open-open.com/lib/view/open1464828607502.html
- 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 ...
- object store in javascript
- 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
- [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 ...
- [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 ...
- truncate table很慢之enq: RO - fast object reuse和local write wait等待分析
使用ASSM表空间(默认模式)的时候,在dss系统中确实会出现truncate很慢的现象,但是他不会100%重现,得看概率.通过sql trace(对任何v$sysstat看起来资源消耗很低的情况,都 ...
- ODBC、OLE DB、 ADO的区别
转自:http://blog.csdn.net/yinjingjing198808/article/details/7665577 一.ODBC ODBC的由来 1992年Microsoft和Syba ...
- 【转载】OLE DB, ADO, ODBC关系与区别
原文:OLE DB, ADO, ODBC关系与区别 OLE DB, ADO, ODBC 一. ODBC(Open Database Connectivity,开放数据库互连)是微软公司开放服务结构(W ...
随机推荐
- objective-c基础教程
command+shift+c foundation框架包含的头文件 /system/library/frameworkks/foundation.framework/headers/
- IOS--UISlider的使用方法
IOS--UISlider的使用方法 // UISlider的常用方法 UISlider *oneSlider = [[UISlider alloc] init]; // 最常用 oneSlider. ...
- iOS播放短的音效
在IOS中,有的时候需要播放很简短的声音文件,比如系统声音等,我们需要使用到下面的方式来播放声音: // 一.引入头文件 #import <AudioToolbox/AudioToolbox.h ...
- Struts2 Tomcat的配置
1. 下载Struts2包,网站http://struts.apache.org/download.cgi#struts2315 2. 将struts-2.3.15-all.zip 包解压到本地 3. ...
- [BZOJ 1188] [HNOI2007] 分裂游戏 【博弈论|SG函数】
题目链接:BZOJ - 1188 题目分析 我们把每一颗石子看做一个单个的游戏,它的 SG 值取决于它的位置. 对于一颗在 i 位置的石子,根据游戏规则,它的后继状态就是枚举符合条件的 j, k.然后 ...
- 图片的css自适应
当需要css来缩放图片的时候,可以采用外层容器100%或者任意百分比, 内层图片img tag 没有宽高,用sass写经过断点后的mixin中的样式就是这样: .workscon_section{ w ...
- Dll方式的线程,需要引用这个
{== D6DLLSynchronizer =================================================} {: This unit handles the D6 ...
- mysql 安装1
Linux 安装mysql.tar.gz包(2012-09-28 19:25:06) 标签: it 分类: linux学习编 我用的mysql的版本的是:mysql--linux-i686-icc-g ...
- 【HDOJ】2579 Dating with girls(2)
简单BFS. /* 2579 */ #include <iostream> #include <queue> #include <cstdio> #include ...
- QT、QTE、qtopia区别
QT.QTE.qtopia区别 Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序 ...