[PWA] 12. Intro to IndexedDB
Use the library indexedDB-promised.
Create a database and stroe:
import idb from 'idb'; // Open(db_name, version, cb)
var dbPromise = idb.open('test-db', 1, function (upgradeDb) {
var keyValStore = upgradeDb.createObjectStore('keyval'); // create table
//put(value, key)
keyValStore.put('world', 'Hello');
});
Notice put() function take value frist then key.
Read the key in stroe:
// 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);
});
Write value to store:
// set "foo" to be "bar" in "keyval"
dbPromise.then(function (db) {
var tx = db.transaction('keyval', 'readwrite'); // ready for add key value
var keyValStore = tx.objectStore('keyval'); // get the store
keyValStore.put('bar', 'foo'); // set the value, notice it may not be success if any step in transaction fail.
return tx.complete; // tx.complete is a promise
}).then(function () {
console.log('Added foo:bar to keyval');
});

[PWA] 12. Intro to IndexedDB的更多相关文章
- [PWA] 1. Intro to Service worker
Service worker stays between our browser and noetwork requests. It can help to fetch data from cache ...
- Angularjs学习---ubuntu12.04中karma安装配置中常见的问题总结
karma启动时出现了很多问题: 1.安装karma前提条件 安装karma首先要安装nodejs,npm然后才可以安装karma.nodejs,npm的安装过程可以参考文章:Angularjs学习- ...
- Angularjs学习---ubuntu12.04中karma安装配置
Angularjs学习---ubuntu12.04中karma安装配置中常见的问题总结 karma启动时出现了很多问题: 1.安装karma前提条件 安装karma首先要安装nodejs,npm然 ...
- golang结构体
声明结构体 定义结构体使用struct关键字.在结构体内部定义它们的成员变量和类型.如果成员变量的类型相同还可以把它们写到同一行. struct里面可以包含多个字段(属性) struct类型可以定义方 ...
- 手把手教你如何安装和使用Karma-Jasmine
注意:本文中出现的资料链接.karma的插件安装等,均可能需要翻$墙后才能正确执行. Jasmine是一个Javascript的测试工具,在Karma上运行Jasmine可完成Javascript的自 ...
- HTTP cache in depth
HTTP cache in depth HTTP 缓存 https://developers.google.com/web/fundamentals/performance/optimizing-co ...
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
随机推荐
- 行列的几种命名方式.line-.colume======.row-.col=========.tr-.td
第一种从line-height的语义来来讲,line表示hang再贴切不过了,colume纵列或者柱子也很形象,缺点太长了 第二种组合模拟rowspan,colspan而来,想必这个col也是colu ...
- wdcp-apache开启KeepAlive提高响应速度
因为我们的网站,媒体文件,js文件,css文件等都在同一个服务器上,并且,我们网站有非常多的图片,所以当建立好tcp链接之后,不应该马上关闭连接,因为每建立一次连接还要进行dns解析,以及启动一个ht ...
- hibernate 多对一的情况
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-mapping PUBLIC ...
- BZOJ 3872 Ant colony
Description There is an entrance to the ant hill in every chamber with only one corridor leading int ...
- MFC任务管理器task manager----进程的挂起与恢复--NtSuspendProcess&&NtResumeProcess
http://hi.baidu.com/xbbsh/blog/item/b73d3125462201084c088db1.html ---------------------------------- ...
- VBA -excel --遍历行
Sub filter1()Rem MsgBox ("AAAAA")Rem 1 get selected zoneRem 2 loop rows and check columnRe ...
- Boolean类源码分析
Boolean类里面的常量: Boolean.TRUE:这个是调用Boolean的构造函数,新建了一个Boolean对象,所以TRUE是Boolean类型的.用来避免每次都创建新的Boolean对象, ...
- zabbix 添加jvm监控
1. zabbix 服务端安装,监控jmx 需要--enable-java zabbix 客户端不需要 --enable-java 2.zabbix_server端安装jdk 安装jdk [root@ ...
- Unity NGUI实现序列帧动画播放
如题,要实现序列帧的播放导入图片的时候需要注意: (1)图片的命名要连续,如图: (2)将这些图片在NGUI中打包成Altas图集的时候图片应该在同一个Altas中: 这里以播放特效为例,满足条件时播 ...
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...