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