@pisell/core 0.0.2
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/README.md +1 -0
- package/es/app/app.d.ts +101 -0
- package/es/app/app.js +177 -0
- package/es/app/const.d.ts +4 -0
- package/es/app/const.js +6 -0
- package/es/app/index.d.ts +14 -0
- package/es/app/index.js +52 -0
- package/es/applicationManager/application.d.ts +197 -0
- package/es/applicationManager/application.js +469 -0
- package/es/applicationManager/index.d.ts +19 -0
- package/es/applicationManager/index.js +206 -0
- package/es/aws/index.d.ts +16 -0
- package/es/aws/index.js +76 -0
- package/es/cmd/const.d.ts +11 -0
- package/es/cmd/const.js +13 -0
- package/es/cmd/index.d.ts +13 -0
- package/es/cmd/index.js +91 -0
- package/es/cmd/type.d.ts +5 -0
- package/es/cmd/type.js +1 -0
- package/es/communicationManager/index.d.ts +59 -0
- package/es/communicationManager/index.js +174 -0
- package/es/config.d.ts +3 -0
- package/es/config.js +14 -0
- package/es/cookie/index.d.ts +13 -0
- package/es/cookie/index.js +47 -0
- package/es/css/global.less +73 -0
- package/es/css/index.less +31 -0
- package/es/css/variables.css +84 -0
- package/es/data/index.d.ts +8 -0
- package/es/data/index.js +16 -0
- package/es/history/config.d.ts +24 -0
- package/es/history/config.js +43 -0
- package/es/history/index.d.ts +26 -0
- package/es/history/index.js +80 -0
- package/es/history/type.d.ts +2 -0
- package/es/history/type.js +1 -0
- package/es/hooks/index.d.ts +12 -0
- package/es/hooks/index.js +13 -0
- package/es/hooks/useDelayedValue/index.d.ts +2 -0
- package/es/hooks/useDelayedValue/index.js +26 -0
- package/es/hooks/useDispatch/index.d.ts +2 -0
- package/es/hooks/useDispatch/index.js +2 -0
- package/es/hooks/useLowCode/index.d.ts +13 -0
- package/es/hooks/useLowCode/index.js +74 -0
- package/es/hooks/useStore/index.d.ts +6 -0
- package/es/hooks/useStore/index.js +11 -0
- package/es/index.d.ts +8 -0
- package/es/index.js +10 -0
- package/es/indexDB/index.d.ts +296 -0
- package/es/indexDB/index.js +1619 -0
- package/es/locales/en.d.ts +3 -0
- package/es/locales/en.js +5 -0
- package/es/locales/index.d.ts +39 -0
- package/es/locales/index.js +199 -0
- package/es/locales/original.d.ts +3 -0
- package/es/locales/original.js +7 -0
- package/es/locales/type.d.ts +19 -0
- package/es/locales/type.js +1 -0
- package/es/locales/zh-CN.d.ts +3 -0
- package/es/locales/zh-CN.js +5 -0
- package/es/locales/zh-HK.d.ts +3 -0
- package/es/locales/zh-HK.js +5 -0
- package/es/logger/feishu.d.ts +11 -0
- package/es/logger/feishu.js +50 -0
- package/es/logger/index.d.ts +135 -0
- package/es/logger/index.js +870 -0
- package/es/menuManager/hooks.d.ts +17 -0
- package/es/menuManager/hooks.js +136 -0
- package/es/menuManager/index.d.ts +28 -0
- package/es/menuManager/index.js +165 -0
- package/es/models/global.d.ts +32 -0
- package/es/models/global.js +66 -0
- package/es/models/index.d.ts +45 -0
- package/es/models/index.js +65 -0
- package/es/models/type.d.ts +2 -0
- package/es/models/type.js +1 -0
- package/es/plugin/index.d.ts +0 -0
- package/es/plugin/index.js +0 -0
- package/es/pubsub/example.d.ts +5 -0
- package/es/pubsub/example.js +92 -0
- package/es/pubsub/index.d.ts +73 -0
- package/es/pubsub/index.js +166 -0
- package/es/render/index.d.ts +0 -0
- package/es/render/index.js +0 -0
- package/es/request/cache.d.ts +46 -0
- package/es/request/cache.js +372 -0
- package/es/request/cancelToken.d.ts +38 -0
- package/es/request/cancelToken.js +59 -0
- package/es/request/config.d.ts +3 -0
- package/es/request/config.js +63 -0
- package/es/request/constants.d.ts +2 -0
- package/es/request/constants.js +6 -0
- package/es/request/index.d.ts +24 -0
- package/es/request/index.js +267 -0
- package/es/request/pisell2Request.d.ts +6 -0
- package/es/request/pisell2Request.js +62 -0
- package/es/request/type.d.ts +57 -0
- package/es/request/type.js +10 -0
- package/es/request/utils.d.ts +46 -0
- package/es/request/utils.js +147 -0
- package/es/routes/config.d.ts +7 -0
- package/es/routes/config.js +17 -0
- package/es/routes/index.d.ts +28 -0
- package/es/routes/index.js +151 -0
- package/es/socket/components/SocketMonitorPage.d.ts +6 -0
- package/es/socket/components/SocketMonitorPage.js +485 -0
- package/es/socket/components/index.d.ts +2 -0
- package/es/socket/components/index.js +2 -0
- package/es/socket/constants.d.ts +33 -0
- package/es/socket/constants.js +39 -0
- package/es/socket/events.d.ts +31 -0
- package/es/socket/events.js +19 -0
- package/es/socket/heartbeat.d.ts +66 -0
- package/es/socket/heartbeat.js +184 -0
- package/es/socket/index.d.ts +61 -0
- package/es/socket/index.js +246 -0
- package/es/socket/monitor.d.ts +169 -0
- package/es/socket/monitor.js +448 -0
- package/es/socket/reconnect.d.ts +61 -0
- package/es/socket/reconnect.js +199 -0
- package/es/socket/socket.d.ts +130 -0
- package/es/socket/socket.js +613 -0
- package/es/socket/types.d.ts +85 -0
- package/es/socket/types.js +35 -0
- package/es/storage/index.d.ts +17 -0
- package/es/storage/index.js +46 -0
- package/es/tasks/index.d.ts +127 -0
- package/es/tasks/index.js +1119 -0
- package/es/tasks/scheduledTasksExample.d.ts +61 -0
- package/es/tasks/scheduledTasksExample.js +351 -0
- package/es/tasks/type.d.ts +100 -0
- package/es/tasks/type.js +1 -0
- package/es/tasks/useTasks.d.ts +5 -0
- package/es/tasks/useTasks.js +25 -0
- package/es/type.d.ts +2 -0
- package/es/type.js +1 -0
- package/es/utils/adaptiveThrottle/index.d.ts +36 -0
- package/es/utils/adaptiveThrottle/index.js +136 -0
- package/es/variables/VariablesProvider.d.ts +7 -0
- package/es/variables/VariablesProvider.js +12 -0
- package/es/variables/config.d.ts +3 -0
- package/es/variables/config.js +16 -0
- package/es/variables/index.d.ts +6 -0
- package/es/variables/index.js +5 -0
- package/es/variables/type.d.ts +2 -0
- package/es/variables/type.js +1 -0
- package/es/website/index.d.ts +6 -0
- package/es/website/index.js +65 -0
- package/lib/app/app.d.ts +101 -0
- package/lib/app/app.js +171 -0
- package/lib/app/const.d.ts +4 -0
- package/lib/app/const.js +33 -0
- package/lib/app/index.d.ts +14 -0
- package/lib/app/index.js +76 -0
- package/lib/applicationManager/application.d.ts +197 -0
- package/lib/applicationManager/application.js +280 -0
- package/lib/applicationManager/index.d.ts +19 -0
- package/lib/applicationManager/index.js +88 -0
- package/lib/aws/index.d.ts +16 -0
- package/lib/aws/index.js +67 -0
- package/lib/cmd/const.d.ts +11 -0
- package/lib/cmd/const.js +39 -0
- package/lib/cmd/index.d.ts +13 -0
- package/lib/cmd/index.js +96 -0
- package/lib/cmd/type.d.ts +5 -0
- package/lib/cmd/type.js +17 -0
- package/lib/communicationManager/index.d.ts +59 -0
- package/lib/communicationManager/index.js +102 -0
- package/lib/config.d.ts +3 -0
- package/lib/config.js +36 -0
- package/lib/cookie/index.d.ts +13 -0
- package/lib/cookie/index.js +62 -0
- package/lib/css/global.less +73 -0
- package/lib/css/index.less +31 -0
- package/lib/css/variables.css +84 -0
- package/lib/data/index.d.ts +8 -0
- package/lib/data/index.js +38 -0
- package/lib/history/config.d.ts +24 -0
- package/lib/history/config.js +41 -0
- package/lib/history/index.d.ts +26 -0
- package/lib/history/index.js +99 -0
- package/lib/history/type.d.ts +2 -0
- package/lib/history/type.js +17 -0
- package/lib/hooks/index.d.ts +12 -0
- package/lib/hooks/index.js +44 -0
- package/lib/hooks/useDelayedValue/index.d.ts +2 -0
- package/lib/hooks/useDelayedValue/index.js +36 -0
- package/lib/hooks/useDispatch/index.d.ts +2 -0
- package/lib/hooks/useDispatch/index.js +26 -0
- package/lib/hooks/useLowCode/index.d.ts +13 -0
- package/lib/hooks/useLowCode/index.js +75 -0
- package/lib/hooks/useStore/index.d.ts +6 -0
- package/lib/hooks/useStore/index.js +33 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +58 -0
- package/lib/indexDB/index.d.ts +296 -0
- package/lib/indexDB/index.js +767 -0
- package/lib/locales/en.d.ts +3 -0
- package/lib/locales/en.js +29 -0
- package/lib/locales/index.d.ts +39 -0
- package/lib/locales/index.js +159 -0
- package/lib/locales/original.d.ts +3 -0
- package/lib/locales/original.js +31 -0
- package/lib/locales/type.d.ts +19 -0
- package/lib/locales/type.js +17 -0
- package/lib/locales/zh-CN.d.ts +3 -0
- package/lib/locales/zh-CN.js +29 -0
- package/lib/locales/zh-HK.d.ts +3 -0
- package/lib/locales/zh-HK.js +29 -0
- package/lib/logger/feishu.d.ts +11 -0
- package/lib/logger/feishu.js +52 -0
- package/lib/logger/index.d.ts +135 -0
- package/lib/logger/index.js +476 -0
- package/lib/menuManager/hooks.d.ts +17 -0
- package/lib/menuManager/hooks.js +135 -0
- package/lib/menuManager/index.d.ts +28 -0
- package/lib/menuManager/index.js +125 -0
- package/lib/models/global.d.ts +32 -0
- package/lib/models/global.js +62 -0
- package/lib/models/index.d.ts +45 -0
- package/lib/models/index.js +69 -0
- package/lib/models/type.d.ts +2 -0
- package/lib/models/type.js +17 -0
- package/lib/plugin/index.d.ts +0 -0
- package/lib/plugin/index.js +0 -0
- package/lib/pubsub/example.d.ts +5 -0
- package/lib/pubsub/example.js +61 -0
- package/lib/pubsub/index.d.ts +73 -0
- package/lib/pubsub/index.js +141 -0
- package/lib/render/index.d.ts +0 -0
- package/lib/render/index.js +0 -0
- package/lib/request/cache.d.ts +46 -0
- package/lib/request/cache.js +195 -0
- package/lib/request/cancelToken.d.ts +38 -0
- package/lib/request/cancelToken.js +59 -0
- package/lib/request/config.d.ts +3 -0
- package/lib/request/config.js +69 -0
- package/lib/request/constants.d.ts +2 -0
- package/lib/request/constants.js +34 -0
- package/lib/request/index.d.ts +24 -0
- package/lib/request/index.js +210 -0
- package/lib/request/pisell2Request.d.ts +6 -0
- package/lib/request/pisell2Request.js +75 -0
- package/lib/request/type.d.ts +57 -0
- package/lib/request/type.js +36 -0
- package/lib/request/utils.d.ts +46 -0
- package/lib/request/utils.js +113 -0
- package/lib/routes/config.d.ts +7 -0
- package/lib/routes/config.js +50 -0
- package/lib/routes/index.d.ts +28 -0
- package/lib/routes/index.js +121 -0
- package/lib/socket/components/SocketMonitorPage.d.ts +6 -0
- package/lib/socket/components/SocketMonitorPage.js +340 -0
- package/lib/socket/components/index.d.ts +2 -0
- package/lib/socket/components/index.js +39 -0
- package/lib/socket/constants.d.ts +33 -0
- package/lib/socket/constants.js +62 -0
- package/lib/socket/events.d.ts +31 -0
- package/lib/socket/events.js +44 -0
- package/lib/socket/heartbeat.d.ts +66 -0
- package/lib/socket/heartbeat.js +157 -0
- package/lib/socket/index.d.ts +61 -0
- package/lib/socket/index.js +198 -0
- package/lib/socket/monitor.d.ts +169 -0
- package/lib/socket/monitor.js +350 -0
- package/lib/socket/reconnect.d.ts +61 -0
- package/lib/socket/reconnect.js +157 -0
- package/lib/socket/socket.d.ts +130 -0
- package/lib/socket/socket.js +426 -0
- package/lib/socket/types.d.ts +85 -0
- package/lib/socket/types.js +36 -0
- package/lib/storage/index.d.ts +17 -0
- package/lib/storage/index.js +64 -0
- package/lib/tasks/index.d.ts +127 -0
- package/lib/tasks/index.js +710 -0
- package/lib/tasks/scheduledTasksExample.d.ts +61 -0
- package/lib/tasks/scheduledTasksExample.js +267 -0
- package/lib/tasks/type.d.ts +100 -0
- package/lib/tasks/type.js +17 -0
- package/lib/tasks/useTasks.d.ts +5 -0
- package/lib/tasks/useTasks.js +37 -0
- package/lib/type.d.ts +2 -0
- package/lib/type.js +17 -0
- package/lib/utils/adaptiveThrottle/index.d.ts +36 -0
- package/lib/utils/adaptiveThrottle/index.js +121 -0
- package/lib/variables/VariablesProvider.d.ts +7 -0
- package/lib/variables/VariablesProvider.js +51 -0
- package/lib/variables/config.d.ts +3 -0
- package/lib/variables/config.js +38 -0
- package/lib/variables/index.d.ts +6 -0
- package/lib/variables/index.js +29 -0
- package/lib/variables/type.d.ts +2 -0
- package/lib/variables/type.js +17 -0
- package/lib/website/index.d.ts +6 -0
- package/lib/website/index.js +75 -0
- package/package.json +49 -0
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/indexDB/index.ts
|
|
30
|
+
var indexDB_exports = {};
|
|
31
|
+
__export(indexDB_exports, {
|
|
32
|
+
default: () => indexDB_default,
|
|
33
|
+
mockPromise: () => mockPromise
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(indexDB_exports);
|
|
36
|
+
var import_dayjs = __toESM(require("dayjs"));
|
|
37
|
+
var import_dexie = __toESM(require("dexie"));
|
|
38
|
+
var mockPromise = (data, timeout = 200) => {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
resolve(data);
|
|
42
|
+
}, timeout);
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
var IndexDBManager = class _IndexDBManager {
|
|
46
|
+
/**
|
|
47
|
+
* 检查环境是否支持 IndexedDB
|
|
48
|
+
* @returns {boolean} 是否支持 IndexedDB
|
|
49
|
+
* @private
|
|
50
|
+
*/
|
|
51
|
+
static isSupported() {
|
|
52
|
+
return typeof window !== "undefined" && "indexedDB" in window;
|
|
53
|
+
}
|
|
54
|
+
db = null;
|
|
55
|
+
dbName;
|
|
56
|
+
version;
|
|
57
|
+
stores;
|
|
58
|
+
useIndexDB;
|
|
59
|
+
app;
|
|
60
|
+
// 内存存储:每个 store 使用一个 Map,key 为主键,value 为数据
|
|
61
|
+
memoryStorage = /* @__PURE__ */ new Map();
|
|
62
|
+
// 操作超时时间(毫秒)
|
|
63
|
+
timeout = 1e4;
|
|
64
|
+
// ===== 防抖 add 相关 =====
|
|
65
|
+
// 每个 store 一个 buffer
|
|
66
|
+
debouncedBuffers = /* @__PURE__ */ new Map();
|
|
67
|
+
// 每个 store 一个 timer
|
|
68
|
+
debouncedTimers = /* @__PURE__ */ new Map();
|
|
69
|
+
// 正在 flush 的 store
|
|
70
|
+
debouncedFlushing = /* @__PURE__ */ new Set();
|
|
71
|
+
// 防抖时间(ms)
|
|
72
|
+
debouncedDelay = 200;
|
|
73
|
+
// 单次事务最大写入条数
|
|
74
|
+
debouncedBatchSize = 50;
|
|
75
|
+
// ===== 防抖 update 相关 =====
|
|
76
|
+
// 每个 store 一个 Map(key -> data)
|
|
77
|
+
debouncedUpdateBuffers = /* @__PURE__ */ new Map();
|
|
78
|
+
debouncedUpdateTimers = /* @__PURE__ */ new Map();
|
|
79
|
+
debouncedUpdateFlushing = /* @__PURE__ */ new Set();
|
|
80
|
+
debouncedUpdateDelay = 200;
|
|
81
|
+
debouncedUpdateBatchSize = 50;
|
|
82
|
+
/**
|
|
83
|
+
* 创建 IndexDBManager 实例
|
|
84
|
+
* @param {DBOptions} options - 数据库配置选项
|
|
85
|
+
*/
|
|
86
|
+
constructor(app, options) {
|
|
87
|
+
this.app = app;
|
|
88
|
+
this.dbName = options.dbName;
|
|
89
|
+
this.version = options.version;
|
|
90
|
+
this.stores = options.stores;
|
|
91
|
+
this.useIndexDB = _IndexDBManager.isSupported();
|
|
92
|
+
this.timeout = options.timeout ?? 1e4;
|
|
93
|
+
if (!this.useIndexDB) {
|
|
94
|
+
this.stores.forEach((store) => {
|
|
95
|
+
this.memoryStorage.set(store.name, /* @__PURE__ */ new Map());
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 超时包装器 - 为 Promise 添加超时控制
|
|
101
|
+
* @param {Promise<T>} promise - 需要包装的 Promise
|
|
102
|
+
* @param {string} operation - 操作名称(用于错误提示)
|
|
103
|
+
* @returns {Promise<T>} 带超时控制的 Promise
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
withTimeout(promise, operation) {
|
|
107
|
+
let timeoutId = null;
|
|
108
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
109
|
+
timeoutId = setTimeout(() => {
|
|
110
|
+
const error = new Error(`操作超时: ${operation} 在 ${this.timeout}ms 内未完成`);
|
|
111
|
+
this.app.logger.addLog({
|
|
112
|
+
type: "error",
|
|
113
|
+
title: "[ IndexDB ] TIMEOUT",
|
|
114
|
+
metadata: {
|
|
115
|
+
msg: `操作超时: ${operation}`,
|
|
116
|
+
timeout: this.timeout,
|
|
117
|
+
operation
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
reject(error);
|
|
121
|
+
}, this.timeout);
|
|
122
|
+
});
|
|
123
|
+
return Promise.race([
|
|
124
|
+
promise.then(
|
|
125
|
+
(result) => {
|
|
126
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
127
|
+
return result;
|
|
128
|
+
},
|
|
129
|
+
(error) => {
|
|
130
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
),
|
|
134
|
+
timeoutPromise
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 初始化数据库连接
|
|
139
|
+
* 如果环境不支持 IndexedDB,将自动使用内存存储
|
|
140
|
+
* @returns {Promise<boolean>} 连接是否成功
|
|
141
|
+
*/
|
|
142
|
+
async connect() {
|
|
143
|
+
if (!this.useIndexDB) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return this.withTimeout(
|
|
147
|
+
(async () => {
|
|
148
|
+
try {
|
|
149
|
+
this.db = new import_dexie.default(this.dbName);
|
|
150
|
+
const schema = {};
|
|
151
|
+
this.stores.forEach((store) => {
|
|
152
|
+
var _a;
|
|
153
|
+
const indexes = ((_a = store.indexes) == null ? void 0 : _a.map((index) => {
|
|
154
|
+
var _a2, _b;
|
|
155
|
+
let indexStr = index.name;
|
|
156
|
+
if ((_a2 = index.options) == null ? void 0 : _a2.unique) {
|
|
157
|
+
indexStr = `&${indexStr}`;
|
|
158
|
+
}
|
|
159
|
+
if ((_b = index.options) == null ? void 0 : _b.multiEntry) {
|
|
160
|
+
indexStr = `*${indexStr}`;
|
|
161
|
+
}
|
|
162
|
+
return indexStr;
|
|
163
|
+
})) || [];
|
|
164
|
+
const primaryKey = store.keyPath;
|
|
165
|
+
schema[store.name] = [primaryKey, ...indexes].join(",");
|
|
166
|
+
});
|
|
167
|
+
this.db.version(this.version).stores(schema);
|
|
168
|
+
await this.db.open();
|
|
169
|
+
this.db.on("blocked", () => {
|
|
170
|
+
this.app.logger.addLog({
|
|
171
|
+
type: "error",
|
|
172
|
+
title: "[ IndexDB ] blocked",
|
|
173
|
+
metadata: { msg: "DB blocked" }
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
this.db.on("close", () => {
|
|
177
|
+
this.app.logger.addLog({
|
|
178
|
+
type: "error",
|
|
179
|
+
title: "[ IndexDB ] CLOSE",
|
|
180
|
+
metadata: { msg: "DB close" }
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
return true;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
this.app.logger.addLog({
|
|
186
|
+
type: "error",
|
|
187
|
+
title: "[ IndexDB ] ERROR",
|
|
188
|
+
metadata: { msg: "DB request error", message: error.message }
|
|
189
|
+
});
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
})(),
|
|
193
|
+
"connect"
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 获取内存存储中指定 store 的 Map
|
|
198
|
+
* @param {string} storeName - 存储对象名称
|
|
199
|
+
* @returns {Map<string | number, any>} 存储 Map
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
getMemoryStore(storeName) {
|
|
203
|
+
let store = this.memoryStorage.get(storeName);
|
|
204
|
+
if (!store) {
|
|
205
|
+
store = /* @__PURE__ */ new Map();
|
|
206
|
+
this.memoryStorage.set(storeName, store);
|
|
207
|
+
}
|
|
208
|
+
return store;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 防抖合并 add(用于高频写入场景)
|
|
212
|
+
* @param storeName 存储对象名称
|
|
213
|
+
* @param data 数据
|
|
214
|
+
*/
|
|
215
|
+
debouncedAdd(storeName, data) {
|
|
216
|
+
if (!this.db) return;
|
|
217
|
+
if (!this.debouncedBuffers.has(storeName)) {
|
|
218
|
+
this.debouncedBuffers.set(storeName, []);
|
|
219
|
+
}
|
|
220
|
+
this.debouncedBuffers.get(storeName).push(data);
|
|
221
|
+
if (this.debouncedFlushing.has(storeName)) return;
|
|
222
|
+
const oldTimer = this.debouncedTimers.get(storeName);
|
|
223
|
+
if (oldTimer) clearTimeout(oldTimer);
|
|
224
|
+
this.debouncedTimers.set(
|
|
225
|
+
storeName,
|
|
226
|
+
setTimeout(() => {
|
|
227
|
+
this.flushDebouncedAdd(storeName);
|
|
228
|
+
}, this.debouncedDelay)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 立即 flush 指定 store 的防抖 add
|
|
233
|
+
*/
|
|
234
|
+
async flushDebouncedAdd(storeName) {
|
|
235
|
+
if (!this.useIndexDB || !this.db) return;
|
|
236
|
+
if (this.debouncedFlushing.has(storeName)) return;
|
|
237
|
+
const buffer = this.debouncedBuffers.get(storeName);
|
|
238
|
+
if (!buffer || buffer.length === 0) return;
|
|
239
|
+
this.debouncedFlushing.add(storeName);
|
|
240
|
+
const uuid = `[ IndexDB ] DEBOUNCED_ADD: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
241
|
+
try {
|
|
242
|
+
while (buffer.length > 0) {
|
|
243
|
+
const batch = buffer.splice(0, this.debouncedBatchSize);
|
|
244
|
+
for (const item of batch) {
|
|
245
|
+
await mockPromise({}, this.debouncedDelay);
|
|
246
|
+
try {
|
|
247
|
+
await this.add(storeName, item);
|
|
248
|
+
} catch (err) {
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
this.app.logger.addLog({
|
|
254
|
+
type: "error",
|
|
255
|
+
title: uuid,
|
|
256
|
+
metadata: {
|
|
257
|
+
msg: "防抖合并 add 失败",
|
|
258
|
+
error: error.message
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
} finally {
|
|
262
|
+
this.debouncedFlushing.delete(storeName);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 防抖合并 update(同 key 自动合并)
|
|
267
|
+
*/
|
|
268
|
+
debouncedUpdate(storeName, data) {
|
|
269
|
+
if (!this.useIndexDB) {
|
|
270
|
+
const storeConfig2 = this.stores.find((s) => s.name === storeName);
|
|
271
|
+
const keyPath2 = (storeConfig2 == null ? void 0 : storeConfig2.keyPath) || "id";
|
|
272
|
+
const key2 = data[keyPath2];
|
|
273
|
+
this.getMemoryStore(storeName).set(key2, {
|
|
274
|
+
...this.getMemoryStore(storeName).get(key2),
|
|
275
|
+
...data
|
|
276
|
+
});
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (!this.db) return;
|
|
280
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
281
|
+
const keyPath = (storeConfig == null ? void 0 : storeConfig.keyPath) || "id";
|
|
282
|
+
const key = data[keyPath];
|
|
283
|
+
if (key == null) {
|
|
284
|
+
console.warn(`[IndexDB] debouncedUpdate missing keyPath: ${keyPath}`);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (!this.debouncedUpdateBuffers.has(storeName)) {
|
|
288
|
+
this.debouncedUpdateBuffers.set(storeName, /* @__PURE__ */ new Map());
|
|
289
|
+
}
|
|
290
|
+
const storeBuffer = this.debouncedUpdateBuffers.get(storeName);
|
|
291
|
+
storeBuffer.set(key, {
|
|
292
|
+
...storeBuffer.get(key) || {},
|
|
293
|
+
...data
|
|
294
|
+
});
|
|
295
|
+
if (this.debouncedUpdateFlushing.has(storeName)) return;
|
|
296
|
+
const oldTimer = this.debouncedUpdateTimers.get(storeName);
|
|
297
|
+
if (oldTimer) clearTimeout(oldTimer);
|
|
298
|
+
this.debouncedUpdateTimers.set(
|
|
299
|
+
storeName,
|
|
300
|
+
setTimeout(() => {
|
|
301
|
+
this.flushDebouncedUpdate(storeName);
|
|
302
|
+
}, this.debouncedUpdateDelay)
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* flush 防抖 update
|
|
307
|
+
*/
|
|
308
|
+
async flushDebouncedUpdate(storeName) {
|
|
309
|
+
if (!this.useIndexDB || !this.db) return;
|
|
310
|
+
if (this.debouncedUpdateFlushing.has(storeName)) return;
|
|
311
|
+
const bufferMap = this.debouncedUpdateBuffers.get(storeName);
|
|
312
|
+
if (!bufferMap || bufferMap.size === 0) return;
|
|
313
|
+
this.debouncedUpdateFlushing.add(storeName);
|
|
314
|
+
const uuid = `[ IndexDB ] DEBOUNCED_UPDATE: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
315
|
+
try {
|
|
316
|
+
const entries = Array.from(bufferMap.values());
|
|
317
|
+
bufferMap.clear();
|
|
318
|
+
for (let i = 0; i < entries.length; i += this.debouncedUpdateBatchSize) {
|
|
319
|
+
const batch = entries.slice(i, i + this.debouncedUpdateBatchSize);
|
|
320
|
+
for (const item of batch) {
|
|
321
|
+
await mockPromise(item, this.debouncedUpdateDelay);
|
|
322
|
+
try {
|
|
323
|
+
await this.update(storeName, item);
|
|
324
|
+
} catch (err) {
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} catch (error) {
|
|
329
|
+
this.app.logger.addLog({
|
|
330
|
+
type: "error",
|
|
331
|
+
title: uuid,
|
|
332
|
+
metadata: {
|
|
333
|
+
msg: "防抖合并 update 失败",
|
|
334
|
+
error: error.message
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
} finally {
|
|
338
|
+
this.debouncedUpdateFlushing.delete(storeName);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* 添加数据到指定的存储对象
|
|
343
|
+
* @param {string} storeName - 存储对象名称
|
|
344
|
+
* @param {T} data - 要添加的数据
|
|
345
|
+
* * @param {boolean} [log=true] - 是否记录日志
|
|
346
|
+
* @returns {Promise<T>} 添加的数据
|
|
347
|
+
* @template T
|
|
348
|
+
*/
|
|
349
|
+
async add(storeName, data, log = false) {
|
|
350
|
+
if (!this.useIndexDB) {
|
|
351
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
352
|
+
const keyPath = (storeConfig == null ? void 0 : storeConfig.keyPath) || "id";
|
|
353
|
+
const key = data[keyPath];
|
|
354
|
+
const memStore = this.getMemoryStore(storeName);
|
|
355
|
+
memStore.set(key, data);
|
|
356
|
+
return data;
|
|
357
|
+
}
|
|
358
|
+
const uuid = `[ IndexDB ] ADD: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
359
|
+
if (log) {
|
|
360
|
+
}
|
|
361
|
+
return this.withTimeout(
|
|
362
|
+
(async () => {
|
|
363
|
+
if (!this.db) {
|
|
364
|
+
if (log) {
|
|
365
|
+
this.app.logger.addLog({
|
|
366
|
+
type: "error",
|
|
367
|
+
title: uuid,
|
|
368
|
+
metadata: { msg: "添加数据前 数据库未连接", data }
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
throw new Error("数据库未连接");
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
await this.db.table(storeName).add(data);
|
|
375
|
+
return data;
|
|
376
|
+
} catch (error) {
|
|
377
|
+
const issExists = ((error == null ? void 0 : error.message) || "").includes("exists");
|
|
378
|
+
this.app.logger.addLog({
|
|
379
|
+
type: issExists ? "info" : "error",
|
|
380
|
+
title: uuid,
|
|
381
|
+
metadata: {
|
|
382
|
+
msg: "添加数据失败",
|
|
383
|
+
data,
|
|
384
|
+
error: error.message
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
throw new Error(error == null ? void 0 : error.message);
|
|
388
|
+
}
|
|
389
|
+
})(),
|
|
390
|
+
`add(${storeName})`
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* 快速检查指定存储对象中的数据是否存在
|
|
395
|
+
* @param {string} storeName - 存储对象名称
|
|
396
|
+
* @param {string|number} key - 数据主键
|
|
397
|
+
* @returns {Promise<boolean>} 数据是否存在
|
|
398
|
+
*/
|
|
399
|
+
async exists(storeName, key) {
|
|
400
|
+
if (!this.useIndexDB) {
|
|
401
|
+
const memStore = this.getMemoryStore(storeName);
|
|
402
|
+
return memStore.has(key);
|
|
403
|
+
}
|
|
404
|
+
return this.withTimeout(
|
|
405
|
+
(async () => {
|
|
406
|
+
if (!this.db) {
|
|
407
|
+
throw new Error("数据库未连接");
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
const result = await this.db.table(storeName).get(key);
|
|
411
|
+
return result !== void 0;
|
|
412
|
+
} catch (error) {
|
|
413
|
+
throw new Error("检查数据存在性失败");
|
|
414
|
+
}
|
|
415
|
+
})(),
|
|
416
|
+
`exists(${storeName})`
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* 获取指定存储对象中的数据
|
|
421
|
+
* @param {string} storeName - 存储对象名称
|
|
422
|
+
* @param {string|number} key - 数据主键
|
|
423
|
+
* * @param {boolean} [log=true] - 是否记录日志
|
|
424
|
+
* @returns {Promise<T|null>} 获取的数据,不存在则返回 null
|
|
425
|
+
* @template T
|
|
426
|
+
*/
|
|
427
|
+
async get(storeName, key, log = false) {
|
|
428
|
+
const uuid = `[ IndexDB ] GET: - ${storeName} - ${key} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
429
|
+
if (log) {
|
|
430
|
+
}
|
|
431
|
+
if (!this.useIndexDB) {
|
|
432
|
+
const memStore = this.getMemoryStore(storeName);
|
|
433
|
+
return memStore.get(key) ?? null;
|
|
434
|
+
}
|
|
435
|
+
return this.withTimeout(
|
|
436
|
+
(async () => {
|
|
437
|
+
if (!this.db) {
|
|
438
|
+
if (log) {
|
|
439
|
+
this.app.logger.addLog({
|
|
440
|
+
type: "error",
|
|
441
|
+
title: uuid,
|
|
442
|
+
metadata: { msg: "数据库未连接" }
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
throw new Error("数据库未连接");
|
|
446
|
+
}
|
|
447
|
+
try {
|
|
448
|
+
const result = await this.db.table(storeName).get(key);
|
|
449
|
+
if (log) {
|
|
450
|
+
}
|
|
451
|
+
return result ?? null;
|
|
452
|
+
} catch (error) {
|
|
453
|
+
if (log) {
|
|
454
|
+
this.app.logger.addLog({
|
|
455
|
+
type: "error",
|
|
456
|
+
title: uuid,
|
|
457
|
+
metadata: { msg: "获取失败", error: error.message }
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
throw new Error("获取数据失败");
|
|
461
|
+
}
|
|
462
|
+
})(),
|
|
463
|
+
`get(${storeName})`
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* 更新指定存储对象中的数据
|
|
468
|
+
* @param {string} storeName - 存储对象名称
|
|
469
|
+
* @param {T} data - 要更新的数据
|
|
470
|
+
* * @param {boolean} [log=true] - 是否记录日志
|
|
471
|
+
* @returns {Promise<T>} 更新后的数据
|
|
472
|
+
* @template T
|
|
473
|
+
*/
|
|
474
|
+
async update(storeName, data, log = false) {
|
|
475
|
+
if (!this.useIndexDB) {
|
|
476
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
477
|
+
const keyPath = (storeConfig == null ? void 0 : storeConfig.keyPath) || "id";
|
|
478
|
+
const key = data[keyPath];
|
|
479
|
+
const memStore = this.getMemoryStore(storeName);
|
|
480
|
+
memStore.set(key, data);
|
|
481
|
+
return data;
|
|
482
|
+
}
|
|
483
|
+
const uuid = `[ IndexDB ] UPDATE: - ${storeName} - ${(0, import_dayjs.default)().valueOf()}`;
|
|
484
|
+
if (log) {
|
|
485
|
+
}
|
|
486
|
+
return this.withTimeout(
|
|
487
|
+
(async () => {
|
|
488
|
+
if (!this.db) {
|
|
489
|
+
if (log) {
|
|
490
|
+
this.app.logger.addLog({
|
|
491
|
+
type: "error",
|
|
492
|
+
title: uuid,
|
|
493
|
+
metadata: { msg: "数据库未连接" }
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
throw new Error("数据库未连接");
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
await this.db.table(storeName).put(data);
|
|
500
|
+
if (log) {
|
|
501
|
+
}
|
|
502
|
+
return data;
|
|
503
|
+
} catch (error) {
|
|
504
|
+
this.app.logger.addLog({
|
|
505
|
+
type: "error",
|
|
506
|
+
title: uuid,
|
|
507
|
+
metadata: { msg: "数据更新失败", error: error.message }
|
|
508
|
+
});
|
|
509
|
+
throw new Error(error == null ? void 0 : error.message);
|
|
510
|
+
}
|
|
511
|
+
})(),
|
|
512
|
+
`update(${storeName})`
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* 删除指定存储对象中的数据
|
|
517
|
+
* @param {string} storeName - 存储对象名称
|
|
518
|
+
* @param {string|number} key - 数据主键
|
|
519
|
+
* @returns {Promise<boolean>} 删除是否成功
|
|
520
|
+
*/
|
|
521
|
+
async delete(storeName, key) {
|
|
522
|
+
if (!this.useIndexDB) {
|
|
523
|
+
const memStore = this.getMemoryStore(storeName);
|
|
524
|
+
return memStore.delete(key);
|
|
525
|
+
}
|
|
526
|
+
return this.withTimeout(
|
|
527
|
+
(async () => {
|
|
528
|
+
if (!this.db) {
|
|
529
|
+
throw new Error("数据库未连接");
|
|
530
|
+
}
|
|
531
|
+
try {
|
|
532
|
+
await this.db.table(storeName).delete(key);
|
|
533
|
+
return true;
|
|
534
|
+
} catch (error) {
|
|
535
|
+
throw new Error("删除数据失败");
|
|
536
|
+
}
|
|
537
|
+
})(),
|
|
538
|
+
`delete(${storeName})`
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* 通过索引获取数据
|
|
543
|
+
* @param {string} storeName - 存储对象名称
|
|
544
|
+
* @param {string} indexName - 索引名称
|
|
545
|
+
* @param {string|number} indexValue - 索引值
|
|
546
|
+
* @returns {Promise<T|null>} 获取的数据,不存在则返回 null
|
|
547
|
+
* @template T
|
|
548
|
+
*/
|
|
549
|
+
async getByIndex(storeName, indexName, indexValue) {
|
|
550
|
+
var _a;
|
|
551
|
+
if (!this.useIndexDB) {
|
|
552
|
+
const allData = await this.getAll(storeName);
|
|
553
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
554
|
+
const index = (_a = storeConfig == null ? void 0 : storeConfig.indexes) == null ? void 0 : _a.find((i) => i.name === indexName);
|
|
555
|
+
if (!index) return null;
|
|
556
|
+
return allData.find((item) => item[index.keyPath] === indexValue) || null;
|
|
557
|
+
}
|
|
558
|
+
return this.withTimeout(
|
|
559
|
+
(async () => {
|
|
560
|
+
if (!this.db) {
|
|
561
|
+
throw new Error("数据库未连接");
|
|
562
|
+
}
|
|
563
|
+
try {
|
|
564
|
+
const result = await this.db.table(storeName).where(indexName).equals(indexValue).first();
|
|
565
|
+
return result || null;
|
|
566
|
+
} catch (error) {
|
|
567
|
+
throw new Error("通过索引获取数据失败");
|
|
568
|
+
}
|
|
569
|
+
})(),
|
|
570
|
+
`getByIndex(${storeName}, ${indexName})`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* 通过索引检查数据是否存在
|
|
575
|
+
* @param {string} storeName - 存储对象名称
|
|
576
|
+
* @param {string} indexName - 索引名称
|
|
577
|
+
* @param {string|number} indexValue - 索引值
|
|
578
|
+
* @returns {Promise<boolean>} 数据是否存在
|
|
579
|
+
*/
|
|
580
|
+
async existsByIndex(storeName, indexName, indexValue) {
|
|
581
|
+
var _a;
|
|
582
|
+
if (!this.useIndexDB) {
|
|
583
|
+
const allData = await this.getAll(storeName);
|
|
584
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
585
|
+
const index = (_a = storeConfig == null ? void 0 : storeConfig.indexes) == null ? void 0 : _a.find((i) => i.name === indexName);
|
|
586
|
+
if (!index) return false;
|
|
587
|
+
return allData.some((item) => item[index.keyPath] === indexValue);
|
|
588
|
+
}
|
|
589
|
+
return this.withTimeout(
|
|
590
|
+
(async () => {
|
|
591
|
+
if (!this.db) {
|
|
592
|
+
throw new Error("数据库未连接");
|
|
593
|
+
}
|
|
594
|
+
try {
|
|
595
|
+
const count = await this.db.table(storeName).where(indexName).equals(indexValue).count();
|
|
596
|
+
return count > 0;
|
|
597
|
+
} catch (error) {
|
|
598
|
+
throw new Error("通过索引检查数据存在性失败");
|
|
599
|
+
}
|
|
600
|
+
})(),
|
|
601
|
+
`existsByIndex(${storeName}, ${indexName})`
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* 通过索引获取多条数据
|
|
606
|
+
* @param {string} storeName - 存储对象名称
|
|
607
|
+
* @param {string} indexName - 索引名称
|
|
608
|
+
* @param {string|number} indexValue - 索引值
|
|
609
|
+
* @returns {Promise<T[]>} 数据数组
|
|
610
|
+
* @template T
|
|
611
|
+
*/
|
|
612
|
+
async getAllByIndex(storeName, indexName, indexValue) {
|
|
613
|
+
var _a;
|
|
614
|
+
if (!this.useIndexDB) {
|
|
615
|
+
const allData = await this.getAll(storeName);
|
|
616
|
+
const storeConfig = this.stores.find((s) => s.name === storeName);
|
|
617
|
+
const index = (_a = storeConfig == null ? void 0 : storeConfig.indexes) == null ? void 0 : _a.find((i) => i.name === indexName);
|
|
618
|
+
if (!index) return [];
|
|
619
|
+
return allData.filter((item) => item[index.keyPath] === indexValue);
|
|
620
|
+
}
|
|
621
|
+
return this.withTimeout(
|
|
622
|
+
(async () => {
|
|
623
|
+
if (!this.db) {
|
|
624
|
+
throw new Error("数据库未连接");
|
|
625
|
+
}
|
|
626
|
+
try {
|
|
627
|
+
const results = await this.db.table(storeName).where(indexName).equals(indexValue).toArray();
|
|
628
|
+
return results;
|
|
629
|
+
} catch (error) {
|
|
630
|
+
throw new Error("通过索引获取多条数据失败");
|
|
631
|
+
}
|
|
632
|
+
})(),
|
|
633
|
+
`getAllByIndex(${storeName}, ${indexName})`
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* 统计指定存储对象中的数据数量
|
|
638
|
+
* @param {string} storeName - 存储对象名称
|
|
639
|
+
* @returns {Promise<number>} 数据数量
|
|
640
|
+
*/
|
|
641
|
+
async count(storeName) {
|
|
642
|
+
if (!this.useIndexDB) {
|
|
643
|
+
const allData = await this.getAll(storeName);
|
|
644
|
+
return allData.length;
|
|
645
|
+
}
|
|
646
|
+
return this.withTimeout(
|
|
647
|
+
(async () => {
|
|
648
|
+
if (!this.db) {
|
|
649
|
+
throw new Error("数据库未连接");
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
const count = await this.db.table(storeName).count();
|
|
653
|
+
return count;
|
|
654
|
+
} catch (error) {
|
|
655
|
+
throw new Error("统计数据数量失败");
|
|
656
|
+
}
|
|
657
|
+
})(),
|
|
658
|
+
`count(${storeName})`
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* 通过索引统计数据数量
|
|
663
|
+
* @param {string} storeName - 存储对象名称
|
|
664
|
+
* @param {string} indexName - 索引名称
|
|
665
|
+
* @param {string|number} indexValue - 索引值
|
|
666
|
+
* @returns {Promise<number>} 数据数量
|
|
667
|
+
*/
|
|
668
|
+
async countByIndex(storeName, indexName, indexValue) {
|
|
669
|
+
if (!this.useIndexDB) {
|
|
670
|
+
const matchingData = await this.getAllByIndex(storeName, indexName, indexValue);
|
|
671
|
+
return matchingData.length;
|
|
672
|
+
}
|
|
673
|
+
return this.withTimeout(
|
|
674
|
+
(async () => {
|
|
675
|
+
if (!this.db) {
|
|
676
|
+
throw new Error("数据库未连接");
|
|
677
|
+
}
|
|
678
|
+
try {
|
|
679
|
+
const count = await this.db.table(storeName).where(indexName).equals(indexValue).count();
|
|
680
|
+
return count;
|
|
681
|
+
} catch (error) {
|
|
682
|
+
throw new Error("通过索引统计数据数量失败");
|
|
683
|
+
}
|
|
684
|
+
})(),
|
|
685
|
+
`countByIndex(${storeName}, ${indexName})`
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* 获取指定存储对象中的所有数据
|
|
690
|
+
* @param {string} storeName - 存储对象名称
|
|
691
|
+
* @returns {Promise<T[]>} 数据数组
|
|
692
|
+
* @template T
|
|
693
|
+
*/
|
|
694
|
+
async getAll(storeName) {
|
|
695
|
+
if (!this.useIndexDB) {
|
|
696
|
+
const memStore = this.getMemoryStore(storeName);
|
|
697
|
+
return Array.from(memStore.values());
|
|
698
|
+
}
|
|
699
|
+
return this.withTimeout(
|
|
700
|
+
(async () => {
|
|
701
|
+
if (!this.db) {
|
|
702
|
+
throw new Error("数据库未连接");
|
|
703
|
+
}
|
|
704
|
+
try {
|
|
705
|
+
const results = await this.db.table(storeName).toArray();
|
|
706
|
+
return results;
|
|
707
|
+
} catch (error) {
|
|
708
|
+
this.app.logger.addLog({
|
|
709
|
+
type: "error",
|
|
710
|
+
title: `[ IndexDB ] ERROR: - getAll(${storeName})`,
|
|
711
|
+
metadata: { msg: "获取所有数据失败", error: error.message }
|
|
712
|
+
});
|
|
713
|
+
throw new Error(error == null ? void 0 : error.message);
|
|
714
|
+
}
|
|
715
|
+
})(),
|
|
716
|
+
`getAll(${storeName})`
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* 清空指定存储对象中的所有数据
|
|
721
|
+
* @param {string} storeName - 存储对象名称
|
|
722
|
+
* @returns {Promise<boolean>} 清空是否成功
|
|
723
|
+
*/
|
|
724
|
+
async clear(storeName) {
|
|
725
|
+
if (!this.useIndexDB) {
|
|
726
|
+
const memStore = this.getMemoryStore(storeName);
|
|
727
|
+
memStore.clear();
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
return this.withTimeout(
|
|
731
|
+
(async () => {
|
|
732
|
+
if (!this.db) {
|
|
733
|
+
throw new Error("数据库未连接");
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
await this.db.table(storeName).clear();
|
|
737
|
+
return true;
|
|
738
|
+
} catch (error) {
|
|
739
|
+
throw new Error("清空数据失败");
|
|
740
|
+
}
|
|
741
|
+
})(),
|
|
742
|
+
`clear(${storeName})`
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* 关闭数据库连接
|
|
747
|
+
* 在不再需要使用数据库时调用,释放资源
|
|
748
|
+
*/
|
|
749
|
+
close() {
|
|
750
|
+
if (this.db) {
|
|
751
|
+
this.db.close();
|
|
752
|
+
this.db = null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* 获取当前使用的存储方式
|
|
757
|
+
* @returns {'indexDB'|'memory'} 当前使用的存储方式
|
|
758
|
+
*/
|
|
759
|
+
getCurrentStorage() {
|
|
760
|
+
return this.useIndexDB ? "indexDB" : "memory";
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
var indexDB_default = IndexDBManager;
|
|
764
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
765
|
+
0 && (module.exports = {
|
|
766
|
+
mockPromise
|
|
767
|
+
});
|