@pisell/core 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/indexDB/index.d.ts +26 -10
- package/es/indexDB/index.js +234 -170
- package/es/logger/index.d.ts +3 -3
- package/es/logger/index.js +17 -15
- package/lib/indexDB/index.d.ts +26 -10
- package/lib/indexDB/index.js +462 -384
- package/lib/logger/index.d.ts +3 -3
- package/lib/logger/index.js +11 -10
- package/package.json +7 -7
package/es/indexDB/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import dayjs from 'dayjs';
|
|
|
13
13
|
* IndexDB 管理器模块
|
|
14
14
|
*
|
|
15
15
|
* 这个模块提供了对浏览器 IndexedDB 的简单封装,并在不支持 IndexedDB 的环境中
|
|
16
|
-
*
|
|
16
|
+
* 自动降级使用内存存储作为备选存储方案。
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -31,7 +31,7 @@ import dayjs from 'dayjs';
|
|
|
31
31
|
*/
|
|
32
32
|
/**
|
|
33
33
|
* IndexDB 管理器类
|
|
34
|
-
* 提供对 IndexedDB
|
|
34
|
+
* 提供对 IndexedDB 的简单封装,支持自动降级到内存存储
|
|
35
35
|
* @class IndexDBManager
|
|
36
36
|
*/
|
|
37
37
|
var IndexDBManager = /*#__PURE__*/function () {
|
|
@@ -40,6 +40,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
40
40
|
* @param {DBOptions} options - 数据库配置选项
|
|
41
41
|
*/
|
|
42
42
|
function IndexDBManager(app, options) {
|
|
43
|
+
var _options$timeout,
|
|
44
|
+
_this = this;
|
|
43
45
|
_classCallCheck(this, IndexDBManager);
|
|
44
46
|
_defineProperty(this, "db", null);
|
|
45
47
|
_defineProperty(this, "dbName", void 0);
|
|
@@ -47,23 +49,73 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
47
49
|
_defineProperty(this, "stores", void 0);
|
|
48
50
|
_defineProperty(this, "useIndexDB", void 0);
|
|
49
51
|
_defineProperty(this, "app", void 0);
|
|
52
|
+
// 内存存储:每个 store 使用一个 Map,key 为主键,value 为数据
|
|
53
|
+
_defineProperty(this, "memoryStorage", new Map());
|
|
54
|
+
// 操作超时时间(毫秒)
|
|
55
|
+
_defineProperty(this, "timeout", 5000);
|
|
50
56
|
this.app = app;
|
|
51
57
|
this.dbName = options.dbName;
|
|
52
58
|
this.version = options.version;
|
|
53
59
|
this.stores = options.stores;
|
|
54
60
|
this.useIndexDB = IndexDBManager.isSupported();
|
|
61
|
+
this.timeout = (_options$timeout = options.timeout) !== null && _options$timeout !== void 0 ? _options$timeout : 5000; // 默认 5 秒超时
|
|
62
|
+
|
|
63
|
+
// 初始化内存存储的各个 store
|
|
64
|
+
if (!this.useIndexDB) {
|
|
65
|
+
this.stores.forEach(function (store) {
|
|
66
|
+
_this.memoryStorage.set(store.name, new Map());
|
|
67
|
+
});
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
|
|
57
71
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
72
|
+
* 超时包装器 - 为 Promise 添加超时控制
|
|
73
|
+
* @param {Promise<T>} promise - 需要包装的 Promise
|
|
74
|
+
* @param {string} operation - 操作名称(用于错误提示)
|
|
75
|
+
* @returns {Promise<T>} 带超时控制的 Promise
|
|
76
|
+
* @private
|
|
61
77
|
*/
|
|
62
78
|
_createClass(IndexDBManager, [{
|
|
79
|
+
key: "withTimeout",
|
|
80
|
+
value: function withTimeout(promise, operation) {
|
|
81
|
+
var _this2 = this;
|
|
82
|
+
var timeoutId = null;
|
|
83
|
+
var timeoutPromise = new Promise(function (_, reject) {
|
|
84
|
+
timeoutId = setTimeout(function () {
|
|
85
|
+
var error = new Error("\u64CD\u4F5C\u8D85\u65F6: ".concat(operation, " \u5728 ").concat(_this2.timeout, "ms \u5185\u672A\u5B8C\u6210"));
|
|
86
|
+
_this2.app.logger.addLog({
|
|
87
|
+
type: 'error',
|
|
88
|
+
title: '[ IndexDB ] TIMEOUT',
|
|
89
|
+
metadata: {
|
|
90
|
+
msg: "\u64CD\u4F5C\u8D85\u65F6: ".concat(operation),
|
|
91
|
+
timeout: _this2.timeout,
|
|
92
|
+
operation: operation
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
reject(error);
|
|
96
|
+
}, _this2.timeout);
|
|
97
|
+
});
|
|
98
|
+
return Promise.race([promise.then(function (result) {
|
|
99
|
+
// Promise 成功完成,清除超时定时器
|
|
100
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
101
|
+
return result;
|
|
102
|
+
}, function (error) {
|
|
103
|
+
// Promise 失败,清除超时定时器
|
|
104
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
105
|
+
throw error;
|
|
106
|
+
}), timeoutPromise]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 初始化数据库连接
|
|
111
|
+
* 如果环境不支持 IndexedDB,将自动使用内存存储
|
|
112
|
+
* @returns {Promise<boolean>} 连接是否成功
|
|
113
|
+
*/
|
|
114
|
+
}, {
|
|
63
115
|
key: "connect",
|
|
64
116
|
value: (function () {
|
|
65
117
|
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
66
|
-
var
|
|
118
|
+
var _this3 = this;
|
|
67
119
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
68
120
|
while (1) switch (_context.prev = _context.next) {
|
|
69
121
|
case 0:
|
|
@@ -73,11 +125,11 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
73
125
|
}
|
|
74
126
|
return _context.abrupt("return", true);
|
|
75
127
|
case 2:
|
|
76
|
-
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
77
|
-
var request = indexedDB.open(
|
|
128
|
+
return _context.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
129
|
+
var request = indexedDB.open(_this3.dbName, _this3.version);
|
|
78
130
|
request.onerror = function (event) {
|
|
79
131
|
var _event$target;
|
|
80
|
-
|
|
132
|
+
_this3.app.logger.addLog({
|
|
81
133
|
type: 'error',
|
|
82
134
|
title: '[ IndexDB ] ERROR',
|
|
83
135
|
metadata: {
|
|
@@ -88,11 +140,11 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
88
140
|
});
|
|
89
141
|
};
|
|
90
142
|
request.onsuccess = function (event) {
|
|
91
|
-
|
|
143
|
+
_this3.db = event.target.result;
|
|
92
144
|
resolve(true);
|
|
93
145
|
};
|
|
94
146
|
request.onblocked = function () {
|
|
95
|
-
|
|
147
|
+
_this3.app.logger.addLog({
|
|
96
148
|
type: 'error',
|
|
97
149
|
title: '[ IndexDB ] blocked',
|
|
98
150
|
metadata: {
|
|
@@ -105,7 +157,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
105
157
|
var db = event.target.result;
|
|
106
158
|
db.onerror = function (event) {
|
|
107
159
|
var _event$target2;
|
|
108
|
-
|
|
160
|
+
_this3.app.logger.addLog({
|
|
109
161
|
type: 'error',
|
|
110
162
|
title: '[ IndexDB ] ERROR',
|
|
111
163
|
metadata: {
|
|
@@ -115,7 +167,19 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
115
167
|
}
|
|
116
168
|
});
|
|
117
169
|
};
|
|
118
|
-
|
|
170
|
+
db.onclose = function (event) {
|
|
171
|
+
var _event$target3;
|
|
172
|
+
_this3.app.logger.addLog({
|
|
173
|
+
type: 'error',
|
|
174
|
+
title: '[ IndexDB ] CLOSE',
|
|
175
|
+
metadata: {
|
|
176
|
+
msg: 'DB close',
|
|
177
|
+
data: event,
|
|
178
|
+
message: event === null || event === void 0 || (_event$target3 = event.target) === null || _event$target3 === void 0 || (_event$target3 = _event$target3.error) === null || _event$target3 === void 0 ? void 0 : _event$target3.message
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
_this3.stores.forEach(function (store) {
|
|
119
183
|
if (!db.objectStoreNames.contains(store.name)) {
|
|
120
184
|
var _store$indexes;
|
|
121
185
|
var objectStore = db.createObjectStore(store.name, {
|
|
@@ -127,7 +191,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
127
191
|
}
|
|
128
192
|
});
|
|
129
193
|
};
|
|
130
|
-
}));
|
|
194
|
+
}), 'connect'));
|
|
131
195
|
case 3:
|
|
132
196
|
case "end":
|
|
133
197
|
return _context.stop();
|
|
@@ -140,17 +204,21 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
140
204
|
return connect;
|
|
141
205
|
}()
|
|
142
206
|
/**
|
|
143
|
-
*
|
|
207
|
+
* 获取内存存储中指定 store 的 Map
|
|
144
208
|
* @param {string} storeName - 存储对象名称
|
|
145
|
-
* @
|
|
146
|
-
* @returns {string} 格式化的存储键
|
|
209
|
+
* @returns {Map<string | number, any>} 存储 Map
|
|
147
210
|
* @private
|
|
148
211
|
*/
|
|
149
212
|
)
|
|
150
213
|
}, {
|
|
151
|
-
key: "
|
|
152
|
-
value: function
|
|
153
|
-
|
|
214
|
+
key: "getMemoryStore",
|
|
215
|
+
value: function getMemoryStore(storeName) {
|
|
216
|
+
var store = this.memoryStorage.get(storeName);
|
|
217
|
+
if (!store) {
|
|
218
|
+
store = new Map();
|
|
219
|
+
this.memoryStorage.set(storeName, store);
|
|
220
|
+
}
|
|
221
|
+
return store;
|
|
154
222
|
}
|
|
155
223
|
|
|
156
224
|
/**
|
|
@@ -165,10 +233,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
165
233
|
key: "add",
|
|
166
234
|
value: (function () {
|
|
167
235
|
var _add = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(storeName, data) {
|
|
168
|
-
var
|
|
236
|
+
var _this4 = this;
|
|
169
237
|
var log,
|
|
170
|
-
|
|
238
|
+
storeConfig,
|
|
239
|
+
keyPath,
|
|
171
240
|
key,
|
|
241
|
+
memStore,
|
|
172
242
|
uuid,
|
|
173
243
|
_args2 = arguments;
|
|
174
244
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
@@ -176,15 +246,19 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
176
246
|
case 0:
|
|
177
247
|
log = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : false;
|
|
178
248
|
if (this.useIndexDB) {
|
|
179
|
-
_context2.next =
|
|
249
|
+
_context2.next = 8;
|
|
180
250
|
break;
|
|
181
251
|
}
|
|
182
|
-
|
|
252
|
+
// 使用内存存储
|
|
253
|
+
storeConfig = this.stores.find(function (s) {
|
|
183
254
|
return s.name === storeName;
|
|
184
|
-
})
|
|
185
|
-
|
|
255
|
+
});
|
|
256
|
+
keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
|
|
257
|
+
key = data[keyPath];
|
|
258
|
+
memStore = this.getMemoryStore(storeName);
|
|
259
|
+
memStore.set(key, data);
|
|
186
260
|
return _context2.abrupt("return", data);
|
|
187
|
-
case
|
|
261
|
+
case 8:
|
|
188
262
|
uuid = "[ IndexDB ] ADD: - ".concat(storeName, " - ").concat(dayjs().valueOf());
|
|
189
263
|
if (log) {
|
|
190
264
|
this.app.logger.addLog({
|
|
@@ -196,10 +270,10 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
196
270
|
}
|
|
197
271
|
});
|
|
198
272
|
}
|
|
199
|
-
return _context2.abrupt("return", new Promise(function (resolve, reject) {
|
|
200
|
-
if (!
|
|
273
|
+
return _context2.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
274
|
+
if (!_this4.db) {
|
|
201
275
|
if (log) {
|
|
202
|
-
|
|
276
|
+
_this4.app.logger.addLog({
|
|
203
277
|
type: 'error',
|
|
204
278
|
title: uuid,
|
|
205
279
|
metadata: {
|
|
@@ -211,12 +285,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
211
285
|
reject(new Error('数据库未连接'));
|
|
212
286
|
return;
|
|
213
287
|
}
|
|
214
|
-
var transaction =
|
|
288
|
+
var transaction = _this4.db.transaction(storeName, 'readwrite');
|
|
215
289
|
var store = transaction.objectStore(storeName);
|
|
216
290
|
var request = store.add(data);
|
|
217
291
|
request.onsuccess = function () {
|
|
218
292
|
if (log) {
|
|
219
|
-
|
|
293
|
+
_this4.app.logger.addLog({
|
|
220
294
|
type: 'info',
|
|
221
295
|
title: uuid,
|
|
222
296
|
metadata: {
|
|
@@ -228,7 +302,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
228
302
|
};
|
|
229
303
|
request.onerror = function () {
|
|
230
304
|
if (log) {
|
|
231
|
-
|
|
305
|
+
_this4.app.logger.addLog({
|
|
232
306
|
type: 'error',
|
|
233
307
|
title: uuid,
|
|
234
308
|
metadata: {
|
|
@@ -241,7 +315,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
241
315
|
transaction.oncomplete = function () {
|
|
242
316
|
console.log('✅ 添加事务完成');
|
|
243
317
|
if (log) {
|
|
244
|
-
|
|
318
|
+
_this4.app.logger.addLog({
|
|
245
319
|
type: 'info',
|
|
246
320
|
title: uuid,
|
|
247
321
|
metadata: {
|
|
@@ -254,7 +328,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
254
328
|
transaction.onerror = function (e) {
|
|
255
329
|
var _transaction$error;
|
|
256
330
|
if (log) {
|
|
257
|
-
|
|
331
|
+
_this4.app.logger.addLog({
|
|
258
332
|
type: 'error',
|
|
259
333
|
title: uuid,
|
|
260
334
|
metadata: {
|
|
@@ -266,7 +340,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
266
340
|
};
|
|
267
341
|
transaction.onabort = function (e) {
|
|
268
342
|
if (log) {
|
|
269
|
-
|
|
343
|
+
_this4.app.logger.addLog({
|
|
270
344
|
type: 'error',
|
|
271
345
|
title: uuid,
|
|
272
346
|
metadata: {
|
|
@@ -276,8 +350,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
276
350
|
}
|
|
277
351
|
return reject(new Error('事务被中止'));
|
|
278
352
|
};
|
|
279
|
-
}));
|
|
280
|
-
case
|
|
353
|
+
}), "add(".concat(storeName, ")")));
|
|
354
|
+
case 11:
|
|
281
355
|
case "end":
|
|
282
356
|
return _context2.stop();
|
|
283
357
|
}
|
|
@@ -299,8 +373,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
299
373
|
key: "exists",
|
|
300
374
|
value: (function () {
|
|
301
375
|
var _exists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(storeName, key) {
|
|
302
|
-
var
|
|
303
|
-
var
|
|
376
|
+
var _this5 = this;
|
|
377
|
+
var memStore;
|
|
304
378
|
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
305
379
|
while (1) switch (_context3.prev = _context3.next) {
|
|
306
380
|
case 0:
|
|
@@ -308,15 +382,16 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
308
382
|
_context3.next = 3;
|
|
309
383
|
break;
|
|
310
384
|
}
|
|
311
|
-
|
|
312
|
-
|
|
385
|
+
// 使用内存存储
|
|
386
|
+
memStore = this.getMemoryStore(storeName);
|
|
387
|
+
return _context3.abrupt("return", memStore.has(key));
|
|
313
388
|
case 3:
|
|
314
|
-
return _context3.abrupt("return", new Promise(function (resolve, reject) {
|
|
315
|
-
if (!
|
|
389
|
+
return _context3.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
390
|
+
if (!_this5.db) {
|
|
316
391
|
reject(new Error('数据库未连接'));
|
|
317
392
|
return;
|
|
318
393
|
}
|
|
319
|
-
var transaction =
|
|
394
|
+
var transaction = _this5.db.transaction(storeName, 'readonly');
|
|
320
395
|
var store = transaction.objectStore(storeName);
|
|
321
396
|
var request = store.count(key);
|
|
322
397
|
request.onsuccess = function () {
|
|
@@ -325,7 +400,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
325
400
|
request.onerror = function () {
|
|
326
401
|
return reject(new Error('检查数据存在性失败'));
|
|
327
402
|
};
|
|
328
|
-
}));
|
|
403
|
+
}), "exists(".concat(storeName, ")")));
|
|
329
404
|
case 4:
|
|
330
405
|
case "end":
|
|
331
406
|
return _context3.stop();
|
|
@@ -350,11 +425,11 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
350
425
|
key: "get",
|
|
351
426
|
value: (function () {
|
|
352
427
|
var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(storeName, key) {
|
|
353
|
-
var
|
|
428
|
+
var _this6 = this;
|
|
354
429
|
var log,
|
|
355
430
|
uuid,
|
|
356
|
-
|
|
357
|
-
|
|
431
|
+
_memStore$get,
|
|
432
|
+
memStore,
|
|
358
433
|
_args4 = arguments;
|
|
359
434
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
360
435
|
while (1) switch (_context4.prev = _context4.next) {
|
|
@@ -371,20 +446,17 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
371
446
|
});
|
|
372
447
|
}
|
|
373
448
|
if (this.useIndexDB) {
|
|
374
|
-
_context4.next =
|
|
449
|
+
_context4.next = 6;
|
|
375
450
|
break;
|
|
376
451
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
case 8:
|
|
384
|
-
return _context4.abrupt("return", new Promise(function (resolve, reject) {
|
|
385
|
-
if (!_this4.db) {
|
|
452
|
+
// 使用内存存储
|
|
453
|
+
memStore = this.getMemoryStore(storeName);
|
|
454
|
+
return _context4.abrupt("return", (_memStore$get = memStore.get(key)) !== null && _memStore$get !== void 0 ? _memStore$get : null);
|
|
455
|
+
case 6:
|
|
456
|
+
return _context4.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
457
|
+
if (!_this6.db) {
|
|
386
458
|
if (log) {
|
|
387
|
-
|
|
459
|
+
_this6.app.logger.addLog({
|
|
388
460
|
type: 'error',
|
|
389
461
|
title: uuid,
|
|
390
462
|
metadata: {
|
|
@@ -397,14 +469,14 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
397
469
|
}
|
|
398
470
|
var resolved = false;
|
|
399
471
|
try {
|
|
400
|
-
var transaction =
|
|
472
|
+
var transaction = _this6.db.transaction(storeName, 'readonly');
|
|
401
473
|
var store = transaction.objectStore(storeName);
|
|
402
474
|
var request = store.get(key);
|
|
403
475
|
request.onsuccess = function () {
|
|
404
476
|
var _request$result;
|
|
405
477
|
resolved = true;
|
|
406
478
|
if (log) {
|
|
407
|
-
|
|
479
|
+
_this6.app.logger.addLog({
|
|
408
480
|
type: 'info',
|
|
409
481
|
title: uuid,
|
|
410
482
|
metadata: {
|
|
@@ -418,7 +490,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
418
490
|
var _request$error;
|
|
419
491
|
resolved = true;
|
|
420
492
|
if (log) {
|
|
421
|
-
|
|
493
|
+
_this6.app.logger.addLog({
|
|
422
494
|
type: 'error',
|
|
423
495
|
title: uuid,
|
|
424
496
|
metadata: {
|
|
@@ -430,7 +502,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
430
502
|
};
|
|
431
503
|
transaction.onabort = function () {
|
|
432
504
|
if (log) {
|
|
433
|
-
|
|
505
|
+
_this6.app.logger.addLog({
|
|
434
506
|
type: 'info',
|
|
435
507
|
title: uuid,
|
|
436
508
|
metadata: {
|
|
@@ -442,7 +514,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
442
514
|
};
|
|
443
515
|
transaction.onerror = function (event) {
|
|
444
516
|
if (log) {
|
|
445
|
-
|
|
517
|
+
_this6.app.logger.addLog({
|
|
446
518
|
type: 'error',
|
|
447
519
|
title: uuid,
|
|
448
520
|
metadata: {
|
|
@@ -454,7 +526,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
454
526
|
};
|
|
455
527
|
transaction.oncomplete = function () {
|
|
456
528
|
if (log) {
|
|
457
|
-
|
|
529
|
+
_this6.app.logger.addLog({
|
|
458
530
|
type: 'info',
|
|
459
531
|
title: uuid,
|
|
460
532
|
metadata: {
|
|
@@ -467,8 +539,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
467
539
|
} catch (e) {
|
|
468
540
|
reject(e);
|
|
469
541
|
}
|
|
470
|
-
}));
|
|
471
|
-
case
|
|
542
|
+
}), "get(".concat(storeName, ")")));
|
|
543
|
+
case 7:
|
|
472
544
|
case "end":
|
|
473
545
|
return _context4.stop();
|
|
474
546
|
}
|
|
@@ -492,10 +564,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
492
564
|
key: "update",
|
|
493
565
|
value: (function () {
|
|
494
566
|
var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(storeName, data) {
|
|
495
|
-
var
|
|
567
|
+
var _this7 = this;
|
|
496
568
|
var log,
|
|
497
|
-
|
|
569
|
+
storeConfig,
|
|
570
|
+
keyPath,
|
|
498
571
|
key,
|
|
572
|
+
memStore,
|
|
499
573
|
uuid,
|
|
500
574
|
_args5 = arguments;
|
|
501
575
|
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
@@ -503,15 +577,19 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
503
577
|
case 0:
|
|
504
578
|
log = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : false;
|
|
505
579
|
if (this.useIndexDB) {
|
|
506
|
-
_context5.next =
|
|
580
|
+
_context5.next = 8;
|
|
507
581
|
break;
|
|
508
582
|
}
|
|
509
|
-
|
|
583
|
+
// 使用内存存储
|
|
584
|
+
storeConfig = this.stores.find(function (s) {
|
|
510
585
|
return s.name === storeName;
|
|
511
|
-
})
|
|
512
|
-
|
|
586
|
+
});
|
|
587
|
+
keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
|
|
588
|
+
key = data[keyPath];
|
|
589
|
+
memStore = this.getMemoryStore(storeName);
|
|
590
|
+
memStore.set(key, data);
|
|
513
591
|
return _context5.abrupt("return", data);
|
|
514
|
-
case
|
|
592
|
+
case 8:
|
|
515
593
|
uuid = "[ IndexDB ] UPDATE: - ".concat(storeName, " - ").concat(dayjs().valueOf());
|
|
516
594
|
if (log) {
|
|
517
595
|
this.app.logger.addLog({
|
|
@@ -523,10 +601,10 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
523
601
|
}
|
|
524
602
|
});
|
|
525
603
|
}
|
|
526
|
-
return _context5.abrupt("return", new Promise(function (resolve, reject) {
|
|
527
|
-
if (!
|
|
604
|
+
return _context5.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
605
|
+
if (!_this7.db) {
|
|
528
606
|
if (log) {
|
|
529
|
-
|
|
607
|
+
_this7.app.logger.addLog({
|
|
530
608
|
type: 'error',
|
|
531
609
|
title: uuid,
|
|
532
610
|
metadata: {
|
|
@@ -538,12 +616,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
538
616
|
return;
|
|
539
617
|
}
|
|
540
618
|
try {
|
|
541
|
-
var transaction =
|
|
619
|
+
var transaction = _this7.db.transaction(storeName, 'readwrite');
|
|
542
620
|
var store = transaction.objectStore(storeName);
|
|
543
621
|
var request = store.put(data);
|
|
544
622
|
request.onsuccess = function () {
|
|
545
623
|
if (log) {
|
|
546
|
-
|
|
624
|
+
_this7.app.logger.addLog({
|
|
547
625
|
type: 'info',
|
|
548
626
|
title: uuid,
|
|
549
627
|
metadata: {
|
|
@@ -556,7 +634,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
556
634
|
request.onerror = function () {
|
|
557
635
|
var _request$error2;
|
|
558
636
|
if (log) {
|
|
559
|
-
|
|
637
|
+
_this7.app.logger.addLog({
|
|
560
638
|
type: 'error',
|
|
561
639
|
title: uuid,
|
|
562
640
|
metadata: {
|
|
@@ -568,7 +646,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
568
646
|
};
|
|
569
647
|
transaction.oncomplete = function () {
|
|
570
648
|
if (log) {
|
|
571
|
-
|
|
649
|
+
_this7.app.logger.addLog({
|
|
572
650
|
type: 'info',
|
|
573
651
|
title: uuid,
|
|
574
652
|
metadata: {
|
|
@@ -580,7 +658,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
580
658
|
};
|
|
581
659
|
transaction.onabort = function (e) {
|
|
582
660
|
if (log) {
|
|
583
|
-
|
|
661
|
+
_this7.app.logger.addLog({
|
|
584
662
|
type: 'error',
|
|
585
663
|
title: uuid,
|
|
586
664
|
metadata: {
|
|
@@ -593,7 +671,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
593
671
|
transaction.onerror = function (e) {
|
|
594
672
|
var _transaction$error2;
|
|
595
673
|
if (log) {
|
|
596
|
-
|
|
674
|
+
_this7.app.logger.addLog({
|
|
597
675
|
type: 'error',
|
|
598
676
|
title: uuid,
|
|
599
677
|
metadata: {
|
|
@@ -606,8 +684,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
606
684
|
} catch (e) {
|
|
607
685
|
reject(e);
|
|
608
686
|
}
|
|
609
|
-
}));
|
|
610
|
-
case
|
|
687
|
+
}), "update(".concat(storeName, ")")));
|
|
688
|
+
case 11:
|
|
611
689
|
case "end":
|
|
612
690
|
return _context5.stop();
|
|
613
691
|
}
|
|
@@ -629,25 +707,25 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
629
707
|
key: "delete",
|
|
630
708
|
value: (function () {
|
|
631
709
|
var _delete2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(storeName, key) {
|
|
632
|
-
var
|
|
633
|
-
var
|
|
710
|
+
var _this8 = this;
|
|
711
|
+
var memStore;
|
|
634
712
|
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
635
713
|
while (1) switch (_context6.prev = _context6.next) {
|
|
636
714
|
case 0:
|
|
637
715
|
if (this.useIndexDB) {
|
|
638
|
-
_context6.next =
|
|
716
|
+
_context6.next = 3;
|
|
639
717
|
break;
|
|
640
718
|
}
|
|
641
|
-
|
|
642
|
-
this.
|
|
643
|
-
return _context6.abrupt("return",
|
|
644
|
-
case
|
|
645
|
-
return _context6.abrupt("return", new Promise(function (resolve, reject) {
|
|
646
|
-
if (!
|
|
719
|
+
// 使用内存存储
|
|
720
|
+
memStore = this.getMemoryStore(storeName);
|
|
721
|
+
return _context6.abrupt("return", memStore.delete(key));
|
|
722
|
+
case 3:
|
|
723
|
+
return _context6.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
724
|
+
if (!_this8.db) {
|
|
647
725
|
reject(new Error('数据库未连接'));
|
|
648
726
|
return;
|
|
649
727
|
}
|
|
650
|
-
var transaction =
|
|
728
|
+
var transaction = _this8.db.transaction(storeName, 'readwrite');
|
|
651
729
|
var store = transaction.objectStore(storeName);
|
|
652
730
|
var request = store.delete(key);
|
|
653
731
|
request.onsuccess = function () {
|
|
@@ -656,8 +734,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
656
734
|
request.onerror = function () {
|
|
657
735
|
return reject(new Error('删除数据失败'));
|
|
658
736
|
};
|
|
659
|
-
}));
|
|
660
|
-
case
|
|
737
|
+
}), "delete(".concat(storeName, ")")));
|
|
738
|
+
case 4:
|
|
661
739
|
case "end":
|
|
662
740
|
return _context6.stop();
|
|
663
741
|
}
|
|
@@ -681,7 +759,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
681
759
|
key: "getByIndex",
|
|
682
760
|
value: (function () {
|
|
683
761
|
var _getByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(storeName, indexName, indexValue) {
|
|
684
|
-
var
|
|
762
|
+
var _this9 = this;
|
|
685
763
|
var _storeConfig$indexes, allData, storeConfig, index;
|
|
686
764
|
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
687
765
|
while (1) switch (_context7.prev = _context7.next) {
|
|
@@ -710,12 +788,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
710
788
|
return item[index.keyPath] === indexValue;
|
|
711
789
|
}) || null);
|
|
712
790
|
case 9:
|
|
713
|
-
return _context7.abrupt("return", new Promise(function (resolve, reject) {
|
|
714
|
-
if (!
|
|
791
|
+
return _context7.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
792
|
+
if (!_this9.db) {
|
|
715
793
|
reject(new Error('数据库未连接'));
|
|
716
794
|
return;
|
|
717
795
|
}
|
|
718
|
-
var transaction =
|
|
796
|
+
var transaction = _this9.db.transaction(storeName, 'readonly');
|
|
719
797
|
var store = transaction.objectStore(storeName);
|
|
720
798
|
var index = store.index(indexName);
|
|
721
799
|
var request = index.get(indexValue);
|
|
@@ -725,7 +803,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
725
803
|
request.onerror = function () {
|
|
726
804
|
return reject(new Error('通过索引获取数据失败'));
|
|
727
805
|
};
|
|
728
|
-
}));
|
|
806
|
+
}), "getByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
729
807
|
case 10:
|
|
730
808
|
case "end":
|
|
731
809
|
return _context7.stop();
|
|
@@ -749,7 +827,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
749
827
|
key: "existsByIndex",
|
|
750
828
|
value: (function () {
|
|
751
829
|
var _existsByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(storeName, indexName, indexValue) {
|
|
752
|
-
var
|
|
830
|
+
var _this10 = this;
|
|
753
831
|
var _storeConfig$indexes2, allData, storeConfig, index;
|
|
754
832
|
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
755
833
|
while (1) switch (_context8.prev = _context8.next) {
|
|
@@ -778,12 +856,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
778
856
|
return item[index.keyPath] === indexValue;
|
|
779
857
|
}));
|
|
780
858
|
case 9:
|
|
781
|
-
return _context8.abrupt("return", new Promise(function (resolve, reject) {
|
|
782
|
-
if (!
|
|
859
|
+
return _context8.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
860
|
+
if (!_this10.db) {
|
|
783
861
|
reject(new Error('数据库未连接'));
|
|
784
862
|
return;
|
|
785
863
|
}
|
|
786
|
-
var transaction =
|
|
864
|
+
var transaction = _this10.db.transaction(storeName, 'readonly');
|
|
787
865
|
var store = transaction.objectStore(storeName);
|
|
788
866
|
var index = store.index(indexName);
|
|
789
867
|
var request = index.count(indexValue);
|
|
@@ -793,7 +871,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
793
871
|
request.onerror = function () {
|
|
794
872
|
return reject(new Error('通过索引检查数据存在性失败'));
|
|
795
873
|
};
|
|
796
|
-
}));
|
|
874
|
+
}), "existsByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
797
875
|
case 10:
|
|
798
876
|
case "end":
|
|
799
877
|
return _context8.stop();
|
|
@@ -818,7 +896,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
818
896
|
key: "getAllByIndex",
|
|
819
897
|
value: (function () {
|
|
820
898
|
var _getAllByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(storeName, indexName, indexValue) {
|
|
821
|
-
var
|
|
899
|
+
var _this11 = this;
|
|
822
900
|
var _storeConfig$indexes3, allData, storeConfig, index;
|
|
823
901
|
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
824
902
|
while (1) switch (_context9.prev = _context9.next) {
|
|
@@ -847,12 +925,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
847
925
|
return item[index.keyPath] === indexValue;
|
|
848
926
|
}));
|
|
849
927
|
case 9:
|
|
850
|
-
return _context9.abrupt("return", new Promise(function (resolve, reject) {
|
|
851
|
-
if (!
|
|
928
|
+
return _context9.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
929
|
+
if (!_this11.db) {
|
|
852
930
|
reject(new Error('数据库未连接'));
|
|
853
931
|
return;
|
|
854
932
|
}
|
|
855
|
-
var transaction =
|
|
933
|
+
var transaction = _this11.db.transaction(storeName, 'readonly');
|
|
856
934
|
var store = transaction.objectStore(storeName);
|
|
857
935
|
var index = store.index(indexName);
|
|
858
936
|
var request = index.getAll(indexValue);
|
|
@@ -862,7 +940,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
862
940
|
request.onerror = function () {
|
|
863
941
|
return reject(new Error('通过索引获取多条数据失败'));
|
|
864
942
|
};
|
|
865
|
-
}));
|
|
943
|
+
}), "getAllByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
866
944
|
case 10:
|
|
867
945
|
case "end":
|
|
868
946
|
return _context9.stop();
|
|
@@ -884,7 +962,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
884
962
|
key: "count",
|
|
885
963
|
value: (function () {
|
|
886
964
|
var _count = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(storeName) {
|
|
887
|
-
var
|
|
965
|
+
var _this12 = this;
|
|
888
966
|
var allData;
|
|
889
967
|
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
890
968
|
while (1) switch (_context10.prev = _context10.next) {
|
|
@@ -899,12 +977,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
899
977
|
allData = _context10.sent;
|
|
900
978
|
return _context10.abrupt("return", allData.length);
|
|
901
979
|
case 5:
|
|
902
|
-
return _context10.abrupt("return", new Promise(function (resolve, reject) {
|
|
903
|
-
if (!
|
|
980
|
+
return _context10.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
981
|
+
if (!_this12.db) {
|
|
904
982
|
reject(new Error('数据库未连接'));
|
|
905
983
|
return;
|
|
906
984
|
}
|
|
907
|
-
var transaction =
|
|
985
|
+
var transaction = _this12.db.transaction(storeName, 'readonly');
|
|
908
986
|
var store = transaction.objectStore(storeName);
|
|
909
987
|
var request = store.count();
|
|
910
988
|
request.onsuccess = function () {
|
|
@@ -913,7 +991,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
913
991
|
request.onerror = function () {
|
|
914
992
|
return reject(new Error('统计数据数量失败'));
|
|
915
993
|
};
|
|
916
|
-
}));
|
|
994
|
+
}), "count(".concat(storeName, ")")));
|
|
917
995
|
case 6:
|
|
918
996
|
case "end":
|
|
919
997
|
return _context10.stop();
|
|
@@ -937,7 +1015,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
937
1015
|
key: "countByIndex",
|
|
938
1016
|
value: (function () {
|
|
939
1017
|
var _countByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(storeName, indexName, indexValue) {
|
|
940
|
-
var
|
|
1018
|
+
var _this13 = this;
|
|
941
1019
|
var matchingData;
|
|
942
1020
|
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
943
1021
|
while (1) switch (_context11.prev = _context11.next) {
|
|
@@ -952,12 +1030,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
952
1030
|
matchingData = _context11.sent;
|
|
953
1031
|
return _context11.abrupt("return", matchingData.length);
|
|
954
1032
|
case 5:
|
|
955
|
-
return _context11.abrupt("return", new Promise(function (resolve, reject) {
|
|
956
|
-
if (!
|
|
1033
|
+
return _context11.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
1034
|
+
if (!_this13.db) {
|
|
957
1035
|
reject(new Error('数据库未连接'));
|
|
958
1036
|
return;
|
|
959
1037
|
}
|
|
960
|
-
var transaction =
|
|
1038
|
+
var transaction = _this13.db.transaction(storeName, 'readonly');
|
|
961
1039
|
var store = transaction.objectStore(storeName);
|
|
962
1040
|
var index = store.index(indexName);
|
|
963
1041
|
var request = index.count(indexValue);
|
|
@@ -967,7 +1045,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
967
1045
|
request.onerror = function () {
|
|
968
1046
|
return reject(new Error('通过索引统计数据数量失败'));
|
|
969
1047
|
};
|
|
970
|
-
}));
|
|
1048
|
+
}), "countByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
971
1049
|
case 6:
|
|
972
1050
|
case "end":
|
|
973
1051
|
return _context11.stop();
|
|
@@ -990,37 +1068,25 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
990
1068
|
key: "getAll",
|
|
991
1069
|
value: (function () {
|
|
992
1070
|
var _getAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(storeName) {
|
|
993
|
-
var
|
|
994
|
-
var
|
|
1071
|
+
var _this14 = this;
|
|
1072
|
+
var memStore;
|
|
995
1073
|
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
996
1074
|
while (1) switch (_context12.prev = _context12.next) {
|
|
997
1075
|
case 0:
|
|
998
1076
|
if (this.useIndexDB) {
|
|
999
|
-
_context12.next =
|
|
1077
|
+
_context12.next = 3;
|
|
1000
1078
|
break;
|
|
1001
1079
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
if (value) {
|
|
1009
|
-
value = JSON.parse(value);
|
|
1010
|
-
}
|
|
1011
|
-
if (value) {
|
|
1012
|
-
results.push(value);
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
return _context12.abrupt("return", results);
|
|
1017
|
-
case 5:
|
|
1018
|
-
return _context12.abrupt("return", new Promise(function (resolve, reject) {
|
|
1019
|
-
if (!_this12.db) {
|
|
1080
|
+
// 使用内存存储
|
|
1081
|
+
memStore = this.getMemoryStore(storeName);
|
|
1082
|
+
return _context12.abrupt("return", Array.from(memStore.values()));
|
|
1083
|
+
case 3:
|
|
1084
|
+
return _context12.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
1085
|
+
if (!_this14.db) {
|
|
1020
1086
|
reject(new Error('数据库未连接'));
|
|
1021
1087
|
return;
|
|
1022
1088
|
}
|
|
1023
|
-
var transaction =
|
|
1089
|
+
var transaction = _this14.db.transaction(storeName, 'readonly');
|
|
1024
1090
|
var store = transaction.objectStore(storeName);
|
|
1025
1091
|
var request = store.getAll();
|
|
1026
1092
|
request.onsuccess = function () {
|
|
@@ -1029,8 +1095,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1029
1095
|
request.onerror = function () {
|
|
1030
1096
|
return reject(new Error('获取所有数据失败'));
|
|
1031
1097
|
};
|
|
1032
|
-
}));
|
|
1033
|
-
case
|
|
1098
|
+
}), "getAll(".concat(storeName, ")")));
|
|
1099
|
+
case 4:
|
|
1034
1100
|
case "end":
|
|
1035
1101
|
return _context12.stop();
|
|
1036
1102
|
}
|
|
@@ -1051,34 +1117,26 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1051
1117
|
key: "clear",
|
|
1052
1118
|
value: (function () {
|
|
1053
1119
|
var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(storeName) {
|
|
1054
|
-
var
|
|
1055
|
-
var
|
|
1120
|
+
var _this15 = this;
|
|
1121
|
+
var memStore;
|
|
1056
1122
|
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
1057
1123
|
while (1) switch (_context13.prev = _context13.next) {
|
|
1058
1124
|
case 0:
|
|
1059
1125
|
if (this.useIndexDB) {
|
|
1060
|
-
_context13.next =
|
|
1126
|
+
_context13.next = 4;
|
|
1061
1127
|
break;
|
|
1062
1128
|
}
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
key = localStorage.key(i);
|
|
1067
|
-
if (key !== null && key !== void 0 && key.startsWith(prefix)) {
|
|
1068
|
-
keysToRemove.push(key);
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
keysToRemove.forEach(function (key) {
|
|
1072
|
-
return _this13.app.storage.removeStorage(key);
|
|
1073
|
-
});
|
|
1129
|
+
// 使用内存存储
|
|
1130
|
+
memStore = this.getMemoryStore(storeName);
|
|
1131
|
+
memStore.clear();
|
|
1074
1132
|
return _context13.abrupt("return", true);
|
|
1075
|
-
case
|
|
1076
|
-
return _context13.abrupt("return", new Promise(function (resolve, reject) {
|
|
1077
|
-
if (!
|
|
1133
|
+
case 4:
|
|
1134
|
+
return _context13.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
|
|
1135
|
+
if (!_this15.db) {
|
|
1078
1136
|
reject(new Error('数据库未连接'));
|
|
1079
1137
|
return;
|
|
1080
1138
|
}
|
|
1081
|
-
var transaction =
|
|
1139
|
+
var transaction = _this15.db.transaction(storeName, 'readwrite');
|
|
1082
1140
|
var store = transaction.objectStore(storeName);
|
|
1083
1141
|
var request = store.clear();
|
|
1084
1142
|
request.onsuccess = function () {
|
|
@@ -1087,8 +1145,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1087
1145
|
request.onerror = function () {
|
|
1088
1146
|
return reject(new Error('清空数据失败'));
|
|
1089
1147
|
};
|
|
1090
|
-
}));
|
|
1091
|
-
case
|
|
1148
|
+
}), "clear(".concat(storeName, ")")));
|
|
1149
|
+
case 5:
|
|
1092
1150
|
case "end":
|
|
1093
1151
|
return _context13.stop();
|
|
1094
1152
|
}
|
|
@@ -1115,12 +1173,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1115
1173
|
|
|
1116
1174
|
/**
|
|
1117
1175
|
* 获取当前使用的存储方式
|
|
1118
|
-
* @returns {'indexDB'|'
|
|
1176
|
+
* @returns {'indexDB'|'memory'} 当前使用的存储方式
|
|
1119
1177
|
*/
|
|
1120
1178
|
}, {
|
|
1121
1179
|
key: "getCurrentStorage",
|
|
1122
1180
|
value: function getCurrentStorage() {
|
|
1123
|
-
return this.useIndexDB ? 'indexDB' : '
|
|
1181
|
+
return this.useIndexDB ? 'indexDB' : 'memory';
|
|
1124
1182
|
}
|
|
1125
1183
|
}], [{
|
|
1126
1184
|
key: "isSupported",
|
|
@@ -1203,12 +1261,18 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1203
1261
|
* db.close();
|
|
1204
1262
|
*
|
|
1205
1263
|
* // 15. 检查当前使用的存储方式
|
|
1206
|
-
* const storageType = db.getCurrentStorage(); // 'indexDB' 或 '
|
|
1264
|
+
* const storageType = db.getCurrentStorage(); // 'indexDB' 或 'memory'
|
|
1207
1265
|
*
|
|
1208
1266
|
* // 性能优化建议:
|
|
1209
1267
|
* // - 使用 exists() 而不是 get() 来检查数据是否存在
|
|
1210
1268
|
* // - 为常用查询字段创建索引
|
|
1211
1269
|
* // - 使用索引查询方法来提高查询效率
|
|
1212
1270
|
* // - 批量操作时使用事务(可以考虑后续扩展)
|
|
1271
|
+
*
|
|
1272
|
+
* // 降级方案说明:
|
|
1273
|
+
* // - 当浏览器不支持 IndexedDB 时,会自动使用内存存储 (Map) 作为降级方案
|
|
1274
|
+
* // - 内存存储的数据在页面刷新后会丢失,仅适合临时数据存储
|
|
1275
|
+
* // - 内存存储支持所有 IndexedDB API,包括索引查询
|
|
1276
|
+
* // - 对于需要持久化的数据,请确保浏览器支持 IndexedDB
|
|
1213
1277
|
*/
|
|
1214
1278
|
export default IndexDBManager;
|