@pisell/core 1.1.3 → 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.
@@ -48,6 +48,10 @@ var IndexDBManager = class _IndexDBManager {
48
48
  stores;
49
49
  useIndexDB;
50
50
  app;
51
+ // 内存存储:每个 store 使用一个 Map,key 为主键,value 为数据
52
+ memoryStorage = /* @__PURE__ */ new Map();
53
+ // 操作超时时间(毫秒)
54
+ timeout = 5e3;
51
55
  /**
52
56
  * 创建 IndexDBManager 实例
53
57
  * @param {DBOptions} options - 数据库配置选项
@@ -58,49 +62,129 @@ var IndexDBManager = class _IndexDBManager {
58
62
  this.version = options.version;
59
63
  this.stores = options.stores;
60
64
  this.useIndexDB = _IndexDBManager.isSupported();
65
+ this.timeout = options.timeout ?? 5e3;
66
+ if (!this.useIndexDB) {
67
+ this.stores.forEach((store) => {
68
+ this.memoryStorage.set(store.name, /* @__PURE__ */ new Map());
69
+ });
70
+ }
71
+ }
72
+ /**
73
+ * 超时包装器 - 为 Promise 添加超时控制
74
+ * @param {Promise<T>} promise - 需要包装的 Promise
75
+ * @param {string} operation - 操作名称(用于错误提示)
76
+ * @returns {Promise<T>} 带超时控制的 Promise
77
+ * @private
78
+ */
79
+ withTimeout(promise, operation) {
80
+ let timeoutId = null;
81
+ const timeoutPromise = new Promise((_, reject) => {
82
+ timeoutId = setTimeout(() => {
83
+ const error = new Error(`操作超时: ${operation} 在 ${this.timeout}ms 内未完成`);
84
+ this.app.logger.addLog({
85
+ type: "error",
86
+ title: "[ IndexDB ] TIMEOUT",
87
+ metadata: {
88
+ msg: `操作超时: ${operation}`,
89
+ timeout: this.timeout,
90
+ operation
91
+ }
92
+ });
93
+ reject(error);
94
+ }, this.timeout);
95
+ });
96
+ return Promise.race([
97
+ promise.then(
98
+ (result) => {
99
+ if (timeoutId) clearTimeout(timeoutId);
100
+ return result;
101
+ },
102
+ (error) => {
103
+ if (timeoutId) clearTimeout(timeoutId);
104
+ throw error;
105
+ }
106
+ ),
107
+ timeoutPromise
108
+ ]);
61
109
  }
62
110
  /**
63
111
  * 初始化数据库连接
64
- * 如果环境不支持 IndexedDB,将自动使用 localStorage
112
+ * 如果环境不支持 IndexedDB,将自动使用内存存储
65
113
  * @returns {Promise<boolean>} 连接是否成功
66
114
  */
