@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/lib/indexDB/index.js
CHANGED
|
@@ -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,70 +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
|
|
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
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.app.logger.addLog({
|
|
76
|
-
type: "error",
|
|
77
|
-
title: "[ IndexDB ] ERROR",
|
|
78
|
-
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 }
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
request.onsuccess = (event) => {
|
|
82
|
-
this.db = event.target.result;
|
|
83
|
-
resolve(true);
|
|
84
|
-
};
|
|
85
|
-
request.onblocked = () => {
|
|
86
|
-
this.app.logger.addLog({
|
|
87
|
-
type: "error",
|
|
88
|
-
title: "[ IndexDB ] blocked",
|
|
89
|
-
metadata: { msg: "DB blocked" }
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
request.onupgradeneeded = (event) => {
|
|
93
|
-
};
|
|
94
|
-
request.onupgradeneeded = (event) => {
|
|
95
|
-
const db = event.target.result;
|
|
96
|
-
db.onerror = (event2) => {
|
|
119
|
+
return this.withTimeout(
|
|
120
|
+
new Promise((resolve, reject) => {
|
|
121
|
+
const request = indexedDB.open(this.dbName, this.version);
|
|
122
|
+
request.onerror = (event) => {
|
|
97
123
|
var _a, _b;
|
|
98
124
|
this.app.logger.addLog({
|
|
99
125
|
type: "error",
|
|
100
126
|
title: "[ IndexDB ] ERROR",
|
|
101
|
-
metadata: { msg: "DB error", data:
|
|
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" }
|
|
102
139
|
});
|
|
103
140
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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 }
|
|
110
151
|
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
+
);
|
|
115
174
|
}
|
|
116
175
|
/**
|
|
117
|
-
*
|
|
176
|
+
* 获取内存存储中指定 store 的 Map
|
|
118
177
|
* @param {string} storeName - 存储对象名称
|
|
119
|
-
* @
|
|
120
|
-
* @returns {string} 格式化的存储键
|
|
178
|
+
* @returns {Map<string | number, any>} 存储 Map
|
|
121
179
|
* @private
|
|
122
180
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
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;
|
|
125
188
|
}
|
|
126
189
|
/**
|
|
127
190
|
* 添加数据到指定的存储对象
|
|
@@ -132,10 +195,12 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
132
195
|
* @template T
|
|
133
196
|
*/
|
|
134
197
|
async add(storeName, data, log = false) {
|
|
135
|
-
var _a;
|
|
136
198
|
if (!this.useIndexDB) {
|
|
137
|
-
const
|
|
138
|
-
|
|
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);
|
|
139
204
|
return data;
|
|
140
205
|
}
|
|
141
206
|
const uuid = `[ IndexDB ] ADD: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
@@ -146,194 +211,199 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
146
211
|
metadata: { msg: "添加数据前", data }
|
|
147
212
|
});
|
|
148
213
|
}
|
|
149
|
-
return
|
|
150
|
-
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const transaction = this.db.transaction(storeName, "readwrite");
|
|
162
|
-
const store = transaction.objectStore(storeName);
|
|
163
|
-
const request = store.add(data);
|
|
164
|
-
request.onsuccess = () => {
|
|
165
|
-
if (log) {
|
|
166
|
-
this.app.logger.addLog({
|
|
167
|
-
type: "info",
|
|
168
|
-
title: uuid,
|
|
169
|
-
metadata: { msg: "添加数据成功" }
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
return resolve(data);
|
|
173
|
-
};
|
|
174
|
-
request.onerror = () => {
|
|
175
|
-
if (log) {
|
|
176
|
-
this.app.logger.addLog({
|
|
177
|
-
type: "error",
|
|
178
|
-
title: uuid,
|
|
179
|
-
metadata: { msg: "添加数据失败" }
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return reject(new Error("添加数据失败"));
|
|
183
|
-
};
|
|
184
|
-
transaction.oncomplete = () => {
|
|
185
|
-
console.log("✅ 添加事务完成");
|
|
186
|
-
if (log) {
|
|
187
|
-
this.app.logger.addLog({
|
|
188
|
-
type: "info",
|
|
189
|
-
title: uuid,
|
|
190
|
-
metadata: { msg: "事务完成" }
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
return;
|
|
194
|
-
};
|
|
195
|
-
transaction.onerror = (e) => {
|
|
196
|
-
if (log) {
|
|
197
|
-
this.app.logger.addLog({
|
|
198
|
-
type: "error",
|
|
199
|
-
title: uuid,
|
|
200
|
-
metadata: { msg: "事务错误" }
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
return reject(transaction.error ?? new Error("事务错误"));
|
|
204
|
-
};
|
|
205
|
-
transaction.onabort = (e) => {
|
|
206
|
-
if (log) {
|
|
207
|
-
this.app.logger.addLog({
|
|
208
|
-
type: "error",
|
|
209
|
-
title: uuid,
|
|
210
|
-
metadata: { msg: "事务被中止" }
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
return reject(new Error("事务被中止"));
|
|
214
|
-
};
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* 快速检查指定存储对象中的数据是否存在
|
|
219
|
-
* @param {string} storeName - 存储对象名称
|
|
220
|
-
* @param {string|number} key - 数据主键
|
|
221
|
-
* @returns {Promise<boolean>} 数据是否存在
|
|
222
|
-
*/
|
|
223
|
-
async exists(storeName, key) {
|
|
224
|
-
if (!this.useIndexDB) {
|
|
225
|
-
const storageKey = this.getStorageKey(storeName, key);
|
|
226
|
-
return this.app.storage.getStorage(storageKey) !== null;
|
|
227
|
-
}
|
|
228
|
-
return new Promise((resolve, reject) => {
|
|
229
|
-
if (!this.db) {
|
|
230
|
-
reject(new Error("数据库未连接"));
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
const transaction = this.db.transaction(storeName, "readonly");
|
|
234
|
-
const store = transaction.objectStore(storeName);
|
|
235
|
-
const request = store.count(key);
|
|
236
|
-
request.onsuccess = () => resolve(request.result > 0);
|
|
237
|
-
request.onerror = () => reject(new Error("检查数据存在性失败"));
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* 获取指定存储对象中的数据
|
|
242
|
-
* @param {string} storeName - 存储对象名称
|
|
243
|
-
* @param {string|number} key - 数据主键
|
|
244
|
-
* * @param {boolean} [log=true] - 是否记录日志
|
|
245
|
-
* @returns {Promise<T|null>} 获取的数据,不存在则返回 null
|
|
246
|
-
* @template T
|
|
247
|
-
*/
|
|
248
|
-
async get(storeName, key, log = false) {
|
|
249
|
-
const uuid = `[ IndexDB ] GET: - ${storeName} - ${key} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
250
|
-
if (log) {
|
|
251
|
-
this.app.logger.addLog({
|
|
252
|
-
type: "info",
|
|
253
|
-
title: uuid,
|
|
254
|
-
metadata: { msg: "获取数据前" }
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
if (!this.useIndexDB) {
|
|
258
|
-
const storageKey = this.getStorageKey(storeName, key);
|
|
259
|
-
let data = this.app.storage.getStorage(storageKey);
|
|
260
|
-
if (data) {
|
|
261
|
-
data = JSON.parse(data);
|
|
262
|
-
}
|
|
263
|
-
return data;
|
|
264
|
-
}
|
|
265
|
-
return new Promise((resolve, reject) => {
|
|
266
|
-
if (!this.db) {
|
|
267
|
-
if (log) {
|
|
268
|
-
this.app.logger.addLog({
|
|
269
|
-
type: "error",
|
|
270
|
-
title: uuid,
|
|
271
|
-
metadata: { msg: "数据库未连接" }
|
|
272
|
-
});
|
|
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;
|
|
273
226
|
}
|
|
274
|
-
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
let resolved = false;
|
|
278
|
-
try {
|
|
279
|
-
const transaction = this.db.transaction(storeName, "readonly");
|
|
227
|
+
const transaction = this.db.transaction(storeName, "readwrite");
|
|
280
228
|
const store = transaction.objectStore(storeName);
|
|
281
|
-
const request = store.
|
|
229
|
+
const request = store.add(data);
|
|
282
230
|
request.onsuccess = () => {
|
|
283
|
-
resolved = true;
|
|
284
231
|
if (log) {
|
|
285
232
|
this.app.logger.addLog({
|
|
286
233
|
type: "info",
|
|
287
234
|
title: uuid,
|
|
288
|
-
metadata: { msg: "
|
|
235
|
+
metadata: { msg: "添加数据成功" }
|
|
289
236
|
});
|
|
290
237
|
}
|
|
291
|
-
resolve(
|
|
238
|
+
return resolve(data);
|
|
292
239
|
};
|
|
293
240
|
request.onerror = () => {
|
|
294
|
-
resolved = true;
|
|
295
241
|
if (log) {
|
|
296
242
|
this.app.logger.addLog({
|
|
297
243
|
type: "error",
|
|
298
244
|
title: uuid,
|
|
299
|
-
metadata: { msg: "
|
|
245
|
+
metadata: { msg: "添加数据失败" }
|
|
300
246
|
});
|
|
301
247
|
}
|
|
302
|
-
reject(
|
|
248
|
+
return reject(new Error("添加数据失败"));
|
|
303
249
|
};
|
|
304
|
-
transaction.
|
|
250
|
+
transaction.oncomplete = () => {
|
|
251
|
+
console.log("✅ 添加事务完成");
|
|
305
252
|
if (log) {
|
|
306
253
|
this.app.logger.addLog({
|
|
307
254
|
type: "info",
|
|
308
255
|
title: uuid,
|
|
309
|
-
metadata: { msg: "
|
|
256
|
+
metadata: { msg: "事务完成" }
|
|
310
257
|
});
|
|
311
258
|
}
|
|
312
|
-
|
|
259
|
+
return;
|
|
313
260
|
};
|
|
314
|
-
transaction.onerror = (
|
|
261
|
+
transaction.onerror = (e) => {
|
|
315
262
|
if (log) {
|
|
316
263
|
this.app.logger.addLog({
|
|
317
264
|
type: "error",
|
|
318
265
|
title: uuid,
|
|
319
|
-
metadata: { msg:
|
|
266
|
+
metadata: { msg: "事务错误" }
|
|
320
267
|
});
|
|
321
268
|
}
|
|
322
|
-
|
|
269
|
+
return reject(transaction.error ?? new Error("事务错误"));
|
|
323
270
|
};
|
|
324
|
-
transaction.
|
|
271
|
+
transaction.onabort = (e) => {
|
|
325
272
|
if (log) {
|
|
326
273
|
this.app.logger.addLog({
|
|
327
|
-
type: "
|
|
274
|
+
type: "error",
|
|
328
275
|
title: uuid,
|
|
329
|
-
metadata: { msg: "
|
|
276
|
+
metadata: { msg: "事务被中止" }
|
|
330
277
|
});
|
|
331
278
|
}
|
|
279
|
+
return reject(new Error("事务被中止"));
|
|
332
280
|
};
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
+
);
|
|
337
407
|
}
|
|
338
408
|
/**
|
|
339
409
|
* 更新指定存储对象中的数据
|
|
@@ -344,10 +414,12 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
344
414
|
* @template T
|
|
345
415
|
*/
|
|
346
416
|
async update(storeName, data, log = false) {
|
|
347
|
-
var _a;
|
|
348
417
|
if (!this.useIndexDB) {
|
|
349
|
-
const
|
|
350
|
-
|
|
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);
|
|
351
423
|
return data;
|
|
352
424
|
}
|
|
353
425
|
const uuid = `[ IndexDB ] UPDATE: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
@@ -358,76 +430,79 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
358
430
|
metadata: { msg: "更新数据前", data }
|
|
359
431
|
});
|
|
360
432
|
}
|
|
361
|
-
return
|
|
362
|
-
|
|
363
|
-
if (
|
|
364
|
-
this.app.logger.addLog({
|
|
365
|
-
type: "error",
|
|
366
|
-
title: uuid,
|
|
367
|
-
metadata: { msg: "数据库未连接" }
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
reject(new Error("数据库未连接"));
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
try {
|
|
374
|
-
const transaction = this.db.transaction(storeName, "readwrite");
|
|
375
|
-
const store = transaction.objectStore(storeName);
|
|
376
|
-
const request = store.put(data);
|
|
377
|
-
request.onsuccess = () => {
|
|
378
|
-
if (log) {
|
|
379
|
-
this.app.logger.addLog({
|
|
380
|
-
type: "info",
|
|
381
|
-
title: uuid,
|
|
382
|
-
metadata: { msg: "数据更新完成" }
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
return resolve(data);
|
|
386
|
-
};
|
|
387
|
-
request.onerror = () => {
|
|
388
|
-
if (log) {
|
|
389
|
-
this.app.logger.addLog({
|
|
390
|
-
type: "error",
|
|
391
|
-
title: uuid,
|
|
392
|
-
metadata: { msg: "数据更新失败" }
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
return reject(request.error ?? new Error("更新数据失败"));
|
|
396
|
-
};
|
|
397
|
-
transaction.oncomplete = () => {
|
|
398
|
-
if (log) {
|
|
399
|
-
this.app.logger.addLog({
|
|
400
|
-
type: "info",
|
|
401
|
-
title: uuid,
|
|
402
|
-
metadata: { msg: "事务完成" }
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
return console.log("✅ 事务完成");
|
|
406
|
-
};
|
|
407
|
-
transaction.onabort = (e) => {
|
|
433
|
+
return this.withTimeout(
|
|
434
|
+
new Promise((resolve, reject) => {
|
|
435
|
+
if (!this.db) {
|
|
408
436
|
if (log) {
|
|
409
437
|
this.app.logger.addLog({
|
|
410
438
|
type: "error",
|
|
411
439
|
title: uuid,
|
|
412
|
-
metadata: { msg: "
|
|
440
|
+
metadata: { msg: "数据库未连接" }
|
|
413
441
|
});
|
|
414
442
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
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
|
+
);
|
|
431
506
|
}
|
|
432
507
|
/**
|
|
433
508
|
* 删除指定存储对象中的数据
|
|
@@ -437,21 +512,23 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
437
512
|
*/
|
|
438
513
|
async delete(storeName, key) {
|
|
439
514
|
if (!this.useIndexDB) {
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
return true;
|
|
515
|
+
const memStore = this.getMemoryStore(storeName);
|
|
516
|
+
return memStore.delete(key);
|
|
443
517
|
}
|
|
444
|
-
return
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
+
);
|
|
455
532
|
}
|
|
456
533
|
/**
|
|
457
534
|
* 通过索引获取数据
|
|
@@ -470,18 +547,21 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
470
547
|
if (!index) return null;
|
|
471
548
|
return allData.find((item) => item[index.keyPath] === indexValue) || null;
|
|
472
549
|
}
|
|
473
|
-
return
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
+
);
|
|
485
565
|
}
|
|
486
566
|
/**
|
|
487
567
|
* 通过索引检查数据是否存在
|
|
@@ -499,18 +579,21 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
499
579
|
if (!index) return false;
|
|
500
580
|
return allData.some((item) => item[index.keyPath] === indexValue);
|
|
501
581
|
}
|
|
502
|
-
return
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
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
|
+
);
|
|
514
597
|
}
|
|
515
598
|
/**
|
|
516
599
|
* 通过索引获取多条数据
|
|
@@ -529,18 +612,21 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
529
612
|
if (!index) return [];
|
|
530
613
|
return allData.filter((item) => item[index.keyPath] === indexValue);
|
|
531
614
|
}
|
|
532
|
-
return
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
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
|
+
);
|
|
544
630
|
}
|
|
545
631
|
/**
|
|
546
632
|
* 统计指定存储对象中的数据数量
|
|
@@ -552,17 +638,20 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
552
638
|
const allData = await this.getAll(storeName);
|
|
553
639
|
return allData.length;
|
|
554
640
|
}
|
|
555
|
-
return
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
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
|
+
);
|
|
566
655
|
}
|
|
567
656
|
/**
|
|
568
657
|
* 通过索引统计数据数量
|
|
@@ -576,18 +665,21 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
576
665
|
const matchingData = await this.getAllByIndex(storeName, indexName, indexValue);
|
|
577
666
|
return matchingData.length;
|
|
578
667
|
}
|
|
579
|
-
return
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
+
);
|
|
591
683
|
}
|
|
592
684
|
/**
|
|
593
685
|
* 获取指定存储对象中的所有数据
|
|
@@ -597,33 +689,23 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
597
689
|
*/
|
|
598
690
|
async getAll(storeName) {
|
|
599
691
|
if (!this.useIndexDB) {
|
|
600
|
-
const
|
|
601
|
-
|
|
602
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
603
|
-
const key = localStorage.key(i);
|
|
604
|
-
if (key == null ? void 0 : key.startsWith(prefix)) {
|
|
605
|
-
let value = this.app.storage.getStorage(key);
|
|
606
|
-
if (value) {
|
|
607
|
-
value = JSON.parse(value);
|
|
608
|
-
}
|
|
609
|
-
if (value) {
|
|
610
|
-
results.push(value);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
return results;
|
|
692
|
+
const memStore = this.getMemoryStore(storeName);
|
|
693
|
+
return Array.from(memStore.values());
|
|
615
694
|
}
|
|
616
|
-
return
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
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
|
+
);
|
|
627
709
|
}
|
|
628
710
|
/**
|
|
629
711
|
* 清空指定存储对象中的所有数据
|
|
@@ -632,28 +714,24 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
632
714
|
*/
|
|
633
715
|
async clear(storeName) {
|
|
634
716
|
if (!this.useIndexDB) {
|
|
635
|
-
const
|
|
636
|
-
|
|
637
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
638
|
-
const key = localStorage.key(i);
|
|
639
|
-
if (key == null ? void 0 : key.startsWith(prefix)) {
|
|
640
|
-
keysToRemove.push(key);
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
keysToRemove.forEach((key) => this.app.storage.removeStorage(key));
|
|
717
|
+
const memStore = this.getMemoryStore(storeName);
|
|
718
|
+
memStore.clear();
|
|
644
719
|
return true;
|
|
645
720
|
}
|
|
646
|
-
return
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
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
|
+
);
|
|
657
735
|
}
|
|
658
736
|
/**
|
|
659
737
|
* 关闭数据库连接
|
|
@@ -667,10 +745,10 @@ var IndexDBManager = class _IndexDBManager {
|
|
|
667
745
|
}
|
|
668
746
|
/**
|
|
669
747
|
* 获取当前使用的存储方式
|
|
670
|
-
* @returns {'indexDB'|'
|
|
748
|
+
* @returns {'indexDB'|'memory'} 当前使用的存储方式
|
|
671
749
|
*/
|
|
672
750
|
getCurrentStorage() {
|
|
673
|
-
return this.useIndexDB ? "indexDB" : "
|
|
751
|
+
return this.useIndexDB ? "indexDB" : "memory";
|
|
674
752
|
}
|
|
675
753
|
};
|
|
676
754
|
var indexDB_default = IndexDBManager;
|