1- //����flag,�Ժ�ѧ
1+
2+
3+ -- -- -- -- -- -- -- -- -- -- -- --
4+ indexedDB |
5+ -- -- -- -- -- -- -- -- -- -- -- --
6+ # 创建db
7+ window . indexedDB . open ( dbName , version ) ;
8+
9+ # 创建index
10+ let objectStore = db . createObjectStore ( objectStoreName , options ) ;
11+
12+ # 创建事务
13+ let transaction = db . transaction ( objectStoreName , enum ) ;
14+ let objectStore = transaction . objectStore ( objectStoreName ) ;
15+
16+ # CRUD
17+ objectStore . add ( item ) ;
18+ objectStore . get ( id ) ;
19+ objectStore . put ( row ) ;
20+ objectStore . delete ( id ) ;
21+
22+ # 遍历
23+ objectStore . openCursor ( ) ;
24+
25+ # range
26+ . . .
27+
28+ -- -- -- -- -- -- -- -- -- -- -- --
29+ indexedDB - demo |
30+ -- -- -- -- -- -- -- -- -- -- -- --
31+ //db连接
32+ let db = null ;
33+ //db名称
34+ let dbName = 'database' ;
35+ //index名称
36+ let objectStoreName = 'users' ;
37+
38+ //打开链接
39+ let opener = window . indexedDB . open ( dbName , 1 ) ;
40+ opener . onsuccess = function ( event ) {
41+ db = event . target . result ;
42+ }
43+
44+ //在第一次创建db或者是版本号有修改的时候会执行
45+ opener . onupgradeneeded = function ( event ) {
46+ let db = event . target . result ;
47+
48+ //创建数据库存储对象
49+ let objectStore = db . createObjectStore ( objectStoreName , {
50+ keyPath :'id' ,
51+ autoIncrement :true
52+ } ) ;
53+
54+ //定义存储对象的数据项
55+ objectStore . createIndex ( 'id' , 'id' , {
56+ //唯一约束
57+ unique :true
58+ } ) ;
59+ objectStore . createIndex ( 'name' , 'name' ) ;
60+ objectStore . createIndex ( 'age' , 'age' ) ;
61+ }
62+
63+ function create ( db , item ) {
64+ //创建事务
65+ let transaction = db . transaction ( objectStoreName , 'readwrite' ) ; //readonly readwrite versionchange
66+ //通过事务,打开存储对象
67+ let objectStore = transaction . objectStore ( objectStoreName ) ;
68+ //插入记录
69+ objectStore . add ( item ) ;
70+ }
71+
72+ function update ( db , id ) {
73+ let transaction = db . transaction ( objectStoreName , 'readwrite' ) ;
74+ let objectStore = transaction . objectStore ( objectStoreName ) ;
75+ //根据id检索记录,返回游标
76+ let cursor = objectStore . get ( id ) ;
77+ cursor . onsuccess = function ( event ) {
78+ //成功检索结果集,如果检索失败,返回 undefined
79+ let row = event . target . result ;
80+ //修改记录值
81+ row . name = 'new name' ;
82+ //使put更新记录
83+ objectStore . put ( row ) ;
84+ }
85+ }
86+
87+ function remove ( db , id ) {
88+ let transaction = db . transaction ( objectStoreName , 'readwrite' ) ;
89+ let objectStore = transaction . objectStore ( objectStoreName ) ;
90+ //通过id删除记录
91+ objectStore . delete ( id ) ;
92+ }
93+
94+ function foreach ( db ) {
95+ let transaction = db . transaction ( objectStoreName , 'readwrite' ) ;
96+ let objectStore = transaction . objectStore ( objectStoreName ) ;
97+ let cursor = objectStore . openCursor ( ) ;
98+ cursor . onsuccess = function ( event ) {
99+ let cursor = event . target . result ;
100+ if ( cursor ) {
101+ let value = cursor . value ;
102+ //遍历到的值
103+ console . log ( value ) ;
104+ cursor . continue ( ) ; //继续遍历
105+ } else {
106+ //遍历完毕
107+ }
108+ }
109+ }
110+
111+
112+ function rangeEach ( db , start , end ) {
113+ let transaction = db . transaction ( objectStoreName , 'readwrite' ) ;
114+ let objectStore = transaction . objectStore ( objectStoreName ) ;
115+ /*
116+ bound(start,end,lowerOpen,upperOpen);
117+ id范围内,后面俩bool参数可以设置是否包含开始和结束(默认都为true)
118+ only(id);
119+ 仅仅是指定的id
120+ lowerBound(id);
121+ 小余参数的id
122+ upperBound(id);
123+ 大于参数的id
124+ */
125+ //创建范api,表示仅仅检索id start - end 之间的数据
126+ let range = IDBKeyRange . bound ( start , end ) ;
127+ //判断range检索的范围,是否包含了id = 5的记录,返回 bool
128+ let include = range . includes ( 5 ) ;
129+ //通过范围api创建游标
130+ let cursor = objectStore . openCursor ( range ) ;
131+ cursor . onsuccess = function ( event ) {
132+ let cursor = event . target . result ;
133+ if ( cursor ) {
134+ let value = cursor . value ;
135+ //遍历到的值
136+ console . log ( value ) ;
137+ cursor . continue ( ) ; //继续遍历
138+ } else {
139+ //遍历完毕
140+ }
141+ }
142+ }
0 commit comments