Skip to content

Commit faad2db

Browse files
committed
迁移到github
1 parent 5b46094 commit faad2db

11 files changed

+4725
-1
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": ["transform-es2015-arrow-functions"]
4+
}

README.md

+232-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,232 @@
1-
# IndexedDB
1+
> 作者:BLUE
2+
3+
> 日期:2017-11-19
4+
5+
> 描述:indexedDB类的API文档,所有方法均为实例方法
6+
7+
## 目录
8+
##### 1. 数据库操作idb
9+
- new IndexedDB({})
10+
- deletedb(name)
11+
- closeDB(db)
12+
- addData(db, storename, data[, callback])
13+
- putData(db, storename, data[, callback])
14+
- getByKey(db, storename, key[, callback])
15+
- deleteByKey(db, storename, key)
16+
- clearData(db, storename)
17+
- getDataByCur(db, storename[, keyRange][, callback])
18+
- getDataByCurInd(db, storename, indexKey[,keyRange][, callback])
19+
- edtDataByCur(db, storename[, keyRange, params[, callback]])
20+
- edtDataByCurInd(db, storename, indexKey[, keyRange, params[, callback]])
21+
- delDataByCur(db, storename, keyRange[, callback])
22+
- delDataByCurInd(db, storename, indexKey, keyRange[, callback])
23+
24+
##### 2. 操作条件KeyRange
25+
- eq(x)
26+
- lt(x)
27+
- lte(x)
28+
- gt(x)
29+
- gte(x)
30+
- ltgt(x, y)
31+
- ltegt(x, y)
32+
- ltgte(x, y)
33+
- ltegte(x, y)
34+
35+
36+
37+
## API详解
38+
#### 数据库操作
39+
**# new IndexedDB({})**
40+
41+
创建indexedDB实例
42+
- dbname <String> 【数据库名称】 默认“db_index”
43+
- version <String> 【数据库版本】 默认“1”
44+
- dbstore <JSON>【对象存储空间JSON】(name:存储空间名称,keyPath:主键字段,indexKey:如果要设置索引则添加该索引字段)
45+
- callback <Function> 【回调函数】 回调参数为数据库对象
46+
47+
48+
```
49+
let db=null;
50+
const idb = new IndexedDB({
51+
dbname: "db_index",
52+
version: "1",
53+
dbstore: [{ name: 'teachers', keyPath: "id", indexKey: "age" }],
54+
callback(db1) { db = db1; }
55+
});
56+
```
57+
58+
**# deletedb(name)**
59+
60+
删除数据库
61+
- name <String> 【数据库名称】
62+
---
63+
**# closeDB(db)**
64+
65+
关闭数据库
66+
67+
- db <Object> 【数据库对象】
68+
---
69+
**# addData(db, storename, data[, callback])**
70+
71+
添加数据到存储空间,如果遇到相同的主键会抛出错误
72+
- db <Object> 【数据库对象】
73+
- storename <String> 【存储空间名称】
74+
- data <JSON> 【数据JSON】
75+
- callback <Function> 【成功后的回调】
76+
77+
78+
```
79+
var teachers = [
80+
{id: 1001,name: "Byron",age: 21},
81+
{id: 1002,name: "Frank",age: 22},
82+
{id: 1003,name: "Aaron",age: 23}
83+
];
84+
85+
idb.addData(db, "teachers", teachers, () => {
86+
alert("ok")
87+
})
88+
```
89+
---
90+
**# putData(db, storename, data[, callback])**
91+
92+
添加数据到存储空间,如果遇到相同的主键会修改原有数据
93+
94+
- db <Object> 【数据库对象】
95+
- storename <String> 【存储空间名称】
96+
- data <JSON> 【数据JSON】
97+
- callback <Function> 【成功后的回调】
98+
99+
**# getByKey(db, storename, key[, callback])**
100+
101+
根据存储空间的键找到对应数据
102+
103+
- db <Object> 【数据库对象】
104+
- storename <String> 【存储空间名称】
105+
- key <Any> 【主键值】
106+
- callback <Function> 【回调函数,参数为数据对象】
107+
108+
```
109+
idb.getByKey(db, "teachers", idb.eq(1002), result => {
110+
console.log(result)
111+
})
112+
```
113+
---
114+
**# deleteByKey(db, storename, key)**
115+
116+
根据存储空间的键删除某一条记录
117+
- db <Object> 【数据库对象】
118+
- storename <String> 【存储空间名称】
119+
- key <Any> 【主键值】
120+
---
121+
**# clearData(db, storename)**
122+
123+
删除存储空间全部记录
124+
125+
- db <Object> 【数据库对象】
126+
- storename <String> 【存储空间名称】
127+
---
128+
**# getDataByCur(db, storename[, keyRange][, callback])**
129+
130+
通过游标查询记录
131+
132+
- db <Object> 【数据库对象】
133+
- storename <String> 【存储空间名称】
134+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
135+
- callback <Function> 【回调函数,参数为数据JSON】
136+
137+
---
138+
```
139+
idb.getDataByCur(db, "teachers", idb.eq(1004), result => {
140+
console.log(result)
141+
})
142+
```
143+
---
144+
**# getDataByCurInd(db, storename, indexKey[,keyRange][, callback])**
145+
146+
通过索引游标查询记录
147+
148+
- db <Object> 【数据库对象】
149+
- storename <String> 【存储空间名称】
150+
- indexKey <String> 【索引字段名称】
151+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
152+
- callback <Function> 【回调函数,参数为数据JSON】
153+
154+
155+
```
156+
idb.getDataByCurInd(db, "teachers", "age", 24, result => {
157+
console.log(result)
158+
})
159+
```
160+
---
161+
**# edtDataByCur(db, storename[, keyRange, params[, callback]])**
162+
163+
通过游标更新记录
164+
165+
- db <Object> 【数据库对象】
166+
- storename <String> 【存储空间名称】
167+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
168+
- params <Object> 【修改数据的参数对象】
169+
- callback <Function> 【更新成功回调】
170+
171+
172+
```
173+
idb.edtDataByCur(db, "teachers", 1002, params2, () => {
174+
alert("ok")
175+
});
176+
```
177+
---
178+
**# edtDataByCurInd(db, storename, indexKey[, keyRange, params[, callback]])**
179+
180+
通过索引游标更新记录
181+
182+
183+
- db <Object> 【数据库对象】
184+
- storename <String> 【存储空间名称】
185+
- indexKey <String> 【索引字段名称】
186+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
187+
- params <Object> 【修改数据的参数对象】
188+
- callback <Function> 【更新成功回调】
189+
190+
191+
```
192+
idb.edtDataByCurInd(db, "teachers", "age", idb.ltegt(21, 24), { name: "PINK" }, () => {
193+
alert("OK")
194+
})
195+
```
196+
---
197+
**# delDataByCur(db, storename, keyRange[, callback])**
198+
199+
通过游标删除记录
200+
201+
202+
- db <Object> 【数据库对象】
203+
- storename <String> 【存储空间名称】
204+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
205+
- callback <Function> 【删除成功回调】
206+
---
207+
**# delDataByCurInd(db, storename, indexKey, keyRange[, callback])**
208+
209+
通过索引游标删除记录
210+
211+
- db <Object> 【数据库对象】
212+
- storename <String> 【存储空间名称】
213+
- indexKey <String> 【索引字段名称】
214+
- keyRange <Any> 【游标条件IDBKeyRange对象或者其他】
215+
- callback <Function> 【删除成功回调】
216+
---
217+
#### 操作条件
218+
219+
每个方法都返回一个KeyRange对象
220+
221+
操作 | 翻译 | 函数
222+
---|---|---
223+
等于 | keys = x| eq(x)
224+
小于 | All keys < x| lt(x)
225+
小于等于 | All keys ≤ x | lte(x)
226+
大于 | All keys > x | gt(x)
227+
大于等于 | All keys ≥ x | gte(x)
228+
大于且小于 | All keys > x &&< y| ltgt(x, y)
229+
大于等于且小于 | All keys ≥ x &&< y | ltegt(x, y)
230+
大于且小于等于 | All keys > x && ≤ y | ltgte(x, y)
231+
大于等于且小于等于 | All keys ≥ x && ≤ y| ltegte(x, y)
232+

demo.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
10+
<style>
11+
button{
12+
margin: 10px;
13+
padding: 10px;
14+
}
15+
</style>
16+
</head>
17+
18+
<body>
19+
<button id="dropdb">删除数据库</button>
20+
<button id="add">add添加数据</button>
21+
<button id="put">put添加数据</button>
22+
<button id="get">键获取数据</button>
23+
<button id="del">键删除数据</button>
24+
<button id="delall">删除所有数据</button>
25+
<button id="curget">游标获取数据</button>
26+
<button id="curindexget">通过索引游标查询记录</button>
27+
<button id="curedt">通过游标更新记录</button>
28+
<button id="curindexedt">通过索引游标更新记录</button>
29+
<button id="curdel">通过游标删除记录</button>
30+
<button id="curindexdel">通过索引游标删除记录</button>
31+
</body>
32+
<script src="./dist/js/build.js"></script>
33+
</html>

0 commit comments

Comments
 (0)