-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexDB前端大量数据本地存储方案.js
284 lines (265 loc) · 7.96 KB
/
indexDB前端大量数据本地存储方案.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// indexDB.js
class IndexDB {
constructor() {
this.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB
this.personStore = 'LBM'
this.LBMDB = null
}
// 初始化IndexDB
DBinstall(cb) {
const that = this
if (!this.indexedDB) {
console.log('你的浏览器不支持IndexedDB')
} else {
console.log('你的浏览器支持IndexedDB')
}
console.log('成功初始化IndexDB')
// 打开数据库第一个为DB名字 第二个为DB版本
const IDBOpenDBRequest = this.indexedDB.open(this.personStore, 1)
// 3. 对打开数据库的事件进行处理
// 打开数据库成功后,自动调用onsuccess事件回调。
IDBOpenDBRequest.onsuccess = (e) => {
console.log('e------>数据库打开成功', e)
//
that.LBMDB = e.target.result
cb()
}
// 打开数据库失败
IDBOpenDBRequest.onerror = (e) => {
console.log(e.currentTarget.error.message)
}
// 第一次打开成功后或者版本有变化自动执行以下事件:一般用于初始化数据库。
IDBOpenDBRequest.onupgradeneeded = (e) => {
that.LBMDB = e.target.result // 获取到 demoDB对应的 IDBDatabase实例,也就是我们的数据库。
console.log('this.LBMDB-----1---->', that.LBMDB)
if (!that.LBMDB.objectStoreNames.contains(that.personStore)) {
// 如果表格不存在,创建一个新的表格(keyPath,主键 ; autoIncrement,是否自增),会返回一个对象(objectStore)
// objectStore就相当于数据库中的一张表。IDBObjectStore类型。
that.LBMDB.createObjectStore(that.personStore, {
keyPath: 'field'
// autoIncrement: true
})
}
}
}
// 插入一个字段
/**
*
* @param {*} data
* data : {
* field : '表名字段',
* value: {} , // 插入数据信息 Object
* }
* @param {*} cb
*/
DBAddData(data, cb) {
// 创建一个事务,类型:IDBTransaction,文档地址: https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction
console.log('data--1-->', data.data)
if (!this.LBMDB) return
if ((!data.data) instanceof Object) {
console.log('入参仅支持Object类型')
return
}
console.log(this.personStore)
const transaction = this.LBMDB.transaction(this.personStore, 'readwrite')
// 通过事务来获取IDBObjectStore
const store = transaction.objectStore(this.personStore)
// 获取当前字段
const request = store.get(data.data.field)
const field = data.data.field
console.log('request---->', request)
// 字段获取成功
request.onsuccess = (e) => {
const keys = e.target.result || { field, data: [] }
console.log('keys---1----', keys)
const newList = Array.isArray(keys.data) ? keys.data.concat([data.data]) : [keys.data].concat([data.data])
data.data = newList
console.log('data--3--->', data)
// 如果不是第一次添加数据 则走更新
if (data.data.length > 1) {
this.updateDataByKey(field, data.data, cb)
return
}
// 往store表中添加数据
// return
const addPersonRequest = store.add({ field, data: data.data })
console.log('addPersonRequest----->', addPersonRequest)
// 监听添加成功事件
addPersonRequest.onsuccess = (e) => {
const keys = e.target.result
console.log('keys---->', keys) // 打印添加成功数据的 主键(id)
if (typeof cb === 'function') {
return cb(keys)
}
}
// 监听失败事件
addPersonRequest.onerror = (e) => {
console.log('DBAdd----', e.target.error)
}
}
// console.log('keys---2----', JSON.stringify(keys))
console.log('store------', store)
}
// 获取字段
getDataByKey(field, cb) {
if (!this.LBMDB) return
console.log('value------>', field)
const transaction = this.LBMDB.transaction(this.personStore, 'readwrite')
const store = transaction.objectStore(this.personStore)
const request = store.get(field)
console.log('request---->', request)
request.onsuccess = (e) => {
const keys = e.target.result
console.log('keys----> get', keys)
if (typeof cb === 'function') {
return cb(!keys ? [] : keys.data)
}
}
}
// 删除字段
deleteDataByKey(key) {
if (!this.LBMDB) return
const transaction = this.LBMDB.transaction(this.personStore, 'readwrite')
const store = transaction.objectStore(this.personStore)
const request = store.delete(key)
console.log('request---->', request)
request.onsuccess = (e) => {
console.log('数据删除成功')
}
}
// 更新字段
updateDataByKey(field, value, cb) {
if (!this.LBMDB) return
const transaction = this.LBMDB.transaction(this.personStore, 'readwrite')
const store = transaction.objectStore(this.personStore)
const request = store.get(field)
console.log('request---->', request)
request.onsuccess = (e) => {
const keys = e.target.result
keys.data = value
// keys[field] = value
store.put(keys)
console.log('更新成功 ->>>>>>>>>', keys, value)
if (typeof cb === 'function') {
return cb(keys)
}
}
}
// 删除关闭数据库
closeDB() {
this.indexedDB.deleteDatabase(this.personStore);
this.LBMDB.close();
console.log('删除成功')
}
}
// index.js
<template>
<div>
<el-table
id="tables"
:data="showList">
<el-table-column label="ID" prop="id"></el-table-column>
<el-table-column label="名字" prop="name"></el-table-column>
</el-table>
<div ref="div">
<el-button @click="addData">添加数据</el-button>
<el-button @click="getData('Use')">获取数据</el-button>
<el-button @click="deleteData('Use')">删除数据</el-button>
<el-button @click="closeDB">删除表</el-button>
<el-button @click="importExcal">导出Excal</el-button>
</div>
<div class="block">
<el-pagination
@current-change="handleCurrentChange"
:current-page.sync="param.pageIndex"
:page-size="param.pageSize"
layout="prev, pager, next"
:total="total">
</el-pagination>
</div>
</div>
</template>
<script>
/* eslint-disable */
import '../../indexDB'
export default {
data() {
return {
list: [],
id: 1,
param: {
pageIndex: 1,
pageSize: 5
},
total: 0,
showList: [],
indexDBList: []
}
},
mounted() {
// 开启indexDB
this.$indexDB.DBinstall()
},
methods: {
// 自动新增
addItem() {
const time = setInterval(() => {
this.addData()
}, 1000)
},
// 删除
deleteData(key = 'Use') {
this.$indexDB.deleteDataByKey(key)
this.getData()
},
// 获取
getData(key = 'Use') {
this.$indexDB.getDataByKey(key, (keys) => {
if (keys) {
keys.reverse()
// if (keys.length > 0) {
// this.id = keys[0].value.id
// }
this.total = keys.length
this.showList = this.mockPage(keys).map(item => item.value)
}
})
},
// 新增一条
addData() {
this.id += 1
this.$indexDB.DBAddData(
{
data: {
field: 'Use',
value: {
id: this.id,
name: 'zhangsan' + this.id,
age: 18,
sex: '男'
}
}
},
(key) => {
this.getData()
}
)
},
handleCurrentChange(val) {
this.param.pageIndex = val
this.getData()
},
// 虚拟分页
mockPage(list = []) {
const start = this.param.pageSize * (this.param.pageIndex - 1)
if (list.length < 5) {
return list
}
return list.slice(start, start + 5)
},
closeDB() {
this.$indexDB.closeDB()
}
},
}
</script>