import idb from 'idb';

var dbPromise = idb.open('test-db', 4, 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');
case 3:
var peopleStore = upgradeDb.transaction.objectStore('people');
peopleStore.createIndex('age', 'age');
}
}); // read "hello" in "keyval"
dbPromise.then(function (db) {
var tx = db.transaction('keyval'); // Open a transaction
var keyValStore = tx.objectStore('keyval'); // read the store
return keyValStore.get('hello'); // get value by key
}).then(function (val) {
console.log('The value of "hello" is:', val);
}); 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', 'readwrite');
var peopleStore = tx.objectStore('people');
var ageIndex = peopleStore.index('age');
return ageIndex.openCursor();
}).then(function (cursor) {
if (!cursor) return;
return cursor.advance(1); // skip the first person
}).then(function logPerson(cursor) {
if(cursor.value.name == "Jie"){
cursor.delete(); // delete
}
if(cursor.value.name == "Jay"){
console.log("Cursor at:", cursor.value.name);
var joe = cursor.value;
joe.favoriteAnimal = "Bird";
cursor.update(joe); // update
}
return cursor.continue().then(logPerson);
}).then(function () {
console.log("DONE");
});

最新文章

  1. 【数据压缩】Huffman编码
  2. 使用JQuery能做什么(zz)
  3. byte[]和InputStream的相互转换[转载]
  4. windows下游戏服务器端框架Firefly安装说明及demo运行
  5. Python【第一篇】基础介绍
  6. Axure RP 8.0正式版下载地址 安装和汉化说明
  7. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
  8. (转载)Python装饰器学习
  9. JavaSE思维导图(五)
  10. ajax表单提交全路径
  11. PHP常用配置
  12. Python爬虫【实战篇】bilibili视频弹幕提取
  13. 异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu
  14. sqli-labs(十)(过滤注释符)
  15. 【推导】【数学期望】【冒泡排序】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem C. Earthquake
  16. WPF自定义代码启动应用程序启动方式
  17. PAT 1031 Hello World for U[一般]
  18. CentOS上安装 jdk
  19. IOS8 Playground介绍
  20. python多标签分类模版

热门文章

  1. 安装freebsd9 出现 mountroot>怎么办
  2. asp.net在应用母版的页面下采用了ModalPopupExtender弹出窗中应用autocomplete
  3. easyui tree 判断点击的节点是否还存在子节点
  4. Ajax实现的长轮询不阻塞同一时间内页面的其他Ajax请求(同域请求)
  5. DEDECMS栏目自定义字段添加
  6. 插件和过滤器装饰器开发中的感悟-python-django
  7. iscc2016-basic-心灵鸡汤
  8. Windows Phone 8学习 启动器
  9. Windows开发技术的历史
  10. Python 字典 Print 格式化