67
115
  async connect() {
68
116
  if (!this.useIndexDB) {
69
117
  return true;
70
118
  }
71
- return new Promise((resolve, reject) => {
72
- const request = indexedDB.open(this.dbName, this.version);
73
- request.onerror = () => {
74
- this.useIndexDB = false;
75
- resolve(true);
76
- };
77
- request.onsuccess = (event) => {
78
- this.db = event.target.result;
79
- resolve(true);
80
- };
81
- request.onupgradeneeded = (event) => {
82
- const db = event.target.result;
83
- this.stores.forEach((store) => {
84
- var _a;
85
- if (!db.objectStoreNames.contains(store.name)) {
86
- const objectStore = db.createObjectStore(store.name, { keyPath: store.keyPath });
87
- (_a = store.indexes) == null ? void 0 : _a.forEach((index) => {
88
- objectStore.createIndex(index.name, index.keyPath, index.options);
119
+ return this.withTimeout(
120
+ new Promise((resolve, reject) => {
121
+ const request = indexedDB.open(this.dbName, this.version);
122
+ request.onerror = (event) => {
123
+ var _a, _b;
124
+ this.app.logger.addLog({
125
+ type: "error",
126
+ title: "[ IndexDB ] ERROR",
127
+ metadata: { msg: "DB request error", data: event, message: (_b = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
128
+ });
129
+ };
130
+ request.onsuccess = (event) => {
131
+ this.db = event.target.result;
132
+ resolve(true);
133
+ };
134
+ request.onblocked = () => {
135
+ this.app.logger.addLog({
136
+ type: "error",
137
+ title: "[ IndexDB ] blocked",
138
+ metadata: { msg: "DB blocked" }
139
+ });
140
+ };
141
+ request.onupgradeneeded = (event) => {
142
+ };
143
+ request.onupgradeneeded = (event) => {
144
+ const db = event.target.result;
145
+ db.onerror = (event2) => {
146
+ var _a, _b;
147
+ this.app.logger.addLog({
148
+ type: "error",
149
+ title: "[ IndexDB ] ERROR",
150
+ metadata: { msg: "DB error", data: event2, message: (_b = (_a = event2 == null ? void 0 : event2.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
89
151
  });
90
- }
91
- });
92
- };
93
- });
152
+ };
153
+ db.onclose = (event2) => {
154
+ var _a, _b;
155
+ this.app.logger.addLog({
156
+ type: "error",
157
+ title: "[ IndexDB ] CLOSE",
158
+ metadata: { msg: "DB close", data: event2, message: (_b = (_a = event2 == null ? void 0 : event2.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
159
+ });
160
+ };
161
+ this.stores.forEach((store) => {
162
+ var _a;
163
+ if (!db.objectStoreNames.contains(store.name)) {
164
+ const objectStore = db.createObjectStore(store.name, { keyPath: store.keyPath });
165
+ (_a = store.indexes) == null ? void 0 : _a.forEach((index) => {
166
+ objectStore.createIndex(index.name, index.keyPath, index.options);
167
+ });
168
+ }
169
+ });
170
+ };
171
+ }),
172
+ "connect"
173
+ );
94
174
  }
95
175
  /**
96
- * 生成用于 localStorage 的存储键
176
+ * 获取内存存储中指定 store 的 Map
97
177
  * @param {string} storeName - 存储对象名称
98
- * @param {string|number} [key] - 可选的键值
99
- * @returns {string} 格式化的存储键
178
+ * @returns {Map<string | number, any>} 存储 Map
100
179
  * @private
101
180
  */
102
- getStorageKey(storeName, key) {
103
- return `${this.dbName}_${storeName}${key ? `_${key}` : ""}`;
181
+ getMemoryStore(storeName) {
182
+ let store = this.memoryStorage.get(storeName);
183
+ if (!store) {
184
+ store = /* @__PURE__ */ new Map();
185
+ this.memoryStorage.set(storeName, store);
186
+ }
187
+ return store;
104
188
  }
105
189
  /**
106
190
  * 添加数据到指定的存储对象
@@ -111,201 +195,215 @@ var IndexDBManager = class _IndexDBManager {
111
195
  * @template T
112
196
  */
113
197
  async add(storeName, data, log = false) {
114
- var _a;
115
198
  if (!this.useIndexDB) {
116
- const key = this.getStorageKey(storeName, data[((_a = this.stores.find((s) => s.name === storeName)) == null ? void 0 : _a.keyPath) || "id"]);
117
- this.app.storage.setStorage(key, JSON.stringify(data));
199
+ const storeConfig = this.stores.find((s) => s.name === storeName);
200
+ const keyPath = (storeConfig == null ? void 0 : storeConfig.keyPath) || "id";
201
+ const key = data[keyPath];
202
+ const memStore = this.getMemoryStore(storeName);
203
+ memStore.set(key, data);
118
204
  return data;
119
205
  }
120
206
  const uuid = `[ IndexDB ] ADD: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
121
- return new Promise((resolve, reject) => {
122
- if (!this.db) {
123
- if (log) {
124
- this.app.logger.addLog({
125
- type: "info",
126
- title: uuid,
127
- metadata: { msg: "添加数据前", data }
128
- });
129
- }
130
- reject(new Error("数据库未连接"));
131
- return;
132
- }
133
- const transaction = this.db.transaction(storeName, "readwrite");
134
- const store = transaction.objectStore(storeName);
135
- const request = store.add(data);
136
- request.onsuccess = () => {
137
- if (log) {
138
- this.app.logger.addLog({
139
- type: "info",
140
- title: uuid,
141
- metadata: { msg: "添加数据成功" }
142
- });
143
- }
144
- return resolve(data);
145
- };
146
- request.onerror = () => {
147
- if (log) {
148
- this.app.logger.addLog({
149
- type: "info",
150
- title: uuid,
151
- metadata: { msg: "添加数据失败" }
152
- });
153
- }
154
- return reject(new Error("添加数据失败"));
155
- };
156
- transaction.oncomplete = () => {
157
- console.log("✅ 添加事务完成");
158
- if (log) {
159
- this.app.logger.addLog({
160
- type: "info",
161
- title: uuid,
162
- metadata: { msg: "事务完成" }
163
- });
164
- }
165
- return;
166
- };
167
- transaction.onerror = (e) => {
168
- if (log) {
169
- this.app.logger.addLog({
170
- type: "info",
171
- title: uuid,
172
- metadata: { msg: "事务错误" }
173
- });
174
- }
175
- return reject(transaction.error ?? new Error("事务错误"));
176
- };
177
- transaction.onabort = (e) => {
178
- if (log) {
179
- this.app.logger.addLog({
180
- type: "info",
181
- title: uuid,
182
- metadata: { msg: "事务被中止" }
183
- });
184
- }
185
- return reject(new Error("事务被中止"));
186
- };
187
- });
188
- }
189
- /**
190
- * 快速检查指定存储对象中的数据是否存在
191
- * @param {string} storeName - 存储对象名称
192
- * @param {string|number} key - 数据主键
193
- * @returns {Promise<boolean>} 数据是否存在
194
- */
195
- async exists(storeName, key) {
196
- if (!this.useIndexDB) {
197
- const storageKey = this.getStorageKey(storeName, key);
198
- return this.app.storage.getStorage(storageKey) !== null;
199
- }
200
- return new Promise((resolve, reject) => {
201
- if (!this.db) {
202
- reject(new Error("数据库未连接"));
203
- return;
204
- }
205
- const transaction = this.db.transaction(storeName, "readonly");
206
- const store = transaction.objectStore(storeName);
207
- const request = store.count(key);
208
- request.onsuccess = () => resolve(request.result > 0);
209
- request.onerror = () => reject(new Error("检查数据存在性失败"));
210
- });
211
- }
212
- /**
213
- * 获取指定存储对象中的数据
214
- * @param {string} storeName - 存储对象名称
215
- * @param {string|number} key - 数据主键
216
- * * @param {boolean} [log=true] - 是否记录日志
217
- * @returns {Promise<T|null>} 获取的数据,不存在则返回 null
218
- * @template T
219
- */
220
- async get(storeName, key, log = false) {
221
- const uuid = `[ IndexDB ] GET: - ${storeName} - ${key} - ${(0, import_dayjs.default)().valueOf()}`;
222
207
  if (log) {
223
208
  this.app.logger.addLog({
224
209
  type: "info",
225
210
  title: uuid,
226
- metadata: { msg: "获取数据前" }
211
+ metadata: { msg: "添加数据前", data }
227
212
  });
228
213
  }
229
- if (!this.useIndexDB) {
230
- const storageKey = this.getStorageKey(storeName, key);
231
- let data = this.app.storage.getStorage(storageKey);
232
- if (data) {
233
- data = JSON.parse(data);
234
- }
235
- return data;
236
- }
237
- return new Promise((resolve, reject) => {
238
- if (!this.db) {
239
- if (log) {
240
- this.app.logger.addLog({
241
- type: "info",
242
- title: uuid,
243
- metadata: { msg: "数据库未连接" }
244
- });
214
+ return this.withTimeout(
215
+ new Promise((resolve, reject) => {
216
+ if (!this.db) {
217
+ if (log) {
218
+ this.app.logger.addLog({
219
+ type: "error",
220
+ title: uuid,
221
+ metadata: { msg: "添加数据前 数据库未连接", data }
222
+ });
223
+ }
224
+ reject(new Error("数据库未连接"));
225
+ return;
245
226
  }
246
- reject(new Error("数据库未连接"));
247
- return;
248
- }
249
- let resolved = false;
250
- try {
251
- const transaction = this.db.transaction(storeName, "readonly");
227
+ const transaction = this.db.transaction(storeName, "readwrite");
252
228
  const store = transaction.objectStore(storeName);
253
- const request = store.get(key);
229
+ const request = store.add(data);
254
230
  request.onsuccess = () => {
255
- resolved = true;
256
231
  if (log) {
257
232
  this.app.logger.addLog({
258
233
  type: "info",
259
234
  title: uuid,
260
- metadata: { msg: "获取成功" }
235
+ metadata: { msg: "添加数据成功" }
261
236
  });
262
237
  }
263
- resolve(request.result ?? null);
238
+ return resolve(data);
264
239
  };
265
240
  request.onerror = () => {
266
- resolved = true;
267
241
  if (log) {
268
242
  this.app.logger.addLog({
269
- type: "info",
243
+ type: "error",
270
244
  title: uuid,
271
- metadata: { msg: "获取失败" }
245
+ metadata: { msg: "添加数据失败" }
272
246
  });
273
247
  }
274
- reject(request.error ?? new Error("获取数据失败"));
248
+ return reject(new Error("添加数据失败"));
275
249
  };
276
- transaction.onabort = () => {
250
+ transaction.oncomplete = () => {
251
+ console.log("✅ 添加事务完成");
277
252
  if (log) {
278
253
  this.app.logger.addLog({
279
254
  type: "info",
280
255
  title: uuid,
281
- metadata: { msg: "事务被中止" }
256
+ metadata: { msg: "事务完成" }
282
257
  });
283
258
  }
284
- if (!resolved) reject(new Error("事务被中止"));
259
+ return;
285
260
  };
286
- transaction.onerror = (event) => {
261
+ transaction.onerror = (e) => {
287
262
  if (log) {
288
263
  this.app.logger.addLog({
289
- type: "info",
264
+ type: "error",
290
265
  title: uuid,
291
- metadata: { msg: `事务错误: ${event.target.error}` }
266
+ metadata: { msg: "事务错误" }
292
267
  });
293
268
  }
294
- if (!resolved) reject(new Error(`事务错误: ${event.target.error}`));
269
+ return reject(transaction.error ?? new Error("事务错误"));
295
270
  };
296
- transaction.oncomplete = () => {
271
+ transaction.onabort = (e) => {
297
272
  if (log) {
298
273
  this.app.logger.addLog({
299
- type: "info",
274
+ type: "error",
300
275
  title: uuid,
301
- metadata: { msg: "事务完成" }
276
+ metadata: { msg: "事务被中止" }
302
277
  });
303
278
  }
279
+ return reject(new Error("事务被中止"));
304
280
  };
305
- } catch (e) {
306
- reject(e);
307
- }
308
- });
281
+ }),
282
+ `add(${storeName})`
283
+ );
284
+ }
285
+ /**
286
+ * 快速检查指定存储对象中的数据是否存在
287
+ * @param {string} storeName - 存储对象名称
288
+ * @param {string|number} key - 数据主键
289
+ * @returns {Promise<boolean>} 数据是否存在
290
+ */
291
+ async exists(storeName, key) {
292
+ if (!this.useIndexDB) {
293
+ const memStore = this.getMemoryStore(storeName);
294
+ return memStore.has(key);
295
+ }
296
+ return this.withTimeout(
297
+ new Promise((resolve, reject) => {
298
+ if (!this.db) {
299
+ reject(new Error("数据库未连接"));
300
+ return;
301
+ }
302
+ const transaction = this.db.transaction(storeName, "readonly");
303
+ const store = transaction.objectStore(storeName);
304
+ const request = store.count(key);
305
+ request.onsuccess = () => resolve(request.result > 0);
306
+ request.onerror = () => reject(new Error("检查数据存在性失败"));
307
+ }),
308
+ `exists(${storeName})`
309
+ );
310
+ }
311
+ /**
312
+ * 获取指定存储对象中的数据
313
+ * @param {string} storeName - 存储对象名称
314
+ * @param {string|number} key - 数据主键
315
+ * * @param {boolean} [log=true] - 是否记录日志
316
+ * @returns {Promise<T|null>} 获取的数据,不存在则返回 null
317
+ * @template T
318
+ */
319
+ async get(storeName, key, log = false) {
320
+ const uuid = `[ IndexDB ] GET: - ${storeName} - ${key} - ${(0, import_dayjs.default)().valueOf()}`;
321
+ if (log) {
322
+ this.app.logger.addLog({
323
+ type: "info",
324
+ title: uuid,
325
+ metadata: { msg: "获取数据前" }
326
+ });
327
+ }
328
+ if (!this.useIndexDB) {
329
+ const memStore = this.getMemoryStore(storeName);
330
+ return memStore.get(key) ?? null;
331
+ }
332
+ return this.withTimeout(
333
+ new Promise((resolve, reject) => {
334
+ if (!this.db) {
335
+ if (log) {
336
+ this.app.logger.addLog({
337
+ type: "error",
338
+ title: uuid,
339
+ metadata: { msg: "数据库未连接" }
340
+ });
341
+ }
342
+ reject(new Error("数据库未连接"));
343
+ return;
344
+ }
345
+ let resolved = false;
346
+ try {
347
+ const transaction = this.db.transaction(storeName, "readonly");
348
+ const store = transaction.objectStore(storeName);
349
+ const request = store.get(key);
350
+ request.onsuccess = () => {
351
+ resolved = true;
352
+ if (log) {
353
+ this.app.logger.addLog({
354
+ type: "info",
355
+ title: uuid,
356
+ metadata: { msg: "获取成功" }
357
+ });
358
+ }
359
+ resolve(request.result ?? null);
360
+ };
361
+ request.onerror = () => {
362
+ resolved = true;
363
+ if (log) {
364
+ this.app.logger.addLog({
365
+ type: "error",
366
+ title: uuid,
367
+ metadata: { msg: "获取失败" }
368
+ });
369
+ }
370
+ reject(request.error ?? new Error("获取数据失败"));
371
+ };
372
+ transaction.onabort = () => {
373
+ if (log) {
374
+ this.app.logger.addLog({
375
+ type: "info",
376
+ title: uuid,
377
+ metadata: { msg: "事务被中止" }
378
+ });
379
+ }
380
+ if (!resolved) reject(new Error("事务被中止"));
381
+ };
382
+ transaction.onerror = (event) => {
383
+ if (log) {
384
+ this.app.logger.addLog({
385
+ type: "error",
386
+ title: uuid,
387
+ metadata: { msg: `事务错误: ${event.target.error}` }
388
+ });
389
+ }
390
+ if (!resolved) reject(new Error(`事务错误: ${event.target.error}`));
391
+ };
392
+ transaction.oncomplete = () => {
393
+ if (log) {
394
+ this.app.logger.addLog({
395
+ type: "info",
396
+ title: uuid,
397
+ metadata: { msg: "事务完成" }
398
+ });
399
+ }
400
+ };
401
+ } catch (e) {
402
+ reject(e);
403
+ }
404
+ }),
405
+ `get(${storeName})`
406
+ );
309
407
  }
310
408
  /**
311
409
  * 更新指定存储对象中的数据
@@ -316,10 +414,12 @@ var IndexDBManager = class _IndexDBManager {
316
414
  * @template T
317
415
  */
318
416
  async update(storeName, data, log = false) {
319
- var _a;
320
417
  if (!this.useIndexDB) {
321
- const key = this.getStorageKey(storeName, data[((_a = this.stores.find((s) => s.name === storeName)) == null ? void 0 : _a.keyPath) || "id"]);
322
- this.app.storage.setStorage(key, JSON.stringify(data));
418
+ const storeConfig = this.stores.find((s) => s.name === storeName);
419
+ const keyPath = (storeConfig == null ? void 0 : storeConfig.keyPath) || "id";
420
+ const key = data[keyPath];
421
+ const memStore = this.getMemoryStore(storeName);
422
+ memStore.set(key, data);
323
423
  return data;
324
424
  }
325
425
  const uuid = `[ IndexDB ] UPDATE: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
@@ -330,76 +430,79 @@ var IndexDBManager = class _IndexDBManager {
330
430
  metadata: { msg: "更新数据前", data }
331
431
  });
332
432
  }
333
- return new Promise((resolve, reject) => {
334
- if (!this.db) {
335
- if (log) {
336
- this.app.logger.addLog({
337
- type: "info",
338
- title: uuid,
339
- metadata: { msg: "数据库未连接" }
340
- });
341
- }
342
- reject(new Error("数据库未连接"));
343
- return;
344
- }
345
- try {
346
- const transaction = this.db.transaction(storeName, "readwrite");
347
- const store = transaction.objectStore(storeName);
348
- const request = store.put(data);
349
- request.onsuccess = () => {
350
- if (log) {
351
- this.app.logger.addLog({
352
- type: "info",
353
- title: uuid,
354
- metadata: { msg: "数据更新完成" }
355
- });
356
- }
357
- return resolve(data);
358
- };
359
- request.onerror = () => {
360
- if (log) {
361
- this.app.logger.addLog({
362
- type: "info",
363
- title: uuid,
364
- metadata: { msg: "数据更新失败" }
365
- });
366
- }
367
- return reject(request.error ?? new Error("更新数据失败"));
368
- };
369
- transaction.oncomplete = () => {
370
- if (log) {
371
- this.app.logger.addLog({
372
- type: "info",
373
- title: uuid,
374
- metadata: { msg: "事务完成" }
375
- });
376
- }
377
- return console.log("✅ 事务完成");
378
- };
379
- transaction.onabort = (e) => {
433
+ return this.withTimeout(
434
+ new Promise((resolve, reject) => {
435
+ if (!this.db) {
380
436
  if (log) {
381
437
  this.app.logger.addLog({
382
- type: "info",
438
+ type: "error",
383
439
  title: uuid,
384
- metadata: { msg: "事务被中止" }
385
- });
386
- }
387
- return reject(new Error("事务被中止"));
388
- };
389
- transaction.onerror = (e) => {
390
- if (log) {
391
- this.app.logger.addLog({
392
- type: "info",
393
- title: uuid,
394
- metadata: { msg: "事务错误" }
440
+ metadata: { msg: "数据库未连接" }
395
441
  });
396
442
  }
397
- return reject(transaction.error ?? new Error("事务错误"));
398
- };
399
- } catch (e) {
400
- reject(e);
401
- }
402
- });
443
+ reject(new Error("数据库未连接"));
444
+ return;
445
+ }
446
+ try {
447
+ const transaction = this.db.transaction(storeName, "readwrite");
448
+ const store = transaction.objectStore(storeName);
449
+ const request = store.put(data);
450
+ request.onsuccess = () => {
451
+ if (log) {
452
+ this.app.logger.addLog({
453
+ type: "info",
454
+ title: uuid,
455
+ metadata: { msg: "数据更新完成" }
456
+ });
457
+ }
458
+ return resolve(data);
459
+ };
460
+ request.onerror = () => {
461
+ if (log) {
462
+ this.app.logger.addLog({
463
+ type: "error",
464
+ title: uuid,
465
+ metadata: { msg: "数据更新失败" }
466
+ });
467
+ }
468
+ return reject(request.error ?? new Error("更新数据失败"));
469
+ };
470
+ transaction.oncomplete = () => {
471
+ if (log) {
472
+ this.app.logger.addLog({
473
+ type: "info",
474
+ title: uuid,
475
+ metadata: { msg: "事务完成" }
476
+ });
477
+ }
478
+ return console.log("✅ 事务完成");
479
+ };
480
+ transaction.onabort = (e) => {
481
+ if (log) {
482
+ this.app.logger.addLog({
483
+ type: "error",
484
+ title: uuid,
485
+ metadata: { msg: "事务被中止" }
486
+ });
487
+ }
488
+ return reject(new Error("事务被中止"));
489
+ };
490
+ transaction.onerror = (e) => {
491
+ if (log) {
492
+ this.app.logger.addLog({
493
+ type: "error",
494
+ title: uuid,
495
+ metadata: { msg: "事务错误" }
496
+ });
497
+ }
498
+ return reject(transaction.error ?? new Error("事务错误"));
499
+ };
500
+ } catch (e) {
501
+ reject(e);
502
+ }
503
+ }),
504
+ `update(${storeName})`
505
+ );
403
506
  }
404
507
  /**
405
508
  * 删除指定存储对象中的数据
@@ -409,21 +512,23 @@ var IndexDBManager = class _IndexDBManager {
409
512
  */
410
513
  async delete(storeName, key) {
411
514
  if (!this.useIndexDB) {
412
- const storageKey = this.getStorageKey(storeName, key);
413
- this.app.storage.removeStorage(storageKey);
414
- return true;
515
+ const memStore = this.getMemoryStore(storeName);
516
+ return memStore.delete(key);
415
517
  }
416
- return new Promise((resolve, reject) => {
417
- if (!this.db) {
418
- reject(new Error("数据库未连接"));
419
- return;
420
- }
421
- const transaction = this.db.transaction(storeName, "readwrite");
422
- const store = transaction.objectStore(storeName);
423
- const request = store.delete(key);
424
- request.onsuccess = () => resolve(true);
425
- request.onerror = () => reject(new Error("删除数据失败"));
426
- });
518
+ return this.withTimeout(
519
+ new Promise((resolve, reject) => {
520
+ if (!this.db) {
521
+ reject(new Error("数据库未连接"));
522
+ return;
523
+ }
524
+ const transaction = this.db.transaction(storeName, "readwrite");
525
+ const store = transaction.objectStore(storeName);
526
+ const request = store.delete(key);
527
+ request.onsuccess = () => resolve(true);
528
+ request.onerror = () => reject(new Error("删除数据失败"));
529
+ }),
530
+ `delete(${storeName})`
531
+ );
427
532
  }
428
533
  /**
429
534
  * 通过索引获取数据
@@ -442,18 +547,21 @@ var IndexDBManager = class _IndexDBManager {
442
547
  if (!index) return null;
443
548
  return allData.find((item) => item[index.keyPath] === indexValue) || null;
444
549
  }
445
- return new Promise((resolve, reject) => {
446
- if (!this.db) {
447
- reject(new Error("数据库未连接"));
448
- return;
449
- }
450
- const transaction = this.db.transaction(storeName, "readonly");
451
- const store = transaction.objectStore(storeName);
452
- const index = store.index(indexName);
453
- const request = index.get(indexValue);
454
- request.onsuccess = () => resolve(request.result || null);
455
- request.onerror = () => reject(new Error("通过索引获取数据失败"));
456
- });
550
+ return this.withTimeout(
551
+ new Promise((resolve, reject) => {
552
+ if (!this.db) {
553
+ reject(new Error("数据库未连接"));
554
+ return;
555
+ }
556
+ const transaction = this.db.transaction(storeName, "readonly");
557
+ const store = transaction.objectStore(storeName);
558
+ const index = store.index(indexName);
559
+ const request = index.get(indexValue);
560
+ request.onsuccess = () => resolve(request.result || null);
561
+ request.onerror = () => reject(new Error("通过索引获取数据失败"));
562
+ }),
563
+ `getByIndex(${storeName}, ${indexName})`
564
+ );
457
565
  }
458
566
  /**
459
567
  * 通过索引检查数据是否存在
@@ -471,18 +579,21 @@ var IndexDBManager = class _IndexDBManager {
471
579
  if (!index) return false;
472
580
  return allData.some((item) => item[index.keyPath] === indexValue);
473
581
  }
474
- return new Promise((resolve, reject) => {
475
- if (!this.db) {
476
- reject(new Error("数据库未连接"));
477
- return;
478
- }
479
- const transaction = this.db.transaction(storeName, "readonly");
480
- const store = transaction.objectStore(storeName);
481
- const index = store.index(indexName);
482
- const request = index.count(indexValue);
483
- request.onsuccess = () => resolve(request.result > 0);
484
- request.onerror = () => reject(new Error("通过索引检查数据存在性失败"));
485
- });
582
+ return this.withTimeout(
583
+ new Promise((resolve, reject) => {
584
+ if (!this.db) {
585
+ reject(new Error("数据库未连接"));
586
+ return;
587
+ }
588
+ const transaction = this.db.transaction(storeName, "readonly");
589
+ const store = transaction.objectStore(storeName);
590
+ const index = store.index(indexName);
591
+ const request = index.count(indexValue);
592
+ request.onsuccess = () => resolve(request.result > 0);
593
+ request.onerror = () => reject(new Error("通过索引检查数据存在性失败"));
594
+ }),
595
+ `existsByIndex(${storeName}, ${indexName})`
596
+ );
486
597
  }
487
598
  /**
488
599
  * 通过索引获取多条数据
@@ -501,18 +612,21 @@ var IndexDBManager = class _IndexDBManager {
501
612
  if (!index) return [];
502
613
  return allData.filter((item) => item[index.keyPath] === indexValue);
503
614
  }
504
- return new Promise((resolve, reject) => {
505
- if (!this.db) {
506
- reject(new Error("数据库未连接"));
507
- return;
508
- }
509
- const transaction = this.db.transaction(storeName, "readonly");
510
- const store = transaction.objectStore(storeName);
511
- const index = store.index(indexName);
512
- const request = index.getAll(indexValue);
513
- request.onsuccess = () => resolve(request.result);
514
- request.onerror = () => reject(new Error("通过索引获取多条数据失败"));
515
- });
615
+ return this.withTimeout(
616
+ new Promise((resolve, reject) => {
617
+ if (!this.db) {
618
+ reject(new Error("数据库未连接"));
619
+ return;
620
+ }
621
+ const transaction = this.db.transaction(storeName, "readonly");
622
+ const store = transaction.objectStore(storeName);
623
+ const index = store.index(indexName);
624
+ const request = index.getAll(indexValue);
625
+ request.onsuccess = () => resolve(request.result);
626
+ request.onerror = () => reject(new Error("通过索引获取多条数据失败"));
627
+ }),
628
+ `getAllByIndex(${storeName}, ${indexName})`
629
+ );
516
630
  }
517
631
  /**
518
632
  * 统计指定存储对象中的数据数量
@@ -524,17 +638,20 @@ var IndexDBManager = class _IndexDBManager {
524
638
  const allData = await this.getAll(storeName);
525
639
  return allData.length;
526
640
  }
527
- return new Promise((resolve, reject) => {
528
- if (!this.db) {
529
- reject(new Error("数据库未连接"));
530
- return;
531
- }
532
- const transaction = this.db.transaction(storeName, "readonly");
533
- const store = transaction.objectStore(storeName);
534
- const request = store.count();
535
- request.onsuccess = () => resolve(request.result);
536
- request.onerror = () => reject(new Error("统计数据数量失败"));
537
- });
641
+ return this.withTimeout(
642
+ new Promise((resolve, reject) => {
643
+ if (!this.db) {
644
+ reject(new Error("数据库未连接"));
645
+ return;
646
+ }
647
+ const transaction = this.db.transaction(storeName, "readonly");
648
+ const store = transaction.objectStore(storeName);
649
+ const request = store.count();
650
+ request.onsuccess = () => resolve(request.result);
651
+ request.onerror = () => reject(new Error("统计数据数量失败"));
652
+ }),
653
+ `count(${storeName})`
654
+ );
538
655
  }
539
656
  /**
540
657
  * 通过索引统计数据数量
@@ -548,18 +665,21 @@ var IndexDBManager = class _IndexDBManager {
548
665
  const matchingData = await this.getAllByIndex(storeName, indexName, indexValue);
549
666
  return matchingData.length;
550
667
  }
551
- return new Promise((resolve, reject) => {
552
- if (!this.db) {
553
- reject(new Error("数据库未连接"));
554
- return;
555
- }
556
- const transaction = this.db.transaction(storeName, "readonly");
557
- const store = transaction.objectStore(storeName);
558
- const index = store.index(indexName);
559
- const request = index.count(indexValue);
560
- request.onsuccess = () => resolve(request.result);
561
- request.onerror = () => reject(new Error("通过索引统计数据数量失败"));
562
- });
668
+ return this.withTimeout(
669
+ new Promise((resolve, reject) => {
670
+ if (!this.db) {
671
+ reject(new Error("数据库未连接"));
672
+ return;
673
+ }
674
+ const transaction = this.db.transaction(storeName, "readonly");
675
+ const store = transaction.objectStore(storeName);
676
+ const index = store.index(indexName);
677
+ const request = index.count(indexValue);
678
+ request.onsuccess = () => resolve(request.result);
679
+ request.onerror = () => reject(new Error("通过索引统计数据数量失败"));
680
+ }),
681
+ `countByIndex(${storeName}, ${indexName})`
682
+ );
563
683
  }
564
684
  /**
565
685
  * 获取指定存储对象中的所有数据
@@ -569,33 +689,23 @@ var IndexDBManager = class _IndexDBManager {
569
689
  */
570
690
  async getAll(storeName) {
571
691
  if (!this.useIndexDB) {
572
- const prefix = this.getStorageKey(storeName);
573
- const results = [];
574
- for (let i = 0; i < localStorage.length; i++) {
575
- const key = localStorage.key(i);
576
- if (key == null ? void 0 : key.startsWith(prefix)) {
577
- let value = this.app.storage.getStorage(key);
578
- if (value) {
579
- value = JSON.parse(value);
580
- }
581
- if (value) {
582
- results.push(value);
583
- }
584
- }
585
- }
586
- return results;
692
+ const memStore = this.getMemoryStore(storeName);
693
+ return Array.from(memStore.values());
587
694
  }
588
- return new Promise((resolve, reject) => {
589
- if (!this.db) {
590
- reject(new Error("数据库未连接"));
591
- return;
592
- }
593
- const transaction = this.db.transaction(storeName, "readonly");
594
- const store = transaction.objectStore(storeName);
595
- const request = store.getAll();
596
- request.onsuccess = () => resolve(request.result);
597
- request.onerror = () => reject(new Error("获取所有数据失败"));
598
- });
695
+ return this.withTimeout(
696
+ new Promise((resolve, reject) => {
697
+ if (!this.db) {
698
+ reject(new Error("数据库未连接"));
699
+ return;
700
+ }
701
+ const transaction = this.db.transaction(storeName, "readonly");
702
+ const store = transaction.objectStore(storeName);
703
+ const request = store.getAll();
704
+ request.onsuccess = () => resolve(request.result);
705
+ request.onerror = () => reject(new Error("获取所有数据失败"));
706
+ }),
707
+ `getAll(${storeName})`
708
+ );
599
709
  }
600
710
  /**
601
711
  * 清空指定存储对象中的所有数据
@@ -604,28 +714,24 @@ var IndexDBManager = class _IndexDBManager {
604
714
  */
605
715
  async clear(storeName) {
606
716
  if (!this.useIndexDB) {
607
- const prefix = this.getStorageKey(storeName);
608
- const keysToRemove = [];
609
- for (let i = 0; i < localStorage.length; i++) {
610
- const key = localStorage.key(i);
611
- if (key == null ? void 0 : key.startsWith(prefix)) {
612
- keysToRemove.push(key);
613
- }
614
- }
615
- keysToRemove.forEach((key) => this.app.storage.removeStorage(key));
717
+ const memStore = this.getMemoryStore(storeName);
718
+ memStore.clear();
616
719
  return true;
617
720
  }
618
- return new Promise((resolve, reject) => {
619
- if (!this.db) {
620
- reject(new Error("数据库未连接"));
621
- return;
622
- }
623
- const transaction = this.db.transaction(storeName, "readwrite");
624
- const store = transaction.objectStore(storeName);
625
- const request = store.clear();
626
- request.onsuccess = () => resolve(true);
627
- request.onerror = () => reject(new Error("清空数据失败"));
628
- });
721
+ return this.withTimeout(
722
+ new Promise((resolve, reject) => {
723
+ if (!this.db) {
724
+ reject(new Error("数据库未连接"));
725
+ return;
726
+ }
727
+ const transaction = this.db.transaction(storeName, "readwrite");
728
+ const store = transaction.objectStore(storeName);
729
+ const request = store.clear();
730
+ request.onsuccess = () => resolve(true);
731
+ request.onerror = () => reject(new Error("清空数据失败"));
732
+ }),
733
+ `clear(${storeName})`
734
+ );
629
735
  }
630
736
  /**
631
737
  * 关闭数据库连接
@@ -639,10 +745,10 @@ var IndexDBManager = class _IndexDBManager {
639
745
  }
640
746
  /**
641
747
  * 获取当前使用的存储方式
642
- * @returns {'indexDB'|'localStorage'} 当前使用的存储方式
748
+ * @returns {'indexDB'|'memory'} 当前使用的存储方式
643
749
  */
644
750
  getCurrentStorage() {
645
- return this.useIndexDB ? "indexDB" : "localStorage";
751
+ return this.useIndexDB ? "indexDB" : "memory";
646
752
  }
647
753
  };
648
754
  var indexDB_default = IndexDBManager;