@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/es/indexDB/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
2
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
4
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
8
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
3
9
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
10
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
11
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
@@ -9,11 +15,13 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
|
|
|
9
15
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
10
16
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
11
17
|
import dayjs from 'dayjs';
|
|
18
|
+
import Dexie from 'dexie';
|
|
19
|
+
|
|
12
20
|
/**
|
|
13
21
|
* IndexDB 管理器模块
|
|
14
22
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
23
|
+
* 这个模块使用 Dexie.js 提供了对浏览器 IndexedDB 的简单封装,并在不支持 IndexedDB 的环境中
|
|
24
|
+
* 自动降级使用内存存储作为备选存储方案。
|
|
17
25
|
*/
|
|
18
26
|
|
|
19
27
|
/**
|
|
@@ -31,7 +39,7 @@ import dayjs from 'dayjs';
|
|
|
31
39
|
*/
|
|
32
40
|
/**
|
|
33
41
|
* IndexDB 管理器类
|
|
34
|
-
* 提供对 IndexedDB
|
|
42
|
+
* 使用 Dexie.js 提供对 IndexedDB 的封装,支持自动降级到内存存储
|
|
35
43
|
* @class IndexDBManager
|
|
36
44
|
*/
|
|
37
45
|
var IndexDBManager = /*#__PURE__*/function () {
|
|
@@ -40,6 +48,8 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
40
48
|
* @param {DBOptions} options - 数据库配置选项
|
|
41
49
|
*/
|
|
42
50
|
function IndexDBManager(app, options) {
|
|
51
|
+
var _options$timeout,
|
|
52
|
+
_this = this;
|
|
43
53
|
_classCallCheck(this, IndexDBManager);
|
|
44
54
|
_defineProperty(this, "db", null);
|
|
45
55
|
_defineProperty(this, "dbName", void 0);
|
|
@@ -47,92 +57,164 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
47
57
|
_defineProperty(this, "stores", void 0);
|
|
48
58
|
_defineProperty(this, "useIndexDB", void 0);
|
|
49
59
|
_defineProperty(this, "app", void 0);
|
|
60
|
+
// 内存存储:每个 store 使用一个 Map,key 为主键,value 为数据
|
|
61
|
+
_defineProperty(this, "memoryStorage", new Map());
|
|
62
|
+
// 操作超时时间(毫秒)
|
|
63
|
+
_defineProperty(this, "timeout", 5000);
|
|
50
64
|
this.app = app;
|
|
51
65
|
this.dbName = options.dbName;
|
|
52
66
|
this.version = options.version;
|
|
53
67
|
this.stores = options.stores;
|
|
54
68
|
this.useIndexDB = IndexDBManager.isSupported();
|
|
69
|
+
this.timeout = (_options$timeout = options.timeout) !== null && _options$timeout !== void 0 ? _options$timeout : 5000; // 默认 5 秒超时
|
|
70
|
+
|
|
71
|
+
// 初始化内存存储的各个 store
|
|
72
|
+
if (!this.useIndexDB) {
|
|
73
|
+
this.stores.forEach(function (store) {
|
|
74
|
+
_this.memoryStorage.set(store.name, new Map());
|
|
75
|
+
});
|
|
76
|
+
}
|
|
55
77
|
}
|
|
56
78
|
|
|
57
79
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
80
|
+
* 超时包装器 - 为 Promise 添加超时控制
|
|
81
|
+
* @param {Promise<T>} promise - 需要包装的 Promise
|
|
82
|
+
* @param {string} operation - 操作名称(用于错误提示)
|
|
83
|
+
* @returns {Promise<T>} 带超时控制的 Promise
|
|
84
|
+
* @private
|
|
61
85
|
*/
|
|
62
86
|
_createClass(IndexDBManager, [{
|
|
87
|
+
key: "withTimeout",
|
|
88
|
+
value: function withTimeout(promise, operation) {
|
|
89
|
+
var _this2 = this;
|
|
90
|
+
var timeoutId = null;
|
|
91
|
+
var timeoutPromise = new Promise(function (_, reject) {
|
|
92
|
+
timeoutId = setTimeout(function () {
|
|
93
|
+
var error = new Error("\u64CD\u4F5C\u8D85\u65F6: ".concat(operation, " \u5728 ").concat(_this2.timeout, "ms \u5185\u672A\u5B8C\u6210"));
|
|
94
|
+
_this2.app.logger.addLog({
|
|
95
|
+
type: 'error',
|
|
96
|
+
title: '[ IndexDB ] TIMEOUT',
|
|
97
|
+
metadata: {
|
|
98
|
+
msg: "\u64CD\u4F5C\u8D85\u65F6: ".concat(operation),
|
|
99
|
+
timeout: _this2.timeout,
|
|
100
|
+
operation: operation
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
reject(error);
|
|
104
|
+
}, _this2.timeout);
|
|
105
|
+
});
|
|
106
|
+
return Promise.race([promise.then(function (result) {
|
|
107
|
+
// Promise 成功完成,清除超时定时器
|
|
108
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
109
|
+
return result;
|
|
110
|
+
}, function (error) {
|
|
111
|
+
// Promise 失败,清除超时定时器
|
|
112
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
113
|
+
throw error;
|
|
114
|
+
}), timeoutPromise]);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 初始化数据库连接
|
|
119
|
+
* 如果环境不支持 IndexedDB,将自动使用内存存储
|
|
120
|
+
* @returns {Promise<boolean>} 连接是否成功
|
|
121
|
+
*/
|
|
122
|
+
}, {
|
|
63
123
|
key: "connect",
|
|
64
124
|
value: (function () {
|
|
65
|
-
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
66
|
-
var
|
|
67
|
-
return _regeneratorRuntime().wrap(function
|
|
68
|
-
while (1) switch (
|
|
125
|
+
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
126
|
+
var _this3 = this;
|
|
127
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
128
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
69
129
|
case 0:
|
|
70
130
|
if (this.useIndexDB) {
|
|
71
|
-
|
|
131
|
+
_context2.next = 2;
|
|
72
132
|
break;
|
|
73
133
|
}
|
|
74
|
-
return
|
|
134
|
+
return _context2.abrupt("return", true);
|
|
75
135
|
case 2:
|
|
76
|
-
return
|
|
77
|
-
var
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
db.onerror = function (event) {
|
|
107
|
-
var _event$target2;
|
|
108
|
-
_this.app.logger.addLog({
|
|
109
|
-
type: 'error',
|
|
110
|
-
title: '[ IndexDB ] ERROR',
|
|
111
|
-
metadata: {
|
|
112
|
-
msg: 'DB error',
|
|
113
|
-
data: event,
|
|
114
|
-
message: event === null || event === void 0 || (_event$target2 = event.target) === null || _event$target2 === void 0 || (_event$target2 = _event$target2.error) === null || _event$target2 === void 0 ? void 0 : _event$target2.message
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
_this.stores.forEach(function (store) {
|
|
119
|
-
if (!db.objectStoreNames.contains(store.name)) {
|
|
120
|
-
var _store$indexes;
|
|
121
|
-
var objectStore = db.createObjectStore(store.name, {
|
|
122
|
-
keyPath: store.keyPath
|
|
136
|
+
return _context2.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
137
|
+
var schema;
|
|
138
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
139
|
+
while (1) switch (_context.prev = _context.next) {
|
|
140
|
+
case 0:
|
|
141
|
+
_context.prev = 0;
|
|
142
|
+
// 创建 Dexie 实例
|
|
143
|
+
_this3.db = new Dexie(_this3.dbName);
|
|
144
|
+
|
|
145
|
+
// 构建 schema
|
|
146
|
+
schema = {};
|
|
147
|
+
_this3.stores.forEach(function (store) {
|
|
148
|
+
var _store$indexes;
|
|
149
|
+
// Dexie schema 格式: "primaryKey, index1, index2, ..."
|
|
150
|
+
var indexes = ((_store$indexes = store.indexes) === null || _store$indexes === void 0 ? void 0 : _store$indexes.map(function (index) {
|
|
151
|
+
var _index$options, _index$options2;
|
|
152
|
+
var indexStr = index.name;
|
|
153
|
+
// Dexie 使用 & 表示复合索引,* 表示多值索引,+ 表示自增主键
|
|
154
|
+
if ((_index$options = index.options) !== null && _index$options !== void 0 && _index$options.unique) {
|
|
155
|
+
indexStr = "&".concat(indexStr);
|
|
156
|
+
}
|
|
157
|
+
if ((_index$options2 = index.options) !== null && _index$options2 !== void 0 && _index$options2.multiEntry) {
|
|
158
|
+
indexStr = "*".concat(indexStr);
|
|
159
|
+
}
|
|
160
|
+
return indexStr;
|
|
161
|
+
})) || [];
|
|
162
|
+
|
|
163
|
+
// 主键需要放在最前面,使用 ++ 表示自增,& 表示唯一
|
|
164
|
+
var primaryKey = store.keyPath;
|
|
165
|
+
schema[store.name] = [primaryKey].concat(_toConsumableArray(indexes)).join(',');
|
|
123
166
|
});
|
|
124
|
-
|
|
125
|
-
|
|
167
|
+
|
|
168
|
+
// 定义数据库版本和 schema
|
|
169
|
+
_this3.db.version(_this3.version).stores(schema);
|
|
170
|
+
|
|
171
|
+
// 打开数据库
|
|
172
|
+
_context.next = 7;
|
|
173
|
+
return _this3.db.open();
|
|
174
|
+
case 7:
|
|
175
|
+
// 监听错误和关闭事件
|
|
176
|
+
_this3.db.on('blocked', function () {
|
|
177
|
+
_this3.app.logger.addLog({
|
|
178
|
+
type: 'error',
|
|
179
|
+
title: '[ IndexDB ] blocked',
|
|
180
|
+
metadata: {
|
|
181
|
+
msg: 'DB blocked'
|
|
182
|
+
}
|
|
183
|
+
});
|
|
126
184
|
});
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
185
|
+
_this3.db.on('close', function () {
|
|
186
|
+
_this3.app.logger.addLog({
|
|
187
|
+
type: 'error',
|
|
188
|
+
title: '[ IndexDB ] CLOSE',
|
|
189
|
+
metadata: {
|
|
190
|
+
msg: 'DB close'
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
return _context.abrupt("return", true);
|
|
195
|
+
case 12:
|
|
196
|
+
_context.prev = 12;
|
|
197
|
+
_context.t0 = _context["catch"](0);
|
|
198
|
+
_this3.app.logger.addLog({
|
|
199
|
+
type: 'error',
|
|
200
|
+
title: '[ IndexDB ] ERROR',
|
|
201
|
+
metadata: {
|
|
202
|
+
msg: 'DB request error',
|
|
203
|
+
message: _context.t0.message
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
throw _context.t0;
|
|
207
|
+
case 16:
|
|
208
|
+
case "end":
|
|
209
|
+
return _context.stop();
|
|
210
|
+
}
|
|
211
|
+
}, _callee, null, [[0, 12]]);
|
|
212
|
+
}))(), 'connect'));
|
|
131
213
|
case 3:
|
|
132
214
|
case "end":
|
|
133
|
-
return
|
|
215
|
+
return _context2.stop();
|
|
134
216
|
}
|
|
135
|
-
},
|
|
217
|
+
}, _callee2, this);
|
|
136
218
|
}));
|
|
137
219
|
function connect() {
|
|
138
220
|
return _connect.apply(this, arguments);
|
|
@@ -140,17 +222,21 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
140
222
|
return connect;
|
|
141
223
|
}()
|
|
142
224
|
/**
|
|
143
|
-
*
|
|
225
|
+
* 获取内存存储中指定 store 的 Map
|
|
144
226
|
* @param {string} storeName - 存储对象名称
|
|
145
|
-
* @
|
|
146
|
-
* @returns {string} 格式化的存储键
|
|
227
|
+
* @returns {Map<string | number, any>} 存储 Map
|
|
147
228
|
* @private
|
|
148
229
|
*/
|
|
149
230
|
)
|
|
150
231
|
}, {
|
|
151
|
-
key: "
|
|
152
|
-
value: function
|
|
153
|
-
|
|
232
|
+
key: "getMemoryStore",
|
|
233
|
+
value: function getMemoryStore(storeName) {
|
|
234
|
+
var store = this.memoryStorage.get(storeName);
|
|
235
|
+
if (!store) {
|
|
236
|
+
store = new Map();
|
|
237
|
+
this.memoryStorage.set(storeName, store);
|
|
238
|
+
}
|
|
239
|
+
return store;
|
|
154
240
|
}
|
|
155
241
|
|
|
156
242
|
/**
|
|
@@ -164,27 +250,33 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
164
250
|
}, {
|
|
165
251
|
key: "add",
|
|
166
252
|
value: (function () {
|
|
167
|
-
var _add = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
168
|
-
var
|
|
253
|
+
var _add = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(storeName, data) {
|
|
254
|
+
var _this4 = this;
|
|
169
255
|
var log,
|
|
170
|
-
|
|
171
|
-
|
|
256
|
+
storeConfig,
|
|
257
|
+
keyPath,
|
|
258
|
+
_key,
|
|
259
|
+
memStore,
|
|
172
260
|
uuid,
|
|
173
|
-
|
|
174
|
-
return _regeneratorRuntime().wrap(function
|
|
175
|
-
while (1) switch (
|
|
261
|
+
_args4 = arguments;
|
|
262
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
263
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
176
264
|
case 0:
|
|
177
|
-
log =
|
|
265
|
+
log = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : false;
|
|
178
266
|
if (this.useIndexDB) {
|
|
179
|
-
|
|
267
|
+
_context4.next = 8;
|
|
180
268
|
break;
|
|
181
269
|
}
|
|
182
|
-
|
|
270
|
+
// 使用内存存储
|
|
271
|
+
storeConfig = this.stores.find(function (s) {
|
|
183
272
|
return s.name === storeName;
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
273
|
+
});
|
|
274
|
+
keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
|
|
275
|
+
_key = data[keyPath];
|
|
276
|
+
memStore = this.getMemoryStore(storeName);
|
|
277
|
+
memStore.set(_key, data);
|
|
278
|
+
return _context4.abrupt("return", data);
|
|
279
|
+
case 8:
|
|
188
280
|
uuid = "[ IndexDB ] ADD: - ".concat(storeName, " - ").concat(dayjs().valueOf());
|
|
189
281
|
if (log) {
|
|
190
282
|
this.app.logger.addLog({
|
|
@@ -196,92 +288,73 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
196
288
|
}
|
|
197
289
|
});
|
|
198
290
|
}
|
|
199
|
-
return
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
msg: '添加数据前 数据库未连接',
|
|
207
|
-
data: data
|
|
291
|
+
return _context4.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
292
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
293
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
294
|
+
case 0:
|
|
295
|
+
if (_this4.db) {
|
|
296
|
+
_context3.next = 3;
|
|
297
|
+
break;
|
|
208
298
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (log) {
|
|
219
|
-
_this2.app.logger.addLog({
|
|
220
|
-
type: 'info',
|
|
221
|
-
title: uuid,
|
|
222
|
-
metadata: {
|
|
223
|
-
msg: '添加数据成功'
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
return resolve(data);
|
|
228
|
-
};
|
|
229
|
-
request.onerror = function () {
|
|
230
|
-
if (log) {
|
|
231
|
-
_this2.app.logger.addLog({
|
|
232
|
-
type: 'error',
|
|
233
|
-
title: uuid,
|
|
234
|
-
metadata: {
|
|
235
|
-
msg: '添加数据失败'
|
|
299
|
+
if (log) {
|
|
300
|
+
_this4.app.logger.addLog({
|
|
301
|
+
type: 'error',
|
|
302
|
+
title: uuid,
|
|
303
|
+
metadata: {
|
|
304
|
+
msg: '添加数据前 数据库未连接',
|
|
305
|
+
data: data
|
|
306
|
+
}
|
|
307
|
+
});
|
|
236
308
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
309
|
+
throw new Error('数据库未连接');
|
|
310
|
+
case 3:
|
|
311
|
+
_context3.prev = 3;
|
|
312
|
+
_context3.next = 6;
|
|
313
|
+
return _this4.db.table(storeName).add(data);
|
|
314
|
+
case 6:
|
|
315
|
+
if (log) {
|
|
316
|
+
_this4.app.logger.addLog({
|
|
317
|
+
type: 'info',
|
|
318
|
+
title: uuid,
|
|
319
|
+
metadata: {
|
|
320
|
+
msg: '添加数据成功'
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
console.log('✅ 添加事务完成');
|
|
324
|
+
_this4.app.logger.addLog({
|
|
325
|
+
type: 'info',
|
|
326
|
+
title: uuid,
|
|
327
|
+
metadata: {
|
|
328
|
+
msg: '事务完成'
|
|
329
|
+
}
|
|
330
|
+
});
|
|
249
331
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
return reject((_transaction$error = transaction.error) !== null && _transaction$error !== void 0 ? _transaction$error : new Error('事务错误'));
|
|
266
|
-
};
|
|
267
|
-
transaction.onabort = function (e) {
|
|
268
|
-
if (log) {
|
|
269
|
-
_this2.app.logger.addLog({
|
|
270
|
-
type: 'error',
|
|
271
|
-
title: uuid,
|
|
272
|
-
metadata: {
|
|
273
|
-
msg: '事务被中止'
|
|
332
|
+
return _context3.abrupt("return", data);
|
|
333
|
+
case 10:
|
|
334
|
+
_context3.prev = 10;
|
|
335
|
+
_context3.t0 = _context3["catch"](3);
|
|
336
|
+
if (log) {
|
|
337
|
+
_this4.app.logger.addLog({
|
|
338
|
+
type: 'error',
|
|
339
|
+
title: uuid,
|
|
340
|
+
metadata: {
|
|
341
|
+
msg: '添加数据失败',
|
|
342
|
+
error: _context3.t0.message
|
|
343
|
+
}
|
|
344
|
+
});
|
|
274
345
|
}
|
|
275
|
-
|
|
346
|
+
throw new Error('添加数据失败');
|
|
347
|
+
case 14:
|
|
348
|
+
case "end":
|
|
349
|
+
return _context3.stop();
|
|
276
350
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
case 8:
|
|
351
|
+
}, _callee3, null, [[3, 10]]);
|
|
352
|
+
}))(), "add(".concat(storeName, ")")));
|
|
353
|
+
case 11:
|
|
281
354
|
case "end":
|
|
282
|
-
return
|
|
355
|
+
return _context4.stop();
|
|
283
356
|
}
|
|
284
|
-
},
|
|
357
|
+
}, _callee4, this);
|
|
285
358
|
}));
|
|
286
359
|
function add(_x, _x2) {
|
|
287
360
|
return _add.apply(this, arguments);
|
|
@@ -298,39 +371,52 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
298
371
|
}, {
|
|
299
372
|
key: "exists",
|
|
300
373
|
value: (function () {
|
|
301
|
-
var _exists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
302
|
-
var
|
|
303
|
-
var
|
|
304
|
-
return _regeneratorRuntime().wrap(function
|
|
305
|
-
while (1) switch (
|
|
374
|
+
var _exists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(storeName, key) {
|
|
375
|
+
var _this5 = this;
|
|
376
|
+
var memStore;
|
|
377
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
378
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
306
379
|
case 0:
|
|
307
380
|
if (this.useIndexDB) {
|
|
308
|
-
|
|
381
|
+
_context6.next = 3;
|
|
309
382
|
break;
|
|
310
383
|
}
|
|
311
|
-
|
|
312
|
-
|
|
384
|
+
// 使用内存存储
|
|
385
|
+
memStore = this.getMemoryStore(storeName);
|
|
386
|
+
return _context6.abrupt("return", memStore.has(key));
|
|
313
387
|
case 3:
|
|
314
|
-
return
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
388
|
+
return _context6.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
389
|
+
var result;
|
|
390
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
391
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
392
|
+
case 0:
|
|
393
|
+
if (_this5.db) {
|
|
394
|
+
_context5.next = 2;
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
throw new Error('数据库未连接');
|
|
398
|
+
case 2:
|
|
399
|
+
_context5.prev = 2;
|
|
400
|
+
_context5.next = 5;
|
|
401
|
+
return _this5.db.table(storeName).get(key);
|
|
402
|
+
case 5:
|
|
403
|
+
result = _context5.sent;
|
|
404
|
+
return _context5.abrupt("return", result !== undefined);
|
|
405
|
+
case 9:
|
|
406
|
+
_context5.prev = 9;
|
|
407
|
+
_context5.t0 = _context5["catch"](2);
|
|
408
|
+
throw new Error('检查数据存在性失败');
|
|
409
|
+
case 12:
|
|
410
|
+
case "end":
|
|
411
|
+
return _context5.stop();
|
|
412
|
+
}
|
|
413
|
+
}, _callee5, null, [[2, 9]]);
|
|
414
|
+
}))(), "exists(".concat(storeName, ")")));
|
|
329
415
|
case 4:
|
|
330
416
|
case "end":
|
|
331
|
-
return
|
|
417
|
+
return _context6.stop();
|
|
332
418
|
}
|
|
333
|
-
},
|
|
419
|
+
}, _callee6, this);
|
|
334
420
|
}));
|
|
335
421
|
function exists(_x3, _x4) {
|
|
336
422
|
return _exists.apply(this, arguments);
|
|
@@ -349,17 +435,17 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
349
435
|
}, {
|
|
350
436
|
key: "get",
|
|
351
437
|
value: (function () {
|
|
352
|
-
var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
353
|
-
var
|
|
438
|
+
var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(storeName, key) {
|
|
439
|
+
var _this6 = this;
|
|
354
440
|
var log,
|
|
355
441
|
uuid,
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
return _regeneratorRuntime().wrap(function
|
|
360
|
-
while (1) switch (
|
|
442
|
+
_memStore$get,
|
|
443
|
+
memStore,
|
|
444
|
+
_args8 = arguments;
|
|
445
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
446
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
361
447
|
case 0:
|
|
362
|
-
log =
|
|
448
|
+
log = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : false;
|
|
363
449
|
uuid = "[ IndexDB ] GET: - ".concat(storeName, " - ").concat(key, " - ").concat(dayjs().valueOf());
|
|
364
450
|
if (log) {
|
|
365
451
|
this.app.logger.addLog({
|
|
@@ -371,108 +457,80 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
371
457
|
});
|
|
372
458
|
}
|
|
373
459
|
if (this.useIndexDB) {
|
|
374
|
-
|
|
460
|
+
_context8.next = 6;
|
|
375
461
|
break;
|
|
376
462
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
463
|
+
// 使用内存存储
|
|
464
|
+
memStore = this.getMemoryStore(storeName);
|
|
465
|
+
return _context8.abrupt("return", (_memStore$get = memStore.get(key)) !== null && _memStore$get !== void 0 ? _memStore$get : null);
|
|
466
|
+
case 6:
|
|
467
|
+
return _context8.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
|
|
468
|
+
var result;
|
|
469
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
470
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
471
|
+
case 0:
|
|
472
|
+
if (_this6.db) {
|
|
473
|
+
_context7.next = 3;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
if (log) {
|
|
477
|
+
_this6.app.logger.addLog({
|
|
478
|
+
type: 'error',
|
|
479
|
+
title: uuid,
|
|
480
|
+
metadata: {
|
|
481
|
+
msg: '数据库未连接'
|
|
482
|
+
}
|
|
483
|
+
});
|
|
392
484
|
}
|
|
393
|
-
|
|
485
|
+
throw new Error('数据库未连接');
|
|
486
|
+
case 3:
|
|
487
|
+
_context7.prev = 3;
|
|
488
|
+
_context7.next = 6;
|
|
489
|
+
return _this6.db.table(storeName).get(key);
|
|
490
|
+
case 6:
|
|
491
|
+
result = _context7.sent;
|
|
492
|
+
if (log) {
|
|
493
|
+
_this6.app.logger.addLog({
|
|
494
|
+
type: 'info',
|
|
495
|
+
title: uuid,
|
|
496
|
+
metadata: {
|
|
497
|
+
msg: '获取成功'
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
_this6.app.logger.addLog({
|
|
501
|
+
type: 'info',
|
|
502
|
+
title: uuid,
|
|
503
|
+
metadata: {
|
|
504
|
+
msg: '事务完成'
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return _context7.abrupt("return", result !== null && result !== void 0 ? result : null);
|
|
509
|
+
case 11:
|
|
510
|
+
_context7.prev = 11;
|
|
511
|
+
_context7.t0 = _context7["catch"](3);
|
|
512
|
+
if (log) {
|
|
513
|
+
_this6.app.logger.addLog({
|
|
514
|
+
type: 'error',
|
|
515
|
+
title: uuid,
|
|
516
|
+
metadata: {
|
|
517
|
+
msg: '获取失败',
|
|
518
|
+
error: _context7.t0.message
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
throw new Error('获取数据失败');
|
|
523
|
+
case 15:
|
|
524
|
+
case "end":
|
|
525
|
+
return _context7.stop();
|
|
394
526
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
var resolved = false;
|
|
399
|
-
try {
|
|
400
|
-
var transaction = _this4.db.transaction(storeName, 'readonly');
|
|
401
|
-
var store = transaction.objectStore(storeName);
|
|
402
|
-
var request = store.get(key);
|
|
403
|
-
request.onsuccess = function () {
|
|
404
|
-
var _request$result;
|
|
405
|
-
resolved = true;
|
|
406
|
-
if (log) {
|
|
407
|
-
_this4.app.logger.addLog({
|
|
408
|
-
type: 'info',
|
|
409
|
-
title: uuid,
|
|
410
|
-
metadata: {
|
|
411
|
-
msg: '获取成功'
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
resolve((_request$result = request.result) !== null && _request$result !== void 0 ? _request$result : null);
|
|
416
|
-
};
|
|
417
|
-
request.onerror = function () {
|
|
418
|
-
var _request$error;
|
|
419
|
-
resolved = true;
|
|
420
|
-
if (log) {
|
|
421
|
-
_this4.app.logger.addLog({
|
|
422
|
-
type: 'error',
|
|
423
|
-
title: uuid,
|
|
424
|
-
metadata: {
|
|
425
|
-
msg: '获取失败'
|
|
426
|
-
}
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
reject((_request$error = request.error) !== null && _request$error !== void 0 ? _request$error : new Error('获取数据失败'));
|
|
430
|
-
};
|
|
431
|
-
transaction.onabort = function () {
|
|
432
|
-
if (log) {
|
|
433
|
-
_this4.app.logger.addLog({
|
|
434
|
-
type: 'info',
|
|
435
|
-
title: uuid,
|
|
436
|
-
metadata: {
|
|
437
|
-
msg: '事务被中止'
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
if (!resolved) reject(new Error('事务被中止'));
|
|
442
|
-
};
|
|
443
|
-
transaction.onerror = function (event) {
|
|
444
|
-
if (log) {
|
|
445
|
-
_this4.app.logger.addLog({
|
|
446
|
-
type: 'error',
|
|
447
|
-
title: uuid,
|
|
448
|
-
metadata: {
|
|
449
|
-
msg: "\u4E8B\u52A1\u9519\u8BEF: ".concat(event.target.error)
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
if (!resolved) reject(new Error("\u4E8B\u52A1\u9519\u8BEF: ".concat(event.target.error)));
|
|
454
|
-
};
|
|
455
|
-
transaction.oncomplete = function () {
|
|
456
|
-
if (log) {
|
|
457
|
-
_this4.app.logger.addLog({
|
|
458
|
-
type: 'info',
|
|
459
|
-
title: uuid,
|
|
460
|
-
metadata: {
|
|
461
|
-
msg: '事务完成'
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
// ✅ 事务顺利结束
|
|
466
|
-
};
|
|
467
|
-
} catch (e) {
|
|
468
|
-
reject(e);
|
|
469
|
-
}
|
|
470
|
-
}));
|
|
471
|
-
case 9:
|
|
527
|
+
}, _callee7, null, [[3, 11]]);
|
|
528
|
+
}))(), "get(".concat(storeName, ")")));
|
|
529
|
+
case 7:
|
|
472
530
|
case "end":
|
|
473
|
-
return
|
|
531
|
+
return _context8.stop();
|
|
474
532
|
}
|
|
475
|
-
},
|
|
533
|
+
}, _callee8, this);
|
|
476
534
|
}));
|
|
477
535
|
function get(_x5, _x6) {
|
|
478
536
|
return _get.apply(this, arguments);
|
|
@@ -491,27 +549,33 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
491
549
|
}, {
|
|
492
550
|
key: "update",
|
|
493
551
|
value: (function () {
|
|
494
|
-
var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
495
|
-
var
|
|
552
|
+
var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(storeName, data) {
|
|
553
|
+
var _this7 = this;
|
|
496
554
|
var log,
|
|
497
|
-
|
|
498
|
-
|
|
555
|
+
storeConfig,
|
|
556
|
+
keyPath,
|
|
557
|
+
_key2,
|
|
558
|
+
memStore,
|
|
499
559
|
uuid,
|
|
500
|
-
|
|
501
|
-
return _regeneratorRuntime().wrap(function
|
|
502
|
-
while (1) switch (
|
|
560
|
+
_args10 = arguments;
|
|
561
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
562
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
503
563
|
case 0:
|
|
504
|
-
log =
|
|
564
|
+
log = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : false;
|
|
505
565
|
if (this.useIndexDB) {
|
|
506
|
-
|
|
566
|
+
_context10.next = 8;
|
|
507
567
|
break;
|
|
508
568
|
}
|
|
509
|
-
|
|
569
|
+
// 使用内存存储
|
|
570
|
+
storeConfig = this.stores.find(function (s) {
|
|
510
571
|
return s.name === storeName;
|
|
511
|
-
})
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
572
|
+
});
|
|
573
|
+
keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
|
|
574
|
+
_key2 = data[keyPath];
|
|
575
|
+
memStore = this.getMemoryStore(storeName);
|
|
576
|
+
memStore.set(_key2, data);
|
|
577
|
+
return _context10.abrupt("return", data);
|
|
578
|
+
case 8:
|
|
515
579
|
uuid = "[ IndexDB ] UPDATE: - ".concat(storeName, " - ").concat(dayjs().valueOf());
|
|
516
580
|
if (log) {
|
|
517
581
|
this.app.logger.addLog({
|
|
@@ -523,95 +587,72 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
523
587
|
}
|
|
524
588
|
});
|
|
525
589
|
}
|
|
526
|
-
return
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
590
|
+
return _context10.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
591
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
592
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
593
|
+
case 0:
|
|
594
|
+
if (_this7.db) {
|
|
595
|
+
_context9.next = 3;
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
if (log) {
|
|
599
|
+
_this7.app.logger.addLog({
|
|
600
|
+
type: 'error',
|
|
601
|
+
title: uuid,
|
|
602
|
+
metadata: {
|
|
603
|
+
msg: '数据库未连接'
|
|
604
|
+
}
|
|
605
|
+
});
|
|
534
606
|
}
|
|
535
|
-
|
|
607
|
+
throw new Error('数据库未连接');
|
|
608
|
+
case 3:
|
|
609
|
+
_context9.prev = 3;
|
|
610
|
+
_context9.next = 6;
|
|
611
|
+
return _this7.db.table(storeName).put(data);
|
|
612
|
+
case 6:
|
|
613
|
+
if (log) {
|
|
614
|
+
_this7.app.logger.addLog({
|
|
615
|
+
type: 'info',
|
|
616
|
+
title: uuid,
|
|
617
|
+
metadata: {
|
|
618
|
+
msg: '数据更新完成'
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
console.log('✅ 事务完成');
|
|
622
|
+
_this7.app.logger.addLog({
|
|
623
|
+
type: 'info',
|
|
624
|
+
title: uuid,
|
|
625
|
+
metadata: {
|
|
626
|
+
msg: '事务完成'
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
return _context9.abrupt("return", data);
|
|
631
|
+
case 10:
|
|
632
|
+
_context9.prev = 10;
|
|
633
|
+
_context9.t0 = _context9["catch"](3);
|
|
634
|
+
if (log) {
|
|
635
|
+
_this7.app.logger.addLog({
|
|
636
|
+
type: 'error',
|
|
637
|
+
title: uuid,
|
|
638
|
+
metadata: {
|
|
639
|
+
msg: '数据更新失败',
|
|
640
|
+
error: _context9.t0.message
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
throw new Error('更新数据失败');
|
|
645
|
+
case 14:
|
|
646
|
+
case "end":
|
|
647
|
+
return _context9.stop();
|
|
536
648
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
try {
|
|
541
|
-
var transaction = _this5.db.transaction(storeName, 'readwrite');
|
|
542
|
-
var store = transaction.objectStore(storeName);
|
|
543
|
-
var request = store.put(data);
|
|
544
|
-
request.onsuccess = function () {
|
|
545
|
-
if (log) {
|
|
546
|
-
_this5.app.logger.addLog({
|
|
547
|
-
type: 'info',
|
|
548
|
-
title: uuid,
|
|
549
|
-
metadata: {
|
|
550
|
-
msg: '数据更新完成'
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
return resolve(data);
|
|
555
|
-
};
|
|
556
|
-
request.onerror = function () {
|
|
557
|
-
var _request$error2;
|
|
558
|
-
if (log) {
|
|
559
|
-
_this5.app.logger.addLog({
|
|
560
|
-
type: 'error',
|
|
561
|
-
title: uuid,
|
|
562
|
-
metadata: {
|
|
563
|
-
msg: '数据更新失败'
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
return reject((_request$error2 = request.error) !== null && _request$error2 !== void 0 ? _request$error2 : new Error('更新数据失败'));
|
|
568
|
-
};
|
|
569
|
-
transaction.oncomplete = function () {
|
|
570
|
-
if (log) {
|
|
571
|
-
_this5.app.logger.addLog({
|
|
572
|
-
type: 'info',
|
|
573
|
-
title: uuid,
|
|
574
|
-
metadata: {
|
|
575
|
-
msg: '事务完成'
|
|
576
|
-
}
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
return console.log('✅ 事务完成');
|
|
580
|
-
};
|
|
581
|
-
transaction.onabort = function (e) {
|
|
582
|
-
if (log) {
|
|
583
|
-
_this5.app.logger.addLog({
|
|
584
|
-
type: 'error',
|
|
585
|
-
title: uuid,
|
|
586
|
-
metadata: {
|
|
587
|
-
msg: '事务被中止'
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
return reject(new Error('事务被中止'));
|
|
592
|
-
};
|
|
593
|
-
transaction.onerror = function (e) {
|
|
594
|
-
var _transaction$error2;
|
|
595
|
-
if (log) {
|
|
596
|
-
_this5.app.logger.addLog({
|
|
597
|
-
type: 'error',
|
|
598
|
-
title: uuid,
|
|
599
|
-
metadata: {
|
|
600
|
-
msg: '事务错误'
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
return reject((_transaction$error2 = transaction.error) !== null && _transaction$error2 !== void 0 ? _transaction$error2 : new Error('事务错误'));
|
|
605
|
-
};
|
|
606
|
-
} catch (e) {
|
|
607
|
-
reject(e);
|
|
608
|
-
}
|
|
609
|
-
}));
|
|
610
|
-
case 8:
|
|
649
|
+
}, _callee9, null, [[3, 10]]);
|
|
650
|
+
}))(), "update(".concat(storeName, ")")));
|
|
651
|
+
case 11:
|
|
611
652
|
case "end":
|
|
612
|
-
return
|
|
653
|
+
return _context10.stop();
|
|
613
654
|
}
|
|
614
|
-
},
|
|
655
|
+
}, _callee10, this);
|
|
615
656
|
}));
|
|
616
657
|
function update(_x7, _x8) {
|
|
617
658
|
return _update.apply(this, arguments);
|
|
@@ -628,40 +669,50 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
628
669
|
}, {
|
|
629
670
|
key: "delete",
|
|
630
671
|
value: (function () {
|
|
631
|
-
var _delete2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
632
|
-
var
|
|
633
|
-
var
|
|
634
|
-
return _regeneratorRuntime().wrap(function
|
|
635
|
-
while (1) switch (
|
|
672
|
+
var _delete2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(storeName, key) {
|
|
673
|
+
var _this8 = this;
|
|
674
|
+
var memStore;
|
|
675
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
676
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
636
677
|
case 0:
|
|
637
678
|
if (this.useIndexDB) {
|
|
638
|
-
|
|
679
|
+
_context12.next = 3;
|
|
639
680
|
break;
|
|
640
681
|
}
|
|
641
|
-
|
|
642
|
-
this.
|
|
643
|
-
return
|
|
682
|
+
// 使用内存存储
|
|
683
|
+
memStore = this.getMemoryStore(storeName);
|
|
684
|
+
return _context12.abrupt("return", memStore.delete(key));
|
|
685
|
+
case 3:
|
|
686
|
+
return _context12.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
687
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
688
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
689
|
+
case 0:
|
|
690
|
+
if (_this8.db) {
|
|
691
|
+
_context11.next = 2;
|
|
692
|
+
break;
|
|
693
|
+
}
|
|
694
|
+
throw new Error('数据库未连接');
|
|
695
|
+
case 2:
|
|
696
|
+
_context11.prev = 2;
|
|
697
|
+
_context11.next = 5;
|
|
698
|
+
return _this8.db.table(storeName).delete(key);
|
|
699
|
+
case 5:
|
|
700
|
+
return _context11.abrupt("return", true);
|
|
701
|
+
case 8:
|
|
702
|
+
_context11.prev = 8;
|
|
703
|
+
_context11.t0 = _context11["catch"](2);
|
|
704
|
+
throw new Error('删除数据失败');
|
|
705
|
+
case 11:
|
|
706
|
+
case "end":
|
|
707
|
+
return _context11.stop();
|
|
708
|
+
}
|
|
709
|
+
}, _callee11, null, [[2, 8]]);
|
|
710
|
+
}))(), "delete(".concat(storeName, ")")));
|
|
644
711
|
case 4:
|
|
645
|
-
return _context6.abrupt("return", new Promise(function (resolve, reject) {
|
|
646
|
-
if (!_this6.db) {
|
|
647
|
-
reject(new Error('数据库未连接'));
|
|
648
|
-
return;
|
|
649
|
-
}
|
|
650
|
-
var transaction = _this6.db.transaction(storeName, 'readwrite');
|
|
651
|
-
var store = transaction.objectStore(storeName);
|
|
652
|
-
var request = store.delete(key);
|
|
653
|
-
request.onsuccess = function () {
|
|
654
|
-
return resolve(true);
|
|
655
|
-
};
|
|
656
|
-
request.onerror = function () {
|
|
657
|
-
return reject(new Error('删除数据失败'));
|
|
658
|
-
};
|
|
659
|
-
}));
|
|
660
|
-
case 5:
|
|
661
712
|
case "end":
|
|
662
|
-
return
|
|
713
|
+
return _context12.stop();
|
|
663
714
|
}
|
|
664
|
-
},
|
|
715
|
+
}, _callee12, this);
|
|
665
716
|
}));
|
|
666
717
|
function _delete(_x9, _x10) {
|
|
667
718
|
return _delete2.apply(this, arguments);
|
|
@@ -680,20 +731,20 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
680
731
|
}, {
|
|
681
732
|
key: "getByIndex",
|
|
682
733
|
value: (function () {
|
|
683
|
-
var _getByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
684
|
-
var
|
|
734
|
+
var _getByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(storeName, indexName, indexValue) {
|
|
735
|
+
var _this9 = this;
|
|
685
736
|
var _storeConfig$indexes, allData, storeConfig, index;
|
|
686
|
-
return _regeneratorRuntime().wrap(function
|
|
687
|
-
while (1) switch (
|
|
737
|
+
return _regeneratorRuntime().wrap(function _callee14$(_context14) {
|
|
738
|
+
while (1) switch (_context14.prev = _context14.next) {
|
|
688
739
|
case 0:
|
|
689
740
|
if (this.useIndexDB) {
|
|
690
|
-
|
|
741
|
+
_context14.next = 9;
|
|
691
742
|
break;
|
|
692
743
|
}
|
|
693
|
-
|
|
744
|
+
_context14.next = 3;
|
|
694
745
|
return this.getAll(storeName);
|
|
695
746
|
case 3:
|
|
696
|
-
allData =
|
|
747
|
+
allData = _context14.sent;
|
|
697
748
|
storeConfig = this.stores.find(function (s) {
|
|
698
749
|
return s.name === storeName;
|
|
699
750
|
});
|
|
@@ -701,36 +752,47 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
701
752
|
return i.name === indexName;
|
|
702
753
|
});
|
|
703
754
|
if (index) {
|
|
704
|
-
|
|
755
|
+
_context14.next = 8;
|
|
705
756
|
break;
|
|
706
757
|
}
|
|
707
|
-
return
|
|
758
|
+
return _context14.abrupt("return", null);
|
|
708
759
|
case 8:
|
|
709
|
-
return
|
|
760
|
+
return _context14.abrupt("return", allData.find(function (item) {
|
|
710
761
|
return item[index.keyPath] === indexValue;
|
|
711
762
|
}) || null);
|
|
712
763
|
case 9:
|
|
713
|
-
return
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
764
|
+
return _context14.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
|
|
765
|
+
var result;
|
|
766
|
+
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
767
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
768
|
+
case 0:
|
|
769
|
+
if (_this9.db) {
|
|
770
|
+
_context13.next = 2;
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
throw new Error('数据库未连接');
|
|
774
|
+
case 2:
|
|
775
|
+
_context13.prev = 2;
|
|
776
|
+
_context13.next = 5;
|
|
777
|
+
return _this9.db.table(storeName).where(indexName).equals(indexValue).first();
|
|
778
|
+
case 5:
|
|
779
|
+
result = _context13.sent;
|
|
780
|
+
return _context13.abrupt("return", result || null);
|
|
781
|
+
case 9:
|
|
782
|
+
_context13.prev = 9;
|
|
783
|
+
_context13.t0 = _context13["catch"](2);
|
|
784
|
+
throw new Error('通过索引获取数据失败');
|
|
785
|
+
case 12:
|
|
786
|
+
case "end":
|
|
787
|
+
return _context13.stop();
|
|
788
|
+
}
|
|
789
|
+
}, _callee13, null, [[2, 9]]);
|
|
790
|
+
}))(), "getByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
729
791
|
case 10:
|
|
730
792
|
case "end":
|
|
731
|
-
return
|
|
793
|
+
return _context14.stop();
|
|
732
794
|
}
|
|
733
|
-
},
|
|
795
|
+
}, _callee14, this);
|
|
734
796
|
}));
|
|
735
797
|
function getByIndex(_x11, _x12, _x13) {
|
|
736
798
|
return _getByIndex.apply(this, arguments);
|
|
@@ -748,20 +810,20 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
748
810
|
}, {
|
|
749
811
|
key: "existsByIndex",
|
|
750
812
|
value: (function () {
|
|
751
|
-
var _existsByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
752
|
-
var
|
|
813
|
+
var _existsByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(storeName, indexName, indexValue) {
|
|
814
|
+
var _this10 = this;
|
|
753
815
|
var _storeConfig$indexes2, allData, storeConfig, index;
|
|
754
|
-
return _regeneratorRuntime().wrap(function
|
|
755
|
-
while (1) switch (
|
|
816
|
+
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
|
|
817
|
+
while (1) switch (_context16.prev = _context16.next) {
|
|
756
818
|
case 0:
|
|
757
819
|
if (this.useIndexDB) {
|
|
758
|
-
|
|
820
|
+
_context16.next = 9;
|
|
759
821
|
break;
|
|
760
822
|
}
|
|
761
|
-
|
|
823
|
+
_context16.next = 3;
|
|
762
824
|
return this.getAll(storeName);
|
|
763
825
|
case 3:
|
|
764
|
-
allData =
|
|
826
|
+
allData = _context16.sent;
|
|
765
827
|
storeConfig = this.stores.find(function (s) {
|
|
766
828
|
return s.name === storeName;
|
|
767
829
|
});
|
|
@@ -769,36 +831,47 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
769
831
|
return i.name === indexName;
|
|
770
832
|
});
|
|
771
833
|
if (index) {
|
|
772
|
-
|
|
834
|
+
_context16.next = 8;
|
|
773
835
|
break;
|
|
774
836
|
}
|
|
775
|
-
return
|
|
837
|
+
return _context16.abrupt("return", false);
|
|
776
838
|
case 8:
|
|
777
|
-
return
|
|
839
|
+
return _context16.abrupt("return", allData.some(function (item) {
|
|
778
840
|
return item[index.keyPath] === indexValue;
|
|
779
841
|
}));
|
|
780
842
|
case 9:
|
|
781
|
-
return
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
843
|
+
return _context16.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15() {
|
|
844
|
+
var count;
|
|
845
|
+
return _regeneratorRuntime().wrap(function _callee15$(_context15) {
|
|
846
|
+
while (1) switch (_context15.prev = _context15.next) {
|
|
847
|
+
case 0:
|
|
848
|
+
if (_this10.db) {
|
|
849
|
+
_context15.next = 2;
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
throw new Error('数据库未连接');
|
|
853
|
+
case 2:
|
|
854
|
+
_context15.prev = 2;
|
|
855
|
+
_context15.next = 5;
|
|
856
|
+
return _this10.db.table(storeName).where(indexName).equals(indexValue).count();
|
|
857
|
+
case 5:
|
|
858
|
+
count = _context15.sent;
|
|
859
|
+
return _context15.abrupt("return", count > 0);
|
|
860
|
+
case 9:
|
|
861
|
+
_context15.prev = 9;
|
|
862
|
+
_context15.t0 = _context15["catch"](2);
|
|
863
|
+
throw new Error('通过索引检查数据存在性失败');
|
|
864
|
+
case 12:
|
|
865
|
+
case "end":
|
|
866
|
+
return _context15.stop();
|
|
867
|
+
}
|
|
868
|
+
}, _callee15, null, [[2, 9]]);
|
|
869
|
+
}))(), "existsByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
797
870
|
case 10:
|
|
798
871
|
case "end":
|
|
799
|
-
return
|
|
872
|
+
return _context16.stop();
|
|
800
873
|
}
|
|
801
|
-
},
|
|
874
|
+
}, _callee16, this);
|
|
802
875
|
}));
|
|
803
876
|
function existsByIndex(_x14, _x15, _x16) {
|
|
804
877
|
return _existsByIndex.apply(this, arguments);
|
|
@@ -817,20 +890,20 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
817
890
|
}, {
|
|
818
891
|
key: "getAllByIndex",
|
|
819
892
|
value: (function () {
|
|
820
|
-
var _getAllByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
821
|
-
var
|
|
893
|
+
var _getAllByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(storeName, indexName, indexValue) {
|
|
894
|
+
var _this11 = this;
|
|
822
895
|
var _storeConfig$indexes3, allData, storeConfig, index;
|
|
823
|
-
return _regeneratorRuntime().wrap(function
|
|
824
|
-
while (1) switch (
|
|
896
|
+
return _regeneratorRuntime().wrap(function _callee18$(_context18) {
|
|
897
|
+
while (1) switch (_context18.prev = _context18.next) {
|
|
825
898
|
case 0:
|
|
826
899
|
if (this.useIndexDB) {
|
|
827
|
-
|
|
900
|
+
_context18.next = 9;
|
|
828
901
|
break;
|
|
829
902
|
}
|
|
830
|
-
|
|
903
|
+
_context18.next = 3;
|
|
831
904
|
return this.getAll(storeName);
|
|
832
905
|
case 3:
|
|
833
|
-
allData =
|
|
906
|
+
allData = _context18.sent;
|
|
834
907
|
storeConfig = this.stores.find(function (s) {
|
|
835
908
|
return s.name === storeName;
|
|
836
909
|
});
|
|
@@ -838,36 +911,47 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
838
911
|
return i.name === indexName;
|
|
839
912
|
});
|
|
840
913
|
if (index) {
|
|
841
|
-
|
|
914
|
+
_context18.next = 8;
|
|
842
915
|
break;
|
|
843
916
|
}
|
|
844
|
-
return
|
|
917
|
+
return _context18.abrupt("return", []);
|
|
845
918
|
case 8:
|
|
846
|
-
return
|
|
919
|
+
return _context18.abrupt("return", allData.filter(function (item) {
|
|
847
920
|
return item[index.keyPath] === indexValue;
|
|
848
921
|
}));
|
|
849
922
|
case 9:
|
|
850
|
-
return
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
923
|
+
return _context18.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
|
|
924
|
+
var results;
|
|
925
|
+
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
|
|
926
|
+
while (1) switch (_context17.prev = _context17.next) {
|
|
927
|
+
case 0:
|
|
928
|
+
if (_this11.db) {
|
|
929
|
+
_context17.next = 2;
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
throw new Error('数据库未连接');
|
|
933
|
+
case 2:
|
|
934
|
+
_context17.prev = 2;
|
|
935
|
+
_context17.next = 5;
|
|
936
|
+
return _this11.db.table(storeName).where(indexName).equals(indexValue).toArray();
|
|
937
|
+
case 5:
|
|
938
|
+
results = _context17.sent;
|
|
939
|
+
return _context17.abrupt("return", results);
|
|
940
|
+
case 9:
|
|
941
|
+
_context17.prev = 9;
|
|
942
|
+
_context17.t0 = _context17["catch"](2);
|
|
943
|
+
throw new Error('通过索引获取多条数据失败');
|
|
944
|
+
case 12:
|
|
945
|
+
case "end":
|
|
946
|
+
return _context17.stop();
|
|
947
|
+
}
|
|
948
|
+
}, _callee17, null, [[2, 9]]);
|
|
949
|
+
}))(), "getAllByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
866
950
|
case 10:
|
|
867
951
|
case "end":
|
|
868
|
-
return
|
|
952
|
+
return _context18.stop();
|
|
869
953
|
}
|
|
870
|
-
},
|
|
954
|
+
}, _callee18, this);
|
|
871
955
|
}));
|
|
872
956
|
function getAllByIndex(_x17, _x18, _x19) {
|
|
873
957
|
return _getAllByIndex.apply(this, arguments);
|
|
@@ -883,42 +967,54 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
883
967
|
}, {
|
|
884
968
|
key: "count",
|
|
885
969
|
value: (function () {
|
|
886
|
-
var _count = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
887
|
-
var
|
|
970
|
+
var _count = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20(storeName) {
|
|
971
|
+
var _this12 = this;
|
|
888
972
|
var allData;
|
|
889
|
-
return _regeneratorRuntime().wrap(function
|
|
890
|
-
while (1) switch (
|
|
973
|
+
return _regeneratorRuntime().wrap(function _callee20$(_context20) {
|
|
974
|
+
while (1) switch (_context20.prev = _context20.next) {
|
|
891
975
|
case 0:
|
|
892
976
|
if (this.useIndexDB) {
|
|
893
|
-
|
|
977
|
+
_context20.next = 5;
|
|
894
978
|
break;
|
|
895
979
|
}
|
|
896
|
-
|
|
980
|
+
_context20.next = 3;
|
|
897
981
|
return this.getAll(storeName);
|
|
898
982
|
case 3:
|
|
899
|
-
allData =
|
|
900
|
-
return
|
|
983
|
+
allData = _context20.sent;
|
|
984
|
+
return _context20.abrupt("return", allData.length);
|
|
901
985
|
case 5:
|
|
902
|
-
return
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
986
|
+
return _context20.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19() {
|
|
987
|
+
var _count2;
|
|
988
|
+
return _regeneratorRuntime().wrap(function _callee19$(_context19) {
|
|
989
|
+
while (1) switch (_context19.prev = _context19.next) {
|
|
990
|
+
case 0:
|
|
991
|
+
if (_this12.db) {
|
|
992
|
+
_context19.next = 2;
|
|
993
|
+
break;
|
|
994
|
+
}
|
|
995
|
+
throw new Error('数据库未连接');
|
|
996
|
+
case 2:
|
|
997
|
+
_context19.prev = 2;
|
|
998
|
+
_context19.next = 5;
|
|
999
|
+
return _this12.db.table(storeName).count();
|
|
1000
|
+
case 5:
|
|
1001
|
+
_count2 = _context19.sent;
|
|
1002
|
+
return _context19.abrupt("return", _count2);
|
|
1003
|
+
case 9:
|
|
1004
|
+
_context19.prev = 9;
|
|
1005
|
+
_context19.t0 = _context19["catch"](2);
|
|
1006
|
+
throw new Error('统计数据数量失败');
|
|
1007
|
+
case 12:
|
|
1008
|
+
case "end":
|
|
1009
|
+
return _context19.stop();
|
|
1010
|
+
}
|
|
1011
|
+
}, _callee19, null, [[2, 9]]);
|
|
1012
|
+
}))(), "count(".concat(storeName, ")")));
|
|
917
1013
|
case 6:
|
|
918
1014
|
case "end":
|
|
919
|
-
return
|
|
1015
|
+
return _context20.stop();
|
|
920
1016
|
}
|
|
921
|
-
},
|
|
1017
|
+
}, _callee20, this);
|
|
922
1018
|
}));
|
|
923
1019
|
function count(_x20) {
|
|
924
1020
|
return _count.apply(this, arguments);
|
|
@@ -936,43 +1032,54 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
936
1032
|
}, {
|
|
937
1033
|
key: "countByIndex",
|
|
938
1034
|
value: (function () {
|
|
939
|
-
var _countByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
940
|
-
var
|
|
1035
|
+
var _countByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22(storeName, indexName, indexValue) {
|
|
1036
|
+
var _this13 = this;
|
|
941
1037
|
var matchingData;
|
|
942
|
-
return _regeneratorRuntime().wrap(function
|
|
943
|
-
while (1) switch (
|
|
1038
|
+
return _regeneratorRuntime().wrap(function _callee22$(_context22) {
|
|
1039
|
+
while (1) switch (_context22.prev = _context22.next) {
|
|
944
1040
|
case 0:
|
|
945
1041
|
if (this.useIndexDB) {
|
|
946
|
-
|
|
1042
|
+
_context22.next = 5;
|
|
947
1043
|
break;
|
|
948
1044
|
}
|
|
949
|
-
|
|
1045
|
+
_context22.next = 3;
|
|
950
1046
|
return this.getAllByIndex(storeName, indexName, indexValue);
|
|
951
1047
|
case 3:
|
|
952
|
-
matchingData =
|
|
953
|
-
return
|
|
1048
|
+
matchingData = _context22.sent;
|
|
1049
|
+
return _context22.abrupt("return", matchingData.length);
|
|
954
1050
|
case 5:
|
|
955
|
-
return
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1051
|
+
return _context22.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21() {
|
|
1052
|
+
var count;
|
|
1053
|
+
return _regeneratorRuntime().wrap(function _callee21$(_context21) {
|
|
1054
|
+
while (1) switch (_context21.prev = _context21.next) {
|
|
1055
|
+
case 0:
|
|
1056
|
+
if (_this13.db) {
|
|
1057
|
+
_context21.next = 2;
|
|
1058
|
+
break;
|
|
1059
|
+
}
|
|
1060
|
+
throw new Error('数据库未连接');
|
|
1061
|
+
case 2:
|
|
1062
|
+
_context21.prev = 2;
|
|
1063
|
+
_context21.next = 5;
|
|
1064
|
+
return _this13.db.table(storeName).where(indexName).equals(indexValue).count();
|
|
1065
|
+
case 5:
|
|
1066
|
+
count = _context21.sent;
|
|
1067
|
+
return _context21.abrupt("return", count);
|
|
1068
|
+
case 9:
|
|
1069
|
+
_context21.prev = 9;
|
|
1070
|
+
_context21.t0 = _context21["catch"](2);
|
|
1071
|
+
throw new Error('通过索引统计数据数量失败');
|
|
1072
|
+
case 12:
|
|
1073
|
+
case "end":
|
|
1074
|
+
return _context21.stop();
|
|
1075
|
+
}
|
|
1076
|
+
}, _callee21, null, [[2, 9]]);
|
|
1077
|
+
}))(), "countByIndex(".concat(storeName, ", ").concat(indexName, ")")));
|
|
971
1078
|
case 6:
|
|
972
1079
|
case "end":
|
|
973
|
-
return
|
|
1080
|
+
return _context22.stop();
|
|
974
1081
|
}
|
|
975
|
-
},
|
|
1082
|
+
}, _callee22, this);
|
|
976
1083
|
}));
|
|
977
1084
|
function countByIndex(_x21, _x22, _x23) {
|
|
978
1085
|
return _countByIndex.apply(this, arguments);
|
|
@@ -989,52 +1096,52 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
989
1096
|
}, {
|
|
990
1097
|
key: "getAll",
|
|
991
1098
|
value: (function () {
|
|
992
|
-
var _getAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
993
|
-
var
|
|
994
|
-
var
|
|
995
|
-
return _regeneratorRuntime().wrap(function
|
|
996
|
-
while (1) switch (
|
|
1099
|
+
var _getAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee24(storeName) {
|
|
1100
|
+
var _this14 = this;
|
|
1101
|
+
var memStore;
|
|
1102
|
+
return _regeneratorRuntime().wrap(function _callee24$(_context24) {
|
|
1103
|
+
while (1) switch (_context24.prev = _context24.next) {
|
|
997
1104
|
case 0:
|
|
998
1105
|
if (this.useIndexDB) {
|
|
999
|
-
|
|
1106
|
+
_context24.next = 3;
|
|
1000
1107
|
break;
|
|
1001
1108
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1109
|
+
// 使用内存存储
|
|
1110
|
+
memStore = this.getMemoryStore(storeName);
|
|
1111
|
+
return _context24.abrupt("return", Array.from(memStore.values()));
|
|
1112
|
+
case 3:
|
|
1113
|
+
return _context24.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23() {
|
|
1114
|
+
var results;
|
|
1115
|
+
return _regeneratorRuntime().wrap(function _callee23$(_context23) {
|
|
1116
|
+
while (1) switch (_context23.prev = _context23.next) {
|
|
1117
|
+
case 0:
|
|
1118
|
+
if (_this14.db) {
|
|
1119
|
+
_context23.next = 2;
|
|
1120
|
+
break;
|
|
1121
|
+
}
|
|
1122
|
+
throw new Error('数据库未连接');
|
|
1123
|
+
case 2:
|
|
1124
|
+
_context23.prev = 2;
|
|
1125
|
+
_context23.next = 5;
|
|
1126
|
+
return _this14.db.table(storeName).toArray();
|
|
1127
|
+
case 5:
|
|
1128
|
+
results = _context23.sent;
|
|
1129
|
+
return _context23.abrupt("return", results);
|
|
1130
|
+
case 9:
|
|
1131
|
+
_context23.prev = 9;
|
|
1132
|
+
_context23.t0 = _context23["catch"](2);
|
|
1133
|
+
throw new Error('获取所有数据失败');
|
|
1134
|
+
case 12:
|
|
1135
|
+
case "end":
|
|
1136
|
+
return _context23.stop();
|
|
1013
1137
|
}
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
case 5:
|
|
1018
|
-
return _context12.abrupt("return", new Promise(function (resolve, reject) {
|
|
1019
|
-
if (!_this12.db) {
|
|
1020
|
-
reject(new Error('数据库未连接'));
|
|
1021
|
-
return;
|
|
1022
|
-
}
|
|
1023
|
-
var transaction = _this12.db.transaction(storeName, 'readonly');
|
|
1024
|
-
var store = transaction.objectStore(storeName);
|
|
1025
|
-
var request = store.getAll();
|
|
1026
|
-
request.onsuccess = function () {
|
|
1027
|
-
return resolve(request.result);
|
|
1028
|
-
};
|
|
1029
|
-
request.onerror = function () {
|
|
1030
|
-
return reject(new Error('获取所有数据失败'));
|
|
1031
|
-
};
|
|
1032
|
-
}));
|
|
1033
|
-
case 6:
|
|
1138
|
+
}, _callee23, null, [[2, 9]]);
|
|
1139
|
+
}))(), "getAll(".concat(storeName, ")")));
|
|
1140
|
+
case 4:
|
|
1034
1141
|
case "end":
|
|
1035
|
-
return
|
|
1142
|
+
return _context24.stop();
|
|
1036
1143
|
}
|
|
1037
|
-
},
|
|
1144
|
+
}, _callee24, this);
|
|
1038
1145
|
}));
|
|
1039
1146
|
function getAll(_x24) {
|
|
1040
1147
|
return _getAll.apply(this, arguments);
|
|
@@ -1050,49 +1157,51 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1050
1157
|
}, {
|
|
1051
1158
|
key: "clear",
|
|
1052
1159
|
value: (function () {
|
|
1053
|
-
var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
1054
|
-
var
|
|
1055
|
-
var
|
|
1056
|
-
return _regeneratorRuntime().wrap(function
|
|
1057
|
-
while (1) switch (
|
|
1160
|
+
var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee26(storeName) {
|
|
1161
|
+
var _this15 = this;
|
|
1162
|
+
var memStore;
|
|
1163
|
+
return _regeneratorRuntime().wrap(function _callee26$(_context26) {
|
|
1164
|
+
while (1) switch (_context26.prev = _context26.next) {
|
|
1058
1165
|
case 0:
|
|
1059
1166
|
if (this.useIndexDB) {
|
|
1060
|
-
|
|
1167
|
+
_context26.next = 4;
|
|
1061
1168
|
break;
|
|
1062
1169
|
}
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1170
|
+
// 使用内存存储
|
|
1171
|
+
memStore = this.getMemoryStore(storeName);
|
|
1172
|
+
memStore.clear();
|
|
1173
|
+
return _context26.abrupt("return", true);
|
|
1174
|
+
case 4:
|
|
1175
|
+
return _context26.abrupt("return", this.withTimeout(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee25() {
|
|
1176
|
+
return _regeneratorRuntime().wrap(function _callee25$(_context25) {
|
|
1177
|
+
while (1) switch (_context25.prev = _context25.next) {
|
|
1178
|
+
case 0:
|
|
1179
|
+
if (_this15.db) {
|
|
1180
|
+
_context25.next = 2;
|
|
1181
|
+
break;
|
|
1182
|
+
}
|
|
1183
|
+
throw new Error('数据库未连接');
|
|
1184
|
+
case 2:
|
|
1185
|
+
_context25.prev = 2;
|
|
1186
|
+
_context25.next = 5;
|
|
1187
|
+
return _this15.db.table(storeName).clear();
|
|
1188
|
+
case 5:
|
|
1189
|
+
return _context25.abrupt("return", true);
|
|
1190
|
+
case 8:
|
|
1191
|
+
_context25.prev = 8;
|
|
1192
|
+
_context25.t0 = _context25["catch"](2);
|
|
1193
|
+
throw new Error('清空数据失败');
|
|
1194
|
+
case 11:
|
|
1195
|
+
case "end":
|
|
1196
|
+
return _context25.stop();
|
|
1197
|
+
}
|
|
1198
|
+
}, _callee25, null, [[2, 8]]);
|
|
1199
|
+
}))(), "clear(".concat(storeName, ")")));
|
|
1200
|
+
case 5:
|
|
1092
1201
|
case "end":
|
|
1093
|
-
return
|
|
1202
|
+
return _context26.stop();
|
|
1094
1203
|
}
|
|
1095
|
-
},
|
|
1204
|
+
}, _callee26, this);
|
|
1096
1205
|
}));
|
|
1097
1206
|
function clear(_x25) {
|
|
1098
1207
|
return _clear.apply(this, arguments);
|
|
@@ -1115,12 +1224,12 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1115
1224
|
|
|
1116
1225
|
/**
|
|
1117
1226
|
* 获取当前使用的存储方式
|
|
1118
|
-
* @returns {'indexDB'|'
|
|
1227
|
+
* @returns {'indexDB'|'memory'} 当前使用的存储方式
|
|
1119
1228
|
*/
|
|
1120
1229
|
}, {
|
|
1121
1230
|
key: "getCurrentStorage",
|
|
1122
1231
|
value: function getCurrentStorage() {
|
|
1123
|
-
return this.useIndexDB ? 'indexDB' : '
|
|
1232
|
+
return this.useIndexDB ? 'indexDB' : 'memory';
|
|
1124
1233
|
}
|
|
1125
1234
|
}], [{
|
|
1126
1235
|
key: "isSupported",
|
|
@@ -1131,7 +1240,7 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1131
1240
|
* @private
|
|
1132
1241
|
*/
|
|
1133
1242
|
function isSupported() {
|
|
1134
|
-
return window && 'indexedDB' in window;
|
|
1243
|
+
return typeof window !== 'undefined' && 'indexedDB' in window;
|
|
1135
1244
|
}
|
|
1136
1245
|
}]);
|
|
1137
1246
|
return IndexDBManager;
|
|
@@ -1203,12 +1312,18 @@ var IndexDBManager = /*#__PURE__*/function () {
|
|
|
1203
1312
|
* db.close();
|
|
1204
1313
|
*
|
|
1205
1314
|
* // 15. 检查当前使用的存储方式
|
|
1206
|
-
* const storageType = db.getCurrentStorage(); // 'indexDB' 或 '
|
|
1315
|
+
* const storageType = db.getCurrentStorage(); // 'indexDB' 或 'memory'
|
|
1207
1316
|
*
|
|
1208
1317
|
* // 性能优化建议:
|
|
1209
1318
|
* // - 使用 exists() 而不是 get() 来检查数据是否存在
|
|
1210
1319
|
* // - 为常用查询字段创建索引
|
|
1211
1320
|
* // - 使用索引查询方法来提高查询效率
|
|
1212
1321
|
* // - 批量操作时使用事务(可以考虑后续扩展)
|
|
1322
|
+
*
|
|
1323
|
+
* // 降级方案说明:
|
|
1324
|
+
* // - 当浏览器不支持 IndexedDB 时,会自动使用内存存储 (Map) 作为降级方案
|
|
1325
|
+
* // - 内存存储的数据在页面刷新后会丢失,仅适合临时数据存储
|
|
1326
|
+
* // - 内存存储支持所有 IndexedDB API,包括索引查询
|
|
1327
|
+
* // - 对于需要持久化的数据,请确保浏览器支持 IndexedDB
|
|
1213
1328
|
*/
|
|
1214
1329
|
export default IndexDBManager;
|