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

最新文章

  1. iOS 之消息推送(个推)---个人小结
  2. 在Application中集成Microsoft Translator服务之开发前准备
  3. C#------各种常见错误解决方法
  4. 【C】 04 - 表达式和语句
  5. 关于停止AsyncTask和Thread的问题
  6. js的设计模式
  7. shell脚本摘要
  8. 002 The Variables In Csharp
  9. Python - re - 正则表达式 - 怎么用
  10. 恩布企业 IM 安卓端 1.3,服务端 1.12 公布
  11. 主存与Cache的地址映射
  12. 微信内点击链接或扫描二维码可直接用外部浏览器打开H5链接的解决方案
  13. 理解MySql的锁&事务隔离级别
  14. 外显子分析思路总结(Exome Sequencing Analysis review)
  15. 配置本地无密码 SSH登录远程服务器
  16. C语言学习感受
  17. Oracle联合多个子查询(inner join)
  18. 2018.10.24 bzoj2064: 分裂(状压dp)
  19. jquery如何获取元素的滚动高度
  20. ArcGIS API for Silverlight——小滑块

热门文章

  1. smarty 的学习----ubuntu下初步配置
  2. java之两个字符串的比较
  3. pycharm常用快捷键与设置
  4. vim代码折叠功能
  5. win32控制台实现按任意键退出的功能
  6. IOS中用模型取代字典的好处
  7. JavaScript+CSS实现经典的树形导航栏
  8. BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口
  9. 有关linux下redis overcommit_memory的问题(转)
  10. 今天修改bug基本完成