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