browser-localstorge 1.0.0-beta.2 → 1.0.0
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 +9 -4
- package/dist/es/Browser_StoreKit.es.js +238 -232
- package/dist/umd/Browser_StoreKit.umd.js +1 -1
- package/package.json +1 -1
- package/src/db/handle.js +32 -15
- package/src/index.js +0 -6
package/README.md
CHANGED
|
@@ -234,6 +234,10 @@ DBKit.length().then(r => {
|
|
|
234
234
|
|
|
235
235
|
- `options`:`Object`类型:批量操作的配置项。
|
|
236
236
|
|
|
237
|
+
`options`目前只包含一个属性:
|
|
238
|
+
|
|
239
|
+
- `filter`:`(item) => {}`回调函数类型,其中item就是每一个数据项的属性值。可以传入`filter`属性,自定义过滤元素。
|
|
240
|
+
|
|
237
241
|
### 批量新增操作 `ADD`
|
|
238
242
|
|
|
239
243
|
```javascript
|
|
@@ -275,15 +279,16 @@ DBKit.batchProcess("get", [1, 2, 3, 4, 5]).then(r => {
|
|
|
275
279
|
})
|
|
276
280
|
```
|
|
277
281
|
|
|
282
|
+
当我们给第二个参数`itemList`传入一个空数组`[]`的时候,此时会获取数据表中的所有数据。配合`options`的`filter`属性,我们可以实现一些复杂的自定义的查询操作。
|
|
283
|
+
|
|
278
284
|
# 写在最后💻
|
|
279
285
|
|
|
280
286
|
如果觉得这个库对你有帮助,欢迎给我一个star⭐️⭐️⭐️
|
|
281
|
-
|
|
287
|
+
如果有什么问题,欢迎交流。
|
|
282
288
|
|
|
283
|
-
|
|
289
|
+
源码地址:https://gitee.com/AuroraO23/browser-local-storage
|
|
284
290
|
|
|
285
291
|
# 版本更新记录📋
|
|
286
292
|
|
|
287
293
|
## 1.0.0 (2025-05)
|
|
288
|
-
|
|
289
|
-
1.0.0 首个正式版上线
|
|
294
|
+
1.0.0 首次发布
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
1
|
+
var X = Object.defineProperty;
|
|
2
|
+
var Y = (t) => {
|
|
3
3
|
throw TypeError(t);
|
|
4
4
|
};
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var w = (t, e, r) => (
|
|
5
|
+
var z = (t, e, r) => e in t ? X(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
6
|
+
var I = (t, e, r) => z(t, typeof e != "symbol" ? e + "" : e, r), W = (t, e, r) => e.has(t) || Y("Cannot " + r);
|
|
7
|
+
var x = (t, e, r) => e.has(t) ? Y("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, r);
|
|
8
|
+
var w = (t, e, r) => (W(t, e, "access private method"), r);
|
|
9
9
|
function i(t) {
|
|
10
10
|
return t != null;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function Z(t, e) {
|
|
13
13
|
return i(t) ? t : e;
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function j(t) {
|
|
16
16
|
try {
|
|
17
17
|
const e = JSON.parse(t);
|
|
18
18
|
return e && typeof e == "object" && !Array.isArray(e);
|
|
@@ -20,13 +20,13 @@ function M(t) {
|
|
|
20
20
|
return !1;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
function
|
|
23
|
+
function ee() {
|
|
24
24
|
return i(window.localStorage);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function te() {
|
|
27
27
|
return i(window.sessionStorage);
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function re() {
|
|
30
30
|
return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
|
31
31
|
}
|
|
32
32
|
const T = {
|
|
@@ -37,11 +37,11 @@ const T = {
|
|
|
37
37
|
isFunction: (t) => i(t) && typeof t == "function",
|
|
38
38
|
isObject: (t) => Object.prototype.toString.call(t) === "[object Object]"
|
|
39
39
|
};
|
|
40
|
-
function
|
|
41
|
-
const r = t.getFullYear(),
|
|
42
|
-
return e.replace("YYYY", r).replace("MM",
|
|
40
|
+
function F(t = /* @__PURE__ */ new Date(), e = "YYYY-MM-DD HH:mm:ss") {
|
|
41
|
+
const r = t.getFullYear(), a = String(t.getMonth() + 1).padStart(2, "0"), s = String(t.getDate()).padStart(2, "0"), n = String(t.getHours()).padStart(2, "0"), o = String(t.getMinutes()).padStart(2, "0"), m = String(t.getSeconds()).padStart(2, "0");
|
|
42
|
+
return e.replace("YYYY", r).replace("MM", a).replace("DD", s).replace("HH", n).replace("mm", o).replace("ss", m);
|
|
43
43
|
}
|
|
44
|
-
const
|
|
44
|
+
const k = "localStorage", v = "sessionStorage", R = "indexedDB", p = {
|
|
45
45
|
success: "#67c23a",
|
|
46
46
|
warning: "#e6a23c"
|
|
47
47
|
}, c = {
|
|
@@ -49,13 +49,13 @@ const R = "localStorage", U = "sessionStorage", A = "indexedDB", Y = {
|
|
|
49
49
|
throw new Error(t);
|
|
50
50
|
},
|
|
51
51
|
warn: (t) => {
|
|
52
|
-
console.log(`%c[${
|
|
52
|
+
console.log(`%c[${F()}] ` + t, `color: ${p.warning};`);
|
|
53
53
|
},
|
|
54
54
|
info: (t) => {
|
|
55
55
|
console.log(t);
|
|
56
56
|
},
|
|
57
57
|
success: (t) => {
|
|
58
|
-
console.log(`%c[${
|
|
58
|
+
console.log(`%c[${F()}] ` + t, `color: ${p.success};`);
|
|
59
59
|
}
|
|
60
60
|
}, u = /* @__PURE__ */ new Map();
|
|
61
61
|
u.set(1, "缺少参数key");
|
|
@@ -78,33 +78,33 @@ S.set(5, "参数value类型有误,必须为object类型");
|
|
|
78
78
|
S.set(6, "缺少参数mode(可选值: 'get'、'add'、'delete')");
|
|
79
79
|
S.set(7, "缺少参数itemList(批量操作数据集合)");
|
|
80
80
|
S.set(8, "参数mode值有误(可选值: 'get'、'add'、'delete')");
|
|
81
|
-
const
|
|
82
|
-
(t, ...e) => `🔗${
|
|
83
|
-
(t, ...e) => `🔗${
|
|
84
|
-
(t, ...e) => `🔗${
|
|
81
|
+
const $ = [
|
|
82
|
+
(t, ...e) => `🔗${k} 📎[${t}]: ${e.join("➡️")}`,
|
|
83
|
+
(t, ...e) => `🔗${v} 📎[${t}]: ${e.join("➡️")}`,
|
|
84
|
+
(t, ...e) => `🔗${R} 📢[${t}]: ${e.join("➡️")}`
|
|
85
85
|
];
|
|
86
|
-
function
|
|
86
|
+
function L(t) {
|
|
87
87
|
return `(key: ${t})`;
|
|
88
88
|
}
|
|
89
|
-
function
|
|
90
|
-
return `值为${
|
|
89
|
+
function M(t) {
|
|
90
|
+
return `值为${j(t) ? t : JSON.stringify(t)}`;
|
|
91
91
|
}
|
|
92
|
-
const
|
|
92
|
+
const _ = $[0], se = {
|
|
93
93
|
auto_stringify: !1,
|
|
94
94
|
auto_cover: !0,
|
|
95
95
|
auto_parse: !1
|
|
96
96
|
};
|
|
97
|
-
class
|
|
97
|
+
class H {
|
|
98
98
|
constructor(e) {
|
|
99
99
|
/**
|
|
100
100
|
* @type {Storage}
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
I(this, "_module", null);
|
|
103
103
|
/**
|
|
104
104
|
* @type {import('../index.js').LocalStorgeOptions | null}
|
|
105
105
|
*/
|
|
106
|
-
|
|
107
|
-
this.options = e ||
|
|
106
|
+
I(this, "options", null);
|
|
107
|
+
this.options = e || se;
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
110
|
* 读取项
|
|
@@ -112,9 +112,9 @@ class K {
|
|
|
112
112
|
* @returns {any} 值
|
|
113
113
|
*/
|
|
114
114
|
getItem(e) {
|
|
115
|
-
i(e) || c.error(
|
|
115
|
+
i(e) || c.error(_("getItem", u.get(1))), T.isString(e) || c.error(_("getItem", u.get(2)));
|
|
116
116
|
let r = this._module.getItem(e);
|
|
117
|
-
return i(r) && this.options.debug && c.success(
|
|
117
|
+
return i(r) && this.options.debug && c.success(_("getItem", u.get(4), L(e), M(r))), this.options.auto_parse && i(r) && j(r) && (r = JSON.parse(r)), r;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
120
|
* 增加项
|
|
@@ -123,12 +123,12 @@ class K {
|
|
|
123
123
|
* @returns {boolean} 操作结果
|
|
124
124
|
*/
|
|
125
125
|
setItem(e, r) {
|
|
126
|
-
(!i(e) || T.isEmptyString(e)) && c.error(
|
|
127
|
-
const
|
|
128
|
-
if (!this.options.auto_cover && i(
|
|
129
|
-
return c.warn(
|
|
126
|
+
(!i(e) || T.isEmptyString(e)) && c.error(_("setItem", u.get(1))), T.isString(e) || c.error(_("setItem", u.get(2))), i(r) || c.error(_("setItem", u.get(3)));
|
|
127
|
+
const a = this._module.getItem(e);
|
|
128
|
+
if (!this.options.auto_cover && i(a))
|
|
129
|
+
return c.warn(_("setItem", u.get(5), L(e), M(a))), !1;
|
|
130
130
|
let s = r;
|
|
131
|
-
return this.options.auto_stringify && !
|
|
131
|
+
return this.options.auto_stringify && !j(r) && (s = JSON.stringify(r)), this._module.setItem(e, s), i(this._module.getItem(e)) ? (this.options.debug && c.success(_("setItem", u.get(i(a) ? 7 : 6), L(e), M(s))), !0) : !1;
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* 移除项
|
|
@@ -136,16 +136,16 @@ class K {
|
|
|
136
136
|
* @returns {boolean} 操作结果
|
|
137
137
|
*/
|
|
138
138
|
removeItem(e) {
|
|
139
|
-
i(e) || c.error(
|
|
139
|
+
i(e) || c.error(_("removeItem", u.get(1))), T.isString(e) || c.error(_("removeItem", u.get(2)));
|
|
140
140
|
const r = this._module.getItem(e);
|
|
141
|
-
return i(r) || c.warn(
|
|
141
|
+
return i(r) || c.warn(_("removeItem", u.get(8))), this._module.removeItem(e), i(this._module.getItem(e)) ? !1 : (i(r) && this.options.debug && c.success(_("removeItem", u.get(9))), !0);
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
144
|
* 获取长度
|
|
145
145
|
* @returns {number} 长度
|
|
146
146
|
*/
|
|
147
147
|
length() {
|
|
148
|
-
return c.success(
|
|
148
|
+
return c.success(_("length", u.get(11))), this._module.length;
|
|
149
149
|
}
|
|
150
150
|
/**
|
|
151
151
|
* 清空
|
|
@@ -154,20 +154,20 @@ class K {
|
|
|
154
154
|
clear() {
|
|
155
155
|
this._module.clear();
|
|
156
156
|
let e = this.length();
|
|
157
|
-
return i(e) && e === 0 ? (this.options.debug && c.success(
|
|
157
|
+
return i(e) && e === 0 ? (this.options.debug && c.success(_("clear", u.get(10))), !0) : !1;
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
class
|
|
160
|
+
class ne extends H {
|
|
161
161
|
constructor(e) {
|
|
162
162
|
super(e), this._module = window.localStorage;
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
-
class
|
|
165
|
+
class ae extends H {
|
|
166
166
|
constructor(e) {
|
|
167
167
|
super(e), this._module = window.sessionStorage;
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
|
-
class
|
|
170
|
+
class oe {
|
|
171
171
|
/**
|
|
172
172
|
* 规范化的处理返回结果
|
|
173
173
|
* @param {string} type 存储模块类型
|
|
@@ -178,62 +178,63 @@ class ne {
|
|
|
178
178
|
* 类型
|
|
179
179
|
* @type {LOCAL_STORAGE | SESSION_STORAGE | INDEXED_DB}
|
|
180
180
|
*/
|
|
181
|
-
|
|
181
|
+
I(this, "type", null);
|
|
182
182
|
/**
|
|
183
183
|
* 操作方法
|
|
184
184
|
* @type {string}
|
|
185
185
|
*/
|
|
186
|
-
|
|
186
|
+
I(this, "method", null);
|
|
187
187
|
/**
|
|
188
188
|
* 状态码
|
|
189
189
|
* @type {number}
|
|
190
190
|
*/
|
|
191
|
-
|
|
191
|
+
I(this, "code", null);
|
|
192
192
|
/**
|
|
193
193
|
* 结果状态
|
|
194
194
|
* @type {'success' | 'error' | ''}
|
|
195
195
|
*/
|
|
196
|
-
|
|
196
|
+
I(this, "status", "");
|
|
197
197
|
/**
|
|
198
198
|
* 提示消息
|
|
199
199
|
* @type {string}
|
|
200
200
|
*/
|
|
201
|
-
|
|
201
|
+
I(this, "message", "");
|
|
202
202
|
/**
|
|
203
203
|
* 结果数据值
|
|
204
204
|
* @type {Object}
|
|
205
205
|
*/
|
|
206
|
-
|
|
206
|
+
I(this, "data", null);
|
|
207
207
|
this.type = e;
|
|
208
|
-
const { code:
|
|
209
|
-
this.code =
|
|
208
|
+
const { code: a, data: s, method: n } = r;
|
|
209
|
+
this.code = a, this.method = n, this.status = a === 200 ? "success" : "error", this.data = s, this.message = ce(e, a);
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
function
|
|
212
|
+
function ce(t, e) {
|
|
213
213
|
let r = "";
|
|
214
214
|
switch (t) {
|
|
215
|
-
case
|
|
215
|
+
case k:
|
|
216
216
|
r = e === 200 ? "操作成功" : "操作失败";
|
|
217
217
|
break;
|
|
218
|
-
case
|
|
218
|
+
case v:
|
|
219
219
|
r = e === 200 ? "操作成功" : "操作失败";
|
|
220
220
|
break;
|
|
221
|
-
case
|
|
221
|
+
case R:
|
|
222
222
|
r = e === 200 ? "操作成功" : "操作失败";
|
|
223
223
|
break;
|
|
224
224
|
}
|
|
225
225
|
return r;
|
|
226
226
|
}
|
|
227
227
|
function f(t, e, r) {
|
|
228
|
-
return new
|
|
228
|
+
return new oe(R, { method: t, code: e, data: r });
|
|
229
229
|
}
|
|
230
|
-
const b =
|
|
230
|
+
const b = $[2], K = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB, l = {
|
|
231
231
|
DB_OPEN_SUCCESS: "数据库打开成功",
|
|
232
232
|
DB_OPEN_FAIL: "数据库打开失败",
|
|
233
233
|
DB_CREATE_SUCCESS: "数据库创建成功",
|
|
234
234
|
DB_CREATE_FAIL: "数据库创建失败",
|
|
235
235
|
DB_VERSION_LEVELUP: "数据库版本升级",
|
|
236
236
|
TB_CREATE_SUCCESS: "数据表创建成功",
|
|
237
|
+
TB_QUERY_ERROR: "数据表查询出错",
|
|
237
238
|
TB_EXITS: "数据表已存在,无需创建",
|
|
238
239
|
TB_LACK_PRIMARY: "数据表缺少主键",
|
|
239
240
|
TB_CLEAR_FAIL: "数据表清空失败",
|
|
@@ -250,241 +251,251 @@ const b = L[2], F = window.indexedDB || window.mozIndexedDB || window.webkitInde
|
|
|
250
251
|
DI_BATCH_DELETE_SUCCESS: "批量数据删除成功",
|
|
251
252
|
DI_BATCH_ADD_SUCCESS: "批量数据添加成功"
|
|
252
253
|
};
|
|
253
|
-
function
|
|
254
|
+
function ie(t) {
|
|
254
255
|
return `📦${t}`;
|
|
255
256
|
}
|
|
256
|
-
function
|
|
257
|
+
function y(t) {
|
|
257
258
|
return `📊${t}`;
|
|
258
259
|
}
|
|
259
|
-
function
|
|
260
|
+
function A(t, e, r) {
|
|
260
261
|
return `数据项(key:${t}): ${r}` + (e ? `; 值为: ${e}` : "");
|
|
261
262
|
}
|
|
262
|
-
function
|
|
263
|
-
const { debug:
|
|
264
|
-
return new Promise((
|
|
263
|
+
function V(t, e, r) {
|
|
264
|
+
const { debug: a, version: s } = r, n = ie(t);
|
|
265
|
+
return new Promise((o, m) => {
|
|
265
266
|
i(t) && i(e) || c.error(b("init", S.get(1)));
|
|
266
267
|
let d = null;
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
},
|
|
271
|
-
const { newVersion:
|
|
272
|
-
d =
|
|
273
|
-
},
|
|
274
|
-
|
|
268
|
+
const h = s ? K.open(t, s) : K.open(t);
|
|
269
|
+
h.onsuccess = () => {
|
|
270
|
+
a && c.success(b("init", n, l.DB_OPEN_SUCCESS)), d = h.result, o(d);
|
|
271
|
+
}, h.onupgradeneeded = (g) => {
|
|
272
|
+
const { newVersion: B, oldVersion: O } = g;
|
|
273
|
+
d = g.target.result, i(d) || c.error(b("init", n, l.DB_CREATE_FAIL)), O && B && B > O ? a && c.success(b("init", n, `${l.DB_VERSION_LEVELUP}(${O} ➡️ ${B})`)) : a && c.success(b("init", n, l.DB_CREATE_SUCCESS)), Q(d, e) || le(d, r);
|
|
274
|
+
}, h.onerror = (g) => {
|
|
275
|
+
g.target.error ? m(g.target.error) : m(new Error(b("init", n, l.DB_OPEN_FAIL)));
|
|
275
276
|
};
|
|
276
277
|
});
|
|
277
278
|
}
|
|
278
|
-
function
|
|
279
|
-
return new Promise(async (r,
|
|
280
|
-
const { databaseName: s, tableName:
|
|
281
|
-
if (
|
|
282
|
-
e.debug && c.success(b("init",
|
|
279
|
+
function ue(t, e) {
|
|
280
|
+
return new Promise(async (r, a) => {
|
|
281
|
+
const { databaseName: s, tableName: n } = e;
|
|
282
|
+
if (Q(t, n))
|
|
283
|
+
e.debug && c.success(b("init", y(n), l.TB_EXITS)), r(t);
|
|
283
284
|
else {
|
|
284
|
-
const
|
|
285
|
-
return t.close(),
|
|
285
|
+
const o = t.version + 1;
|
|
286
|
+
return t.close(), V(s, n, {
|
|
286
287
|
...e,
|
|
287
|
-
version:
|
|
288
|
+
version: o
|
|
288
289
|
});
|
|
289
290
|
}
|
|
290
291
|
});
|
|
291
292
|
}
|
|
292
|
-
function
|
|
293
|
+
function Q(t, e) {
|
|
293
294
|
return t.objectStoreNames.contains(e);
|
|
294
295
|
}
|
|
295
|
-
function
|
|
296
|
+
function le(t, e) {
|
|
296
297
|
console.log(e);
|
|
297
|
-
const { tableName: r, primaryKey:
|
|
298
|
-
i(
|
|
298
|
+
const { tableName: r, primaryKey: a, autoIncrement: s } = e;
|
|
299
|
+
i(a) || c.error(b("init", y(r), l.TB_LACK_PRIMARY)), t.createObjectStore(r, { keyPath: a, autoIncrement: s }), !e.noMessage && e.debug && c.success(b("init", y(r), l.TB_CREATE_SUCCESS));
|
|
299
300
|
}
|
|
300
|
-
function
|
|
301
|
-
return new Promise(async (r,
|
|
301
|
+
function ge(t, e) {
|
|
302
|
+
return new Promise(async (r, a) => {
|
|
302
303
|
let s = null;
|
|
303
304
|
if (e.autoIncrement)
|
|
304
305
|
s = N(t, e);
|
|
305
306
|
else {
|
|
306
|
-
const
|
|
307
|
+
const o = await U(t, { ...e, noMessage: !0 }), m = o.code === 200 && i(o.data);
|
|
307
308
|
if (e.auto_cover)
|
|
308
|
-
s =
|
|
309
|
+
s = m ? me(t, e) : N(t, e);
|
|
309
310
|
else {
|
|
310
|
-
if (
|
|
311
|
-
return c.warn(b("setItem",
|
|
311
|
+
if (m)
|
|
312
|
+
return c.warn(b("setItem", l.DI_EXITS, outputDataValue(o.data))), r(f("setItem", 0, !1)), null;
|
|
312
313
|
s = N(t, e);
|
|
313
314
|
}
|
|
314
315
|
}
|
|
315
|
-
const
|
|
316
|
-
r(
|
|
316
|
+
const n = await s;
|
|
317
|
+
r(n);
|
|
317
318
|
});
|
|
318
319
|
}
|
|
319
320
|
function N(t, e) {
|
|
320
|
-
return new Promise((r,
|
|
321
|
+
return new Promise((r, a) => {
|
|
321
322
|
const s = {
|
|
322
323
|
...e.value
|
|
323
324
|
};
|
|
324
325
|
!e.autoIncrement && !i(s[e.primaryKey]) && (s[e.primaryKey] = e.key), e.autoIncrement && i(s[e.primaryKey]) && delete s[e.primaryKey];
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
!e.noMessage && e.debug && c.success(
|
|
328
|
-
},
|
|
329
|
-
console.log(
|
|
326
|
+
const n = t.transaction([e.tableName], "readwrite").objectStore(e.tableName).add(s);
|
|
327
|
+
n.onsuccess = () => {
|
|
328
|
+
!e.noMessage && e.debug && c.success(A(e.key, JSON.stringify(e.value), l.DI_CREATE_SUCCESS)), r(f("addItem", 200, s));
|
|
329
|
+
}, n.onerror = (o) => {
|
|
330
|
+
console.log(o), a(new Error(A(e.key, null, l.DI_CREATE_FAIL)));
|
|
330
331
|
};
|
|
331
332
|
});
|
|
332
333
|
}
|
|
333
|
-
function
|
|
334
|
-
return new Promise((r,
|
|
334
|
+
function me(t, e) {
|
|
335
|
+
return new Promise((r, a) => {
|
|
335
336
|
const s = {
|
|
336
337
|
...e.value
|
|
337
338
|
};
|
|
338
339
|
e.primaryKey && (s[e.primaryKey] = e.key);
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
!e.noMessage && e.debug && c.success(
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
},
|
|
345
|
-
|
|
340
|
+
const n = t.transaction([e.tableName], "readwrite").objectStore(e.tableName).put(s);
|
|
341
|
+
n.onsuccess = async () => {
|
|
342
|
+
!e.noMessage && e.debug && c.success(A(e.key, JSON.stringify(e.value), l.DI_UPDATE_SUCCESS));
|
|
343
|
+
const o = await U(t, { ...e, noMessage: !0 });
|
|
344
|
+
o.code === 200 && i(o.data) ? r(f("setItem", 200, o.data)) : r(f("setItem", 200, null));
|
|
345
|
+
}, n.onerror = (o) => {
|
|
346
|
+
a(new Error(A(e.key, null, l.DI_UPDATE_FAIL)));
|
|
346
347
|
};
|
|
347
348
|
});
|
|
348
349
|
}
|
|
349
|
-
function
|
|
350
|
-
return new Promise((r,
|
|
350
|
+
function U(t, e) {
|
|
351
|
+
return new Promise((r, a) => {
|
|
351
352
|
const s = t.transaction([e.tableName], "readonly").objectStore(e.tableName);
|
|
352
|
-
let
|
|
353
|
-
|
|
354
|
-
const d =
|
|
355
|
-
d ? (!e.noMessage && e.debug && c.success(
|
|
356
|
-
},
|
|
357
|
-
|
|
353
|
+
let n = e.autoIncrement ? Number(e.key) : e.key, o = s.get(n);
|
|
354
|
+
o.onsuccess = (m) => {
|
|
355
|
+
const d = m.target.result;
|
|
356
|
+
d ? (!e.noMessage && e.debug && c.success(A(e.key, JSON.stringify(d), l.DI_QUERY_SUCCESS)), r(f("getItem", 200, d))) : r(f("getItem", 200, null));
|
|
357
|
+
}, o.onerror = (m) => {
|
|
358
|
+
a(new Error(l.DI_QUERY_ERROR));
|
|
358
359
|
};
|
|
359
360
|
});
|
|
360
361
|
}
|
|
361
|
-
function
|
|
362
|
-
return new Promise((r,
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
362
|
+
function de(t, e) {
|
|
363
|
+
return new Promise(async (r, a) => {
|
|
364
|
+
const s = await G(t, e);
|
|
365
|
+
let n = null;
|
|
366
|
+
s.code === 200 && i(s.data) && (n = s.data);
|
|
367
|
+
let m = t.transaction([e.tableName], "readonly").objectStore(e.tableName).getAll();
|
|
368
|
+
m.onsuccess = (d) => {
|
|
369
|
+
const h = d.target.result;
|
|
370
|
+
if (h) {
|
|
371
|
+
!e.noMessage && e.debug && c.success(y(e.tableName));
|
|
372
|
+
const g = _e(h, e.filter), B = {
|
|
373
|
+
...P(n, g.length),
|
|
374
|
+
data: g
|
|
375
|
+
};
|
|
376
|
+
r(f("getAllItem", 200, B));
|
|
377
|
+
} else
|
|
378
|
+
r(f("getAllItem", 200, null));
|
|
379
|
+
}, m.onerror = (d) => {
|
|
380
|
+
a(new Error(l.TB_QUERY_ERROR));
|
|
369
381
|
};
|
|
370
382
|
});
|
|
371
383
|
}
|
|
372
384
|
function q(t, e) {
|
|
373
|
-
return new Promise((r,
|
|
385
|
+
return new Promise((r, a) => {
|
|
374
386
|
const s = t.transaction([e.tableName], "readwrite").objectStore(e.tableName).delete(e.key);
|
|
375
|
-
s.onsuccess = (
|
|
376
|
-
!e.noMessage && e.debug && c.success(
|
|
377
|
-
}, s.onerror = (
|
|
378
|
-
|
|
387
|
+
s.onsuccess = (n) => {
|
|
388
|
+
!e.noMessage && e.debug && c.success(A(e.key, null, l.DI_DELETE_SUCCESS)), r(f("removeItem", 200, !0));
|
|
389
|
+
}, s.onerror = (n) => {
|
|
390
|
+
a(new Error(A(e.key, null, l.DI_DELETE_ERROR)));
|
|
379
391
|
};
|
|
380
392
|
});
|
|
381
393
|
}
|
|
382
|
-
function
|
|
383
|
-
return new Promise((r,
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
!e.noMessage && e.debug && c.success(
|
|
387
|
-
},
|
|
388
|
-
|
|
394
|
+
function G(t, e) {
|
|
395
|
+
return new Promise((r, a) => {
|
|
396
|
+
const n = t.transaction(e.tableName).objectStore(e.tableName).count();
|
|
397
|
+
n.onsuccess = () => {
|
|
398
|
+
!e.noMessage && e.debug && c.success(y(e.tableName)), r(f("length", 200, n.result));
|
|
399
|
+
}, n.onerror = () => {
|
|
400
|
+
a(new Error(y(e.tableName)));
|
|
389
401
|
};
|
|
390
402
|
});
|
|
391
403
|
}
|
|
392
|
-
function
|
|
393
|
-
return new Promise((r,
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
!e.noMessage && e.debug && c.success(
|
|
397
|
-
},
|
|
398
|
-
|
|
404
|
+
function Se(t, e) {
|
|
405
|
+
return new Promise((r, a) => {
|
|
406
|
+
const n = t.transaction([e.tableName], "readwrite").objectStore(e.tableName).clear();
|
|
407
|
+
n.onsuccess = (o) => {
|
|
408
|
+
!e.noMessage && e.debug && c.success(y(e.tableName)), r(f("clear", 200, !0));
|
|
409
|
+
}, n.onerror = (o) => {
|
|
410
|
+
a(new Error(l.TB_CLEAR_FAIL));
|
|
399
411
|
};
|
|
400
412
|
});
|
|
401
413
|
}
|
|
402
|
-
function
|
|
403
|
-
return new Promise((
|
|
404
|
-
let
|
|
405
|
-
Promise.allSettled(
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
success: l,
|
|
409
|
-
fail: e.length - l,
|
|
414
|
+
function fe(t, e, r) {
|
|
415
|
+
return new Promise((a, s) => {
|
|
416
|
+
let n = e.map((o) => N(t, { ...r, key: Ee(o, r.primaryKey), value: o, noMessage: !0 }));
|
|
417
|
+
Promise.allSettled(n).then((o) => {
|
|
418
|
+
const m = o.filter((g) => g.status === "fulfilled" && g.value.code === 200 && i(g.value.data)).length, d = o.map((g) => g.value.data), h = {
|
|
419
|
+
...P(e.length, m),
|
|
410
420
|
data: d
|
|
411
421
|
};
|
|
412
|
-
r.debug && c.success(
|
|
413
|
-
}).catch((
|
|
414
|
-
s(
|
|
422
|
+
r.debug && c.success(l.DI_BATCH_ADD_SUCCESS), a(f("batchSetItem", 200, h));
|
|
423
|
+
}).catch((o) => {
|
|
424
|
+
s(o);
|
|
415
425
|
});
|
|
416
426
|
});
|
|
417
427
|
}
|
|
418
|
-
function
|
|
419
|
-
return new Promise((
|
|
428
|
+
function he(t, e, r) {
|
|
429
|
+
return new Promise((a, s) => {
|
|
420
430
|
if (e.length === 0)
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
s(a);
|
|
431
|
+
de(t, { ...r, noMessage: !0 }).then((n) => {
|
|
432
|
+
n.code === 200 && i(n.data) ? (r.debug && c.success(l.DI_BATCH_QUERY_SUCCESS), a(f("batchGetItem", 200, n.data))) : a(f("batchGetItem", 200, null));
|
|
433
|
+
}).catch((n) => {
|
|
434
|
+
s(n);
|
|
426
435
|
});
|
|
427
436
|
else {
|
|
428
|
-
let
|
|
429
|
-
Promise.allSettled(
|
|
430
|
-
let
|
|
431
|
-
const d =
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
process: { success: d, fail: e.length - d },
|
|
435
|
-
// 成功数、失败数
|
|
436
|
-
data: l
|
|
437
|
+
let n = e.filter((o) => i(o)).map((o) => U(t, { ...r, key: o, noMessage: !0 }));
|
|
438
|
+
Promise.allSettled(n).then((o) => {
|
|
439
|
+
let m = o.filter((g) => g.status === "fulfilled" && g.value.code === 200 && i(g.value.data)).filter((g) => r.filter ? r.filter(g.value.data) : !0).map((g) => g.value.data);
|
|
440
|
+
const d = m.length, h = {
|
|
441
|
+
...P(e.length, d),
|
|
442
|
+
data: m
|
|
437
443
|
};
|
|
438
|
-
r.debug && c.success(
|
|
439
|
-
}).catch((
|
|
440
|
-
s(
|
|
444
|
+
r.debug && c.success(l.DI_BATCH_QUERY_SUCCESS), a(f("batchGetItem", 200, h));
|
|
445
|
+
}).catch((o) => {
|
|
446
|
+
s(o);
|
|
441
447
|
});
|
|
442
448
|
}
|
|
443
449
|
});
|
|
444
450
|
}
|
|
445
|
-
function
|
|
446
|
-
return new Promise((
|
|
447
|
-
let
|
|
448
|
-
Promise.allSettled(
|
|
449
|
-
const
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
fail: e.length - l,
|
|
453
|
-
data: l === e.length
|
|
451
|
+
function Ie(t, e, r) {
|
|
452
|
+
return new Promise((a, s) => {
|
|
453
|
+
let n = e.filter((o) => i(o)).map((o) => q(t, { ...r, key: o, noMessage: !0 }));
|
|
454
|
+
Promise.allSettled(n).then((o) => {
|
|
455
|
+
const m = o.filter((h) => h.status === "fulfilled" && h.value.code === 200 && i(h.value.data)).length, d = {
|
|
456
|
+
...P(e.length, m),
|
|
457
|
+
data: m === e.length
|
|
454
458
|
};
|
|
455
|
-
r.debug && c.success(
|
|
456
|
-
}).catch((
|
|
457
|
-
s(
|
|
459
|
+
r.debug && c.success(l.DI_BATCH_DELETE_SUCCESS), a(f("batchRemoveItem", 200, d));
|
|
460
|
+
}).catch((o) => {
|
|
461
|
+
s(o);
|
|
458
462
|
});
|
|
459
463
|
});
|
|
460
464
|
}
|
|
461
|
-
function
|
|
465
|
+
function _e(t, e) {
|
|
462
466
|
return t.filter((r) => e ? e(r) : !0);
|
|
463
467
|
}
|
|
464
|
-
function
|
|
465
|
-
return
|
|
468
|
+
function Ee(t, e) {
|
|
469
|
+
return Z(t[e], null);
|
|
470
|
+
}
|
|
471
|
+
function P(t, e) {
|
|
472
|
+
return {
|
|
473
|
+
total: t,
|
|
474
|
+
success: e === t,
|
|
475
|
+
process: { success: e, fail: t - e }
|
|
476
|
+
};
|
|
466
477
|
}
|
|
467
|
-
const D =
|
|
478
|
+
const D = $[2], be = {
|
|
468
479
|
auto_cover: !1
|
|
469
480
|
};
|
|
470
|
-
class
|
|
481
|
+
class De {
|
|
471
482
|
constructor(e = {}) {
|
|
472
483
|
/**
|
|
473
484
|
* indexedDB配置项,参考IndexedDBOptios
|
|
474
485
|
* @type {import('../index.js').IndexedDBOptions}
|
|
475
486
|
*/
|
|
476
|
-
|
|
487
|
+
I(this, "options", null);
|
|
477
488
|
/**
|
|
478
489
|
* 数据库实例
|
|
479
490
|
* @type {IDBDatabase}
|
|
480
491
|
*/
|
|
481
|
-
|
|
492
|
+
I(this, "db", null);
|
|
482
493
|
/**
|
|
483
494
|
* 数据库版本号
|
|
484
495
|
* @type {number}
|
|
485
496
|
*/
|
|
486
|
-
|
|
487
|
-
this.options = Object.assign(
|
|
497
|
+
I(this, "version", null);
|
|
498
|
+
this.options = Object.assign(be, e);
|
|
488
499
|
}
|
|
489
500
|
/**
|
|
490
501
|
* 初始化数据库
|
|
@@ -492,9 +503,9 @@ class be {
|
|
|
492
503
|
*/
|
|
493
504
|
async init() {
|
|
494
505
|
const { databaseName: e, tableName: r } = this.options;
|
|
495
|
-
this.db = await
|
|
496
|
-
const
|
|
497
|
-
return this.db =
|
|
506
|
+
this.db = await V(e, r, this.options);
|
|
507
|
+
const a = await ue(this.db, this.options);
|
|
508
|
+
return this.db = a, this.version = this.db.version, this.options.autoIncrement = this.db.transaction([r], "readonly").objectStore(r).autoIncrement, this.db;
|
|
498
509
|
}
|
|
499
510
|
/**
|
|
500
511
|
* 获取数据✔️
|
|
@@ -502,7 +513,7 @@ class be {
|
|
|
502
513
|
* @returns {Promise}
|
|
503
514
|
*/
|
|
504
515
|
getItem(e) {
|
|
505
|
-
return i(e) || c.error(D("getItem", S.get(1))),
|
|
516
|
+
return i(e) || c.error(D("getItem", S.get(1))), U(this.db, { ...this.options, key: e });
|
|
506
517
|
}
|
|
507
518
|
/**
|
|
508
519
|
* 增加、更新数据✔️
|
|
@@ -510,7 +521,7 @@ class be {
|
|
|
510
521
|
* @param {Object} value 值
|
|
511
522
|
*/
|
|
512
523
|
setItem(e, r) {
|
|
513
|
-
return this.options.autoIncrement || i(e) || c.error(D("setItem", S.get(1))), i(r) || c.error(D("setItem", S.get(4))), T.isObject(r) || c.error(D("setItem", S.get(5))),
|
|
524
|
+
return this.options.autoIncrement || i(e) || c.error(D("setItem", S.get(1))), i(r) || c.error(D("setItem", S.get(4))), T.isObject(r) || c.error(D("setItem", S.get(5))), ge(this.db, { ...this.options, key: e, value: r });
|
|
514
525
|
}
|
|
515
526
|
/**
|
|
516
527
|
* 删除数据✔️
|
|
@@ -523,13 +534,13 @@ class be {
|
|
|
523
534
|
* 获取数据表数据长度✔️
|
|
524
535
|
*/
|
|
525
536
|
length() {
|
|
526
|
-
return
|
|
537
|
+
return G(this.db, this.options);
|
|
527
538
|
}
|
|
528
539
|
/**
|
|
529
540
|
* 清空数据表✔️
|
|
530
541
|
*/
|
|
531
542
|
clear() {
|
|
532
|
-
return
|
|
543
|
+
return Se(this.db, this.options);
|
|
533
544
|
}
|
|
534
545
|
/**
|
|
535
546
|
* 批量处理
|
|
@@ -537,49 +548,44 @@ class be {
|
|
|
537
548
|
* @param {import('../index.js').BatchProcessItemList} itemList 待处理的数据列表
|
|
538
549
|
* @param {import('../index.js').BatchProcessOptions} options 配置项
|
|
539
550
|
*/
|
|
540
|
-
batchProcess(e, r,
|
|
551
|
+
batchProcess(e, r, a = {}) {
|
|
541
552
|
i(e) || c.error(D("batchProcess", S.get(6))), i(r) || c.error(D("batchProcess", S.get(7)));
|
|
542
553
|
let s = null;
|
|
543
554
|
switch (e) {
|
|
544
555
|
case "get":
|
|
545
|
-
s =
|
|
556
|
+
s = he;
|
|
546
557
|
break;
|
|
547
558
|
case "add":
|
|
548
|
-
s =
|
|
559
|
+
s = fe;
|
|
549
560
|
break;
|
|
550
561
|
case "delete":
|
|
551
|
-
s =
|
|
562
|
+
s = Ie;
|
|
552
563
|
break;
|
|
553
564
|
}
|
|
554
|
-
return s || c.error(D("batchProcess", S.get(8))), s(this.db, r, { ...this.options, ...
|
|
565
|
+
return s || c.error(D("batchProcess", S.get(8))), s(this.db, r, { ...this.options, ...a });
|
|
555
566
|
}
|
|
556
567
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
SESSION_STORAGE: U,
|
|
560
|
-
INDEXED_DB: A
|
|
561
|
-
};
|
|
562
|
-
var E, G, C;
|
|
563
|
-
class we {
|
|
568
|
+
var E, J, C;
|
|
569
|
+
class Ce {
|
|
564
570
|
/**
|
|
565
571
|
* 构造函数,用于初始化BrowserStoreKit。
|
|
566
572
|
* @param {BrowserStoreKitModeOptions} type 存储模块类型
|
|
567
573
|
* @param {BrowserStoreKitOptions} options 自定义参数
|
|
568
574
|
*/
|
|
569
575
|
constructor(e, r) {
|
|
570
|
-
|
|
576
|
+
x(this, E);
|
|
571
577
|
/**
|
|
572
578
|
* @type {BrowserStoreKitModeOptions} 存储模块类型
|
|
573
579
|
*/
|
|
574
|
-
|
|
580
|
+
I(this, "type", null);
|
|
575
581
|
/**
|
|
576
582
|
* @type {LocalStorage | SessionStorage | IndexedDB}
|
|
577
583
|
*/
|
|
578
|
-
|
|
579
|
-
this.type = e, w(this, E,
|
|
584
|
+
I(this, "kitTool", null);
|
|
585
|
+
this.type = e, w(this, E, J).call(this, r);
|
|
580
586
|
}
|
|
581
587
|
init() {
|
|
582
|
-
return this.type !==
|
|
588
|
+
return this.type !== R ? c.error(`当前模式:${this.type}下无需调用init方法`) : (w(this, E, C).call(this, "init"), this.kitTool.init());
|
|
583
589
|
}
|
|
584
590
|
/**
|
|
585
591
|
* 获取数据
|
|
@@ -629,34 +635,34 @@ class we {
|
|
|
629
635
|
* @param {BatchProcessItemList} itemList 待处理项列表
|
|
630
636
|
* @param {BatchProcessOptions} options 配置参数
|
|
631
637
|
*/
|
|
632
|
-
batchProcess(e, r,
|
|
633
|
-
return this.kitTool.batchProcess(e, r,
|
|
638
|
+
batchProcess(e, r, a) {
|
|
639
|
+
return this.kitTool.batchProcess(e, r, a);
|
|
634
640
|
}
|
|
635
641
|
}
|
|
636
|
-
E = new WeakSet(),
|
|
642
|
+
E = new WeakSet(), J = function(e) {
|
|
637
643
|
switch (this.type) {
|
|
638
|
-
case
|
|
639
|
-
if (!
|
|
644
|
+
case k:
|
|
645
|
+
if (!ee())
|
|
640
646
|
throw Error("抱歉,当前浏览器暂不支持使用localStorage");
|
|
641
|
-
this.kitTool = new
|
|
647
|
+
this.kitTool = new ne(e.localStorage);
|
|
642
648
|
break;
|
|
643
|
-
case
|
|
644
|
-
if (!
|
|
649
|
+
case v:
|
|
650
|
+
if (!te())
|
|
645
651
|
throw Error("抱歉,当前浏览器暂不支持使用sessionStorage");
|
|
646
|
-
this.kitTool = new
|
|
652
|
+
this.kitTool = new ae(e.sessionStorage);
|
|
647
653
|
break;
|
|
648
|
-
case
|
|
649
|
-
if (!
|
|
654
|
+
case R:
|
|
655
|
+
if (!re())
|
|
650
656
|
throw Error("抱歉,当前浏览器暂不支持使用indexedDB");
|
|
651
|
-
this.kitTool = new
|
|
657
|
+
this.kitTool = new De(e.indexedDB);
|
|
652
658
|
break;
|
|
653
659
|
}
|
|
654
660
|
}, C = function(e) {
|
|
655
661
|
this.kitTool || c.error("抱歉,未正确实例化,暂时无法使用" + e + "方法");
|
|
656
662
|
};
|
|
657
663
|
export {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
664
|
+
R as INDEXED_DB,
|
|
665
|
+
k as LOCAL_STORAGE,
|
|
666
|
+
v as SESSION_STORAGE,
|
|
667
|
+
Ce as default
|
|
662
668
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,m){typeof exports=="object"&&typeof module<"u"?module.exports=m():typeof define=="function"&&define.amd?define(m):(s=typeof globalThis<"u"?globalThis:s||self,s.BrowserStoreKit=m())})(this,function(){"use strict";var Ee=Object.defineProperty;var q=s=>{throw TypeError(s)};var be=(s,m,I)=>m in s?Ee(s,m,{enumerable:!0,configurable:!0,writable:!0,value:I}):s[m]=I;var E=(s,m,I)=>be(s,typeof m!="symbol"?m+"":m,I),De=(s,m,I)=>m.has(s)||q("Cannot "+I);var G=(s,m,I)=>m.has(s)?q("Cannot add the same private member more than once"):m instanceof WeakSet?m.add(s):m.set(s,I);var T=(s,m,I)=>(De(s,m,"access private method"),I);var D,J,B;function s(t){return t!=null}function m(t,e){return s(t)?t:e}function I(t){try{const e=JSON.parse(t);return e&&typeof e=="object"&&!Array.isArray(e)}catch{return!1}}function Q(){return s(window.localStorage)}function X(){return s(window.sessionStorage)}function z(){return window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB}const y={isString:t=>s(t)&&typeof t=="string",isEmptyString:t=>s(t)&&typeof t=="string"&&t.trim()==="",isNumber:t=>s(t)&&typeof t=="number"&&!isNaN(t),isBoolean:t=>s(t)&&typeof t=="boolean",isFunction:t=>s(t)&&typeof t=="function",isObject:t=>Object.prototype.toString.call(t)==="[object Object]"};function p(t=new Date,e="YYYY-MM-DD HH:mm:ss"){const r=t.getFullYear(),c=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),a=String(t.getHours()).padStart(2,"0"),o=String(t.getMinutes()).padStart(2,"0"),g=String(t.getSeconds()).padStart(2,"0");return e.replace("YYYY",r).replace("MM",c).replace("DD",n).replace("HH",a).replace("mm",o).replace("ss",g)}const U="localStorage",P="sessionStorage",N="indexedDB",x={success:"#67c23a",warning:"#e6a23c"},i={error:t=>{throw new Error(t)},warn:t=>{console.log(`%c[${p()}] `+t,`color: ${x.warning};`)},info:t=>{console.log(t)},success:t=>{console.log(`%c[${p()}] `+t,`color: ${x.success};`)}},u=new Map;u.set(1,"缺少参数key"),u.set(2,"参数key类型有误,必须为string类型"),u.set(3,"缺少参数value"),u.set(4,"数据项获取成功"),u.set(5,"数据项已存在"),u.set(6,"数据项设置成功"),u.set(7,"数据项更新成功"),u.set(8,"数据项不存在"),u.set(9,"数据项移除成功"),u.set(10,"数据项清除成功"),u.set(11,"数据项长度获取成功");const f=new Map;f.set(1,"缺少参数databaseName(数据库名称)、tableName(数据表名称)"),f.set(2,"缺少参数key"),f.set(3,"参数key类型有误,必须为string 或 number类型"),f.set(4,"缺少参数value"),f.set(5,"参数value类型有误,必须为object类型"),f.set(6,"缺少参数mode(可选值: 'get'、'add'、'delete')"),f.set(7,"缺少参数itemList(批量操作数据集合)"),f.set(8,"参数mode值有误(可选值: 'get'、'add'、'delete')");const k=[(t,...e)=>`🔗${U} 📎[${t}]: ${e.join("➡️")}`,(t,...e)=>`🔗${P} 📎[${t}]: ${e.join("➡️")}`,(t,...e)=>`🔗${N} 📢[${t}]: ${e.join("➡️")}`];function v(t){return`(key: ${t})`}function M(t){return`值为${I(t)?t:JSON.stringify(t)}`}const _=k[0],W={auto_stringify:!1,auto_cover:!0,auto_parse:!1};class Y{constructor(e){E(this,"_module",null);E(this,"options",null);this.options=e||W}getItem(e){s(e)||i.error(_("getItem",u.get(1))),y.isString(e)||i.error(_("getItem",u.get(2)));let r=this._module.getItem(e);return s(r)&&this.options.debug&&i.success(_("getItem",u.get(4),v(e),M(r))),this.options.auto_parse&&s(r)&&I(r)&&(r=JSON.parse(r)),r}setItem(e,r){(!s(e)||y.isEmptyString(e))&&i.error(_("setItem",u.get(1))),y.isString(e)||i.error(_("setItem",u.get(2))),s(r)||i.error(_("setItem",u.get(3)));const c=this._module.getItem(e);if(!this.options.auto_cover&&s(c))return i.warn(_("setItem",u.get(5),v(e),M(c))),!1;let n=r;return this.options.auto_stringify&&!I(r)&&(n=JSON.stringify(r)),this._module.setItem(e,n),s(this._module.getItem(e))?(this.options.debug&&i.success(_("setItem",u.get(s(c)?7:6),v(e),M(n))),!0):!1}removeItem(e){s(e)||i.error(_("removeItem",u.get(1))),y.isString(e)||i.error(_("removeItem",u.get(2)));const r=this._module.getItem(e);return s(r)||i.warn(_("removeItem",u.get(8))),this._module.removeItem(e),s(this._module.getItem(e))?!1:(s(r)&&this.options.debug&&i.success(_("removeItem",u.get(9))),!0)}length(){return i.success(_("length",u.get(11))),this._module.length}clear(){this._module.clear();let e=this.length();return s(e)&&e===0?(this.options.debug&&i.success(_("clear",u.get(10))),!0):!1}}class Z extends Y{constructor(e){super(e),this._module=window.localStorage}}class ee extends Y{constructor(e){super(e),this._module=window.sessionStorage}}class te{constructor(e,r){E(this,"type",null);E(this,"method",null);E(this,"code",null);E(this,"status","");E(this,"message","");E(this,"data",null);this.type=e;const{code:c,data:n,method:a}=r;this.code=c,this.method=a,this.status=c===200?"success":"error",this.data=n,this.message=re(e,c)}}function re(t,e){let r="";switch(t){case U:r=e===200?"操作成功":"操作失败";break;case P:r=e===200?"操作成功":"操作失败";break;case N:r=e===200?"操作成功":"操作失败";break}return r}function h(t,e,r){return new te(N,{method:t,code:e,data:r})}const w=k[2],F=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,l={DB_OPEN_SUCCESS:"数据库打开成功",DB_OPEN_FAIL:"数据库打开失败",DB_CREATE_SUCCESS:"数据库创建成功",DB_CREATE_FAIL:"数据库创建失败",DB_VERSION_LEVELUP:"数据库版本升级",TB_CREATE_SUCCESS:"数据表创建成功",TB_EXITS:"数据表已存在,无需创建",TB_LACK_PRIMARY:"数据表缺少主键",TB_CLEAR_FAIL:"数据表清空失败",DI_CREATE_SUCCESS:"数据项创建成功",DI_CREATE_FAIL:"数据项创建失败",DI_UPDATE_SUCCESS:"数据项创建成功",DI_UPDATE_FAIL:"数据项创建失败",DI_QUERY_SUCCESS:"数据查询成功",DI_QUERY_ERROR:"数据项查询出错",DI_DELETE_SUCCESS:"数据删除成功",DI_DELETE_ERROR:"数据项删除出错",DI_EXITS:"数据项已存在",DI_BATCH_QUERY_SUCCESS:"批量数据查询成功",DI_BATCH_DELETE_SUCCESS:"批量数据删除成功",DI_BATCH_ADD_SUCCESS:"批量数据添加成功"};function se(t){return`📦${t}`}function R(t){return`📊${t}`}function A(t,e,r){return`数据项(key:${t}): ${r}`+(e?`; 值为: ${e}`:"")}function K(t,e,r){const{debug:c,version:n}=r,a=se(t);return new Promise((o,g)=>{s(t)&&s(e)||i.error(w("init",f.get(1)));let S=null;const b=n?F.open(t,n):F.open(t);b.onsuccess=()=>{c&&i.success(w("init",a,l.DB_OPEN_SUCCESS)),S=b.result,o(S)},b.onupgradeneeded=d=>{const{newVersion:L,oldVersion:$}=d;S=d.target.result,s(S)||i.error(w("init",a,l.DB_CREATE_FAIL)),$&&L&&L>$?c&&i.success(w("init",a,`${l.DB_VERSION_LEVELUP}(${$} ➡️ ${L})`)):c&&i.success(w("init",a,l.DB_CREATE_SUCCESS)),H(S,e)||oe(S,r)},b.onerror=d=>{d.target.error?g(d.target.error):g(new Error(w("init",a,l.DB_OPEN_FAIL)))}})}function ne(t,e){return new Promise(async(r,c)=>{const{databaseName:n,tableName:a}=e;if(H(t,a))e.debug&&i.success(w("init",R(a),l.TB_EXITS)),r(t);else{const o=t.version+1;return t.close(),K(n,a,{...e,version:o})}})}function H(t,e){return t.objectStoreNames.contains(e)}function oe(t,e){console.log(e);const{tableName:r,primaryKey:c,autoIncrement:n}=e;s(c)||i.error(w("init",R(r),l.TB_LACK_PRIMARY)),t.createObjectStore(r,{keyPath:c,autoIncrement:n}),!e.noMessage&&e.debug&&i.success(w("init",R(r),l.TB_CREATE_SUCCESS))}function ae(t,e){return new Promise(async(r,c)=>{let n=null;if(e.autoIncrement)n=O(t,e);else{const o=await j(t,{...e,noMessage:!0}),g=o.code===200&&s(o.data);if(e.auto_cover)n=g?ce(t,e):O(t,e);else{if(g)return i.warn(w("setItem",l.DI_EXITS,outputDataValue(o.data))),r(h("setItem",0,!1)),null;n=O(t,e)}}const a=await n;r(a)})}function O(t,e){return new Promise((r,c)=>{const n={...e.value};!e.autoIncrement&&!s(n[e.primaryKey])&&(n[e.primaryKey]=e.key),e.autoIncrement&&s(n[e.primaryKey])&&delete n[e.primaryKey];const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).add(n);a.onsuccess=()=>{!e.noMessage&&e.debug&&i.success(A(e.key,JSON.stringify(e.value),l.DI_CREATE_SUCCESS)),r(h("addItem",200,n))},a.onerror=o=>{console.log(o),c(new Error(A(e.key,null,l.DI_CREATE_FAIL)))}})}function ce(t,e){return new Promise((r,c)=>{const n={...e.value};e.primaryKey&&(n[e.primaryKey]=e.key);const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).put(n);a.onsuccess=async()=>{!e.noMessage&&e.debug&&i.success(A(e.key,JSON.stringify(e.value),l.DI_UPDATE_SUCCESS));const o=await j(t,{...e,noMessage:!0});o.code===200&&s(o.data)?r(h("setItem",200,o.data)):r(h("setItem",200,null))},a.onerror=o=>{c(new Error(A(e.key,null,l.DI_UPDATE_FAIL)))}})}function j(t,e){return new Promise((r,c)=>{const n=t.transaction([e.tableName],"readonly").objectStore(e.tableName);let a=e.autoIncrement?Number(e.key):e.key,o=n.get(a);o.onsuccess=g=>{const S=g.target.result;S?(!e.noMessage&&e.debug&&i.success(A(e.key,JSON.stringify(S),l.DI_QUERY_SUCCESS)),r(h("getItem",200,S))):r(h("getItem",200,null))},o.onerror=g=>{c(new Error(l.DI_QUERY_ERROR))}})}function ie(t,e){return new Promise((r,c)=>{let a=t.transaction([e.tableName],"readonly").objectStore(e.tableName).getAll();a.onsuccess=o=>{const g=o.target.result;r(g?h("getAllItem",200,g):h("getAllItem",200,null))},a.onerror=o=>{c(new Error(l.DI_QUERY_ERROR))}})}function V(t,e){return new Promise((r,c)=>{const n=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).delete(e.key);n.onsuccess=a=>{!e.noMessage&&e.debug&&i.success(A(e.key,null,l.DI_DELETE_SUCCESS)),r(h("removeItem",200,!0))},n.onerror=a=>{c(new Error(A(e.key,null,l.DI_DELETE_ERROR)))}})}function ue(t,e){return new Promise((r,c)=>{const a=t.transaction(e.tableName).objectStore(e.tableName).count();a.onsuccess=()=>{!e.noMessage&&e.debug&&i.success(R(e.tableName)),r(h("length",200,a.result))},a.onerror=()=>{c(new Error(R(e.tableName)))}})}function le(t,e){return new Promise((r,c)=>{const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).clear();a.onsuccess=o=>{!e.noMessage&&e.debug&&i.success(R(e.tableName)),r(h("clear",200,!0))},a.onerror=o=>{c(new Error(l.TB_CLEAR_FAIL))}})}function ge(t,e,r){return new Promise((c,n)=>{let a=e.map(o=>O(t,{...r,key:Se(o,r.primaryKey),value:o}));Promise.allSettled(a).then(o=>{const g=o.filter(d=>d.status==="fulfilled"&&d.value.code===200&&s(d.value.data)).length,S=o.map(d=>d.value.data),b={total:e.length,success:g,fail:e.length-g,data:S};r.debug&&i.success(l.DI_BATCH_ADD_SUCCESS),c(h("batchSetItem",200,b))}).catch(o=>{n(o)})})}function me(t,e,r){return new Promise((c,n)=>{if(e.length===0)ie(t,{...r}).then(a=>{const o=fe(a.data,r.filter);a.code===200&&s(a.data)?(r.debug&&i.success(l.DI_BATCH_QUERY_SUCCESS),c(h("batchGetItem",200,o))):c(h("batchGetItem",200,null))}).catch(a=>{n(a)});else{let a=e.filter(o=>s(o)).map(o=>j(t,{...r,key:o,noMessage:!0}));Promise.allSettled(a).then(o=>{let g=o.filter(d=>d.status==="fulfilled"&&d.value.code===200&&s(d.value.data)).filter(d=>r.filter?r.filter(d.value.data):!0).map(d=>d.value.data);const S=g.length,b={total:e.length,success:S===e.length,process:{success:S,fail:e.length-S},data:g};r.debug&&i.success(l.DI_BATCH_QUERY_SUCCESS),c(h("batchGetItem",200,b))}).catch(o=>{n(o)})}})}function de(t,e,r){return new Promise((c,n)=>{let a=e.filter(o=>s(o)).map(o=>V(t,{...r,key:o,noMessage:!0}));Promise.allSettled(a).then(o=>{const g=o.filter(b=>b.status==="fulfilled"&&b.value.code===200&&s(b.value.data)).length,S={total:e.length,success:g,fail:e.length-g,data:g===e.length};r.debug&&i.success(l.DI_BATCH_DELETE_SUCCESS),c(h("batchRemoveItem",200,S))}).catch(o=>{n(o)})})}function fe(t,e){return t.filter(r=>e?e(r):!0)}function Se(t,e){return m(t[e],null)}const C=k[2],he={auto_cover:!1};class Ie{constructor(e={}){E(this,"options",null);E(this,"db",null);E(this,"version",null);this.options=Object.assign(he,e)}async init(){const{databaseName:e,tableName:r}=this.options;this.db=await K(e,r,this.options);const c=await ne(this.db,this.options);return this.db=c,this.version=this.db.version,this.options.autoIncrement=this.db.transaction([r],"readonly").objectStore(r).autoIncrement,this.db}getItem(e){return s(e)||i.error(C("getItem",f.get(1))),j(this.db,{...this.options,key:e})}setItem(e,r){return this.options.autoIncrement||s(e)||i.error(C("setItem",f.get(1))),s(r)||i.error(C("setItem",f.get(4))),y.isObject(r)||i.error(C("setItem",f.get(5))),ae(this.db,{...this.options,key:e,value:r})}removeItem(e){return s(e)||i.error(C("removeItem",f.get(2))),!y.isString(e)&&!y.isNumber(e)&&i.error(C("removeItem",f.get(3))),V(this.db,{...this.options,key:e})}length(){return ue(this.db,this.options)}clear(){return le(this.db,this.options)}batchProcess(e,r,c={}){s(e)||i.error(C("batchProcess",f.get(6))),s(r)||i.error(C("batchProcess",f.get(7)));let n=null;switch(e){case"get":n=me;break;case"add":n=ge;break;case"delete":n=de;break}return n||i.error(C("batchProcess",f.get(8))),n(this.db,r,{...this.options,...c})}}window.BrowserStoreKitMode={LOCAL_STORAGE:U,SESSION_STORAGE:P,INDEXED_DB:N};class _e{constructor(e,r){G(this,D);E(this,"type",null);E(this,"kitTool",null);this.type=e,T(this,D,J).call(this,r)}init(){return this.type!==N?i.error(`当前模式:${this.type}下无需调用init方法`):(T(this,D,B).call(this,"init"),this.kitTool.init())}getItem(e){return T(this,D,B).call(this,"getItem"),this.kitTool.getItem(e)}setItem(e,r){return T(this,D,B).call(this,"setItem"),this.kitTool.setItem(e,r)}removeItem(e){return T(this,D,B).call(this,"removeItem"),this.kitTool.removeItem(e)}length(){return T(this,D,B).call(this,"length"),this.kitTool.length()}clear(){return T(this,D,B).call(this,"clear"),this.kitTool.clear()}getOptions(){return this.kitTool.options}batchProcess(e,r,c){return this.kitTool.batchProcess(e,r,c)}}return D=new WeakSet,J=function(e){switch(this.type){case U:if(!Q())throw Error("抱歉,当前浏览器暂不支持使用localStorage");this.kitTool=new Z(e.localStorage);break;case P:if(!X())throw Error("抱歉,当前浏览器暂不支持使用sessionStorage");this.kitTool=new ee(e.sessionStorage);break;case N:if(!z())throw Error("抱歉,当前浏览器暂不支持使用indexedDB");this.kitTool=new Ie(e.indexedDB);break}},B=function(e){this.kitTool||i.error("抱歉,未正确实例化,暂时无法使用"+e+"方法")},_e});
|
|
1
|
+
(function(s,m){typeof exports=="object"&&typeof module<"u"?module.exports=m():typeof define=="function"&&define.amd?define(m):(s=typeof globalThis<"u"?globalThis:s||self,s.BrowserStoreKit=m())})(this,function(){"use strict";var be=Object.defineProperty;var G=s=>{throw TypeError(s)};var De=(s,m,_)=>m in s?be(s,m,{enumerable:!0,configurable:!0,writable:!0,value:_}):s[m]=_;var b=(s,m,_)=>De(s,typeof m!="symbol"?m+"":m,_),we=(s,m,_)=>m.has(s)||G("Cannot "+_);var J=(s,m,_)=>m.has(s)?G("Cannot add the same private member more than once"):m instanceof WeakSet?m.add(s):m.set(s,_);var C=(s,m,_)=>(we(s,m,"access private method"),_);var D,X,R;function s(t){return t!=null}function m(t,e){return s(t)?t:e}function _(t){try{const e=JSON.parse(t);return e&&typeof e=="object"&&!Array.isArray(e)}catch{return!1}}function z(){return s(window.localStorage)}function W(){return s(window.sessionStorage)}function Z(){return window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB}const y={isString:t=>s(t)&&typeof t=="string",isEmptyString:t=>s(t)&&typeof t=="string"&&t.trim()==="",isNumber:t=>s(t)&&typeof t=="number"&&!isNaN(t),isBoolean:t=>s(t)&&typeof t=="boolean",isFunction:t=>s(t)&&typeof t=="function",isObject:t=>Object.prototype.toString.call(t)==="[object Object]"};function x(t=new Date,e="YYYY-MM-DD HH:mm:ss"){const r=t.getFullYear(),o=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),a=String(t.getHours()).padStart(2,"0"),c=String(t.getMinutes()).padStart(2,"0"),d=String(t.getSeconds()).padStart(2,"0");return e.replace("YYYY",r).replace("MM",o).replace("DD",n).replace("HH",a).replace("mm",c).replace("ss",d)}const P="localStorage",O="sessionStorage",N="indexedDB",Y={success:"#67c23a",warning:"#e6a23c"},i={error:t=>{throw new Error(t)},warn:t=>{console.log(`%c[${x()}] `+t,`color: ${Y.warning};`)},info:t=>{console.log(t)},success:t=>{console.log(`%c[${x()}] `+t,`color: ${Y.success};`)}},u=new Map;u.set(1,"缺少参数key"),u.set(2,"参数key类型有误,必须为string类型"),u.set(3,"缺少参数value"),u.set(4,"数据项获取成功"),u.set(5,"数据项已存在"),u.set(6,"数据项设置成功"),u.set(7,"数据项更新成功"),u.set(8,"数据项不存在"),u.set(9,"数据项移除成功"),u.set(10,"数据项清除成功"),u.set(11,"数据项长度获取成功");const S=new Map;S.set(1,"缺少参数databaseName(数据库名称)、tableName(数据表名称)"),S.set(2,"缺少参数key"),S.set(3,"参数key类型有误,必须为string 或 number类型"),S.set(4,"缺少参数value"),S.set(5,"参数value类型有误,必须为object类型"),S.set(6,"缺少参数mode(可选值: 'get'、'add'、'delete')"),S.set(7,"缺少参数itemList(批量操作数据集合)"),S.set(8,"参数mode值有误(可选值: 'get'、'add'、'delete')");const k=[(t,...e)=>`🔗${P} 📎[${t}]: ${e.join("➡️")}`,(t,...e)=>`🔗${O} 📎[${t}]: ${e.join("➡️")}`,(t,...e)=>`🔗${N} 📢[${t}]: ${e.join("➡️")}`];function v(t){return`(key: ${t})`}function $(t){return`值为${_(t)?t:JSON.stringify(t)}`}const E=k[0],ee={auto_stringify:!1,auto_cover:!0,auto_parse:!1};class F{constructor(e){b(this,"_module",null);b(this,"options",null);this.options=e||ee}getItem(e){s(e)||i.error(E("getItem",u.get(1))),y.isString(e)||i.error(E("getItem",u.get(2)));let r=this._module.getItem(e);return s(r)&&this.options.debug&&i.success(E("getItem",u.get(4),v(e),$(r))),this.options.auto_parse&&s(r)&&_(r)&&(r=JSON.parse(r)),r}setItem(e,r){(!s(e)||y.isEmptyString(e))&&i.error(E("setItem",u.get(1))),y.isString(e)||i.error(E("setItem",u.get(2))),s(r)||i.error(E("setItem",u.get(3)));const o=this._module.getItem(e);if(!this.options.auto_cover&&s(o))return i.warn(E("setItem",u.get(5),v(e),$(o))),!1;let n=r;return this.options.auto_stringify&&!_(r)&&(n=JSON.stringify(r)),this._module.setItem(e,n),s(this._module.getItem(e))?(this.options.debug&&i.success(E("setItem",u.get(s(o)?7:6),v(e),$(n))),!0):!1}removeItem(e){s(e)||i.error(E("removeItem",u.get(1))),y.isString(e)||i.error(E("removeItem",u.get(2)));const r=this._module.getItem(e);return s(r)||i.warn(E("removeItem",u.get(8))),this._module.removeItem(e),s(this._module.getItem(e))?!1:(s(r)&&this.options.debug&&i.success(E("removeItem",u.get(9))),!0)}length(){return i.success(E("length",u.get(11))),this._module.length}clear(){this._module.clear();let e=this.length();return s(e)&&e===0?(this.options.debug&&i.success(E("clear",u.get(10))),!0):!1}}class te extends F{constructor(e){super(e),this._module=window.localStorage}}class re extends F{constructor(e){super(e),this._module=window.sessionStorage}}class se{constructor(e,r){b(this,"type",null);b(this,"method",null);b(this,"code",null);b(this,"status","");b(this,"message","");b(this,"data",null);this.type=e;const{code:o,data:n,method:a}=r;this.code=o,this.method=a,this.status=o===200?"success":"error",this.data=n,this.message=ne(e,o)}}function ne(t,e){let r="";switch(t){case P:r=e===200?"操作成功":"操作失败";break;case O:r=e===200?"操作成功":"操作失败";break;case N:r=e===200?"操作成功":"操作失败";break}return r}function h(t,e,r){return new se(N,{method:t,code:e,data:r})}const w=k[2],K=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,l={DB_OPEN_SUCCESS:"数据库打开成功",DB_OPEN_FAIL:"数据库打开失败",DB_CREATE_SUCCESS:"数据库创建成功",DB_CREATE_FAIL:"数据库创建失败",DB_VERSION_LEVELUP:"数据库版本升级",TB_CREATE_SUCCESS:"数据表创建成功",TB_QUERY_ERROR:"数据表查询出错",TB_EXITS:"数据表已存在,无需创建",TB_LACK_PRIMARY:"数据表缺少主键",TB_CLEAR_FAIL:"数据表清空失败",DI_CREATE_SUCCESS:"数据项创建成功",DI_CREATE_FAIL:"数据项创建失败",DI_UPDATE_SUCCESS:"数据项创建成功",DI_UPDATE_FAIL:"数据项创建失败",DI_QUERY_SUCCESS:"数据查询成功",DI_QUERY_ERROR:"数据项查询出错",DI_DELETE_SUCCESS:"数据删除成功",DI_DELETE_ERROR:"数据项删除出错",DI_EXITS:"数据项已存在",DI_BATCH_QUERY_SUCCESS:"批量数据查询成功",DI_BATCH_DELETE_SUCCESS:"批量数据删除成功",DI_BATCH_ADD_SUCCESS:"批量数据添加成功"};function ae(t){return`📦${t}`}function A(t){return`📊${t}`}function B(t,e,r){return`数据项(key:${t}): ${r}`+(e?`; 值为: ${e}`:"")}function H(t,e,r){const{debug:o,version:n}=r,a=ae(t);return new Promise((c,d)=>{s(t)&&s(e)||i.error(w("init",S.get(1)));let f=null;const I=n?K.open(t,n):K.open(t);I.onsuccess=()=>{o&&i.success(w("init",a,l.DB_OPEN_SUCCESS)),f=I.result,c(f)},I.onupgradeneeded=g=>{const{newVersion:U,oldVersion:p}=g;f=g.target.result,s(f)||i.error(w("init",a,l.DB_CREATE_FAIL)),p&&U&&U>p?o&&i.success(w("init",a,`${l.DB_VERSION_LEVELUP}(${p} ➡️ ${U})`)):o&&i.success(w("init",a,l.DB_CREATE_SUCCESS)),V(f,e)||ce(f,r)},I.onerror=g=>{g.target.error?d(g.target.error):d(new Error(w("init",a,l.DB_OPEN_FAIL)))}})}function oe(t,e){return new Promise(async(r,o)=>{const{databaseName:n,tableName:a}=e;if(V(t,a))e.debug&&i.success(w("init",A(a),l.TB_EXITS)),r(t);else{const c=t.version+1;return t.close(),H(n,a,{...e,version:c})}})}function V(t,e){return t.objectStoreNames.contains(e)}function ce(t,e){console.log(e);const{tableName:r,primaryKey:o,autoIncrement:n}=e;s(o)||i.error(w("init",A(r),l.TB_LACK_PRIMARY)),t.createObjectStore(r,{keyPath:o,autoIncrement:n}),!e.noMessage&&e.debug&&i.success(w("init",A(r),l.TB_CREATE_SUCCESS))}function ie(t,e){return new Promise(async(r,o)=>{let n=null;if(e.autoIncrement)n=M(t,e);else{const c=await L(t,{...e,noMessage:!0}),d=c.code===200&&s(c.data);if(e.auto_cover)n=d?ue(t,e):M(t,e);else{if(d)return i.warn(w("setItem",l.DI_EXITS,outputDataValue(c.data))),r(h("setItem",0,!1)),null;n=M(t,e)}}const a=await n;r(a)})}function M(t,e){return new Promise((r,o)=>{const n={...e.value};!e.autoIncrement&&!s(n[e.primaryKey])&&(n[e.primaryKey]=e.key),e.autoIncrement&&s(n[e.primaryKey])&&delete n[e.primaryKey];const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).add(n);a.onsuccess=()=>{!e.noMessage&&e.debug&&i.success(B(e.key,JSON.stringify(e.value),l.DI_CREATE_SUCCESS)),r(h("addItem",200,n))},a.onerror=c=>{console.log(c),o(new Error(B(e.key,null,l.DI_CREATE_FAIL)))}})}function ue(t,e){return new Promise((r,o)=>{const n={...e.value};e.primaryKey&&(n[e.primaryKey]=e.key);const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).put(n);a.onsuccess=async()=>{!e.noMessage&&e.debug&&i.success(B(e.key,JSON.stringify(e.value),l.DI_UPDATE_SUCCESS));const c=await L(t,{...e,noMessage:!0});c.code===200&&s(c.data)?r(h("setItem",200,c.data)):r(h("setItem",200,null))},a.onerror=c=>{o(new Error(B(e.key,null,l.DI_UPDATE_FAIL)))}})}function L(t,e){return new Promise((r,o)=>{const n=t.transaction([e.tableName],"readonly").objectStore(e.tableName);let a=e.autoIncrement?Number(e.key):e.key,c=n.get(a);c.onsuccess=d=>{const f=d.target.result;f?(!e.noMessage&&e.debug&&i.success(B(e.key,JSON.stringify(f),l.DI_QUERY_SUCCESS)),r(h("getItem",200,f))):r(h("getItem",200,null))},c.onerror=d=>{o(new Error(l.DI_QUERY_ERROR))}})}function le(t,e){return new Promise(async(r,o)=>{const n=await q(t,e);let a=null;n.code===200&&s(n.data)&&(a=n.data);let d=t.transaction([e.tableName],"readonly").objectStore(e.tableName).getAll();d.onsuccess=f=>{const I=f.target.result;if(I){!e.noMessage&&e.debug&&i.success(A(e.tableName));const g=Se(I,e.filter),U={...j(a,g.length),data:g};r(h("getAllItem",200,U))}else r(h("getAllItem",200,null))},d.onerror=f=>{o(new Error(l.TB_QUERY_ERROR))}})}function Q(t,e){return new Promise((r,o)=>{const n=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).delete(e.key);n.onsuccess=a=>{!e.noMessage&&e.debug&&i.success(B(e.key,null,l.DI_DELETE_SUCCESS)),r(h("removeItem",200,!0))},n.onerror=a=>{o(new Error(B(e.key,null,l.DI_DELETE_ERROR)))}})}function q(t,e){return new Promise((r,o)=>{const a=t.transaction(e.tableName).objectStore(e.tableName).count();a.onsuccess=()=>{!e.noMessage&&e.debug&&i.success(A(e.tableName)),r(h("length",200,a.result))},a.onerror=()=>{o(new Error(A(e.tableName)))}})}function me(t,e){return new Promise((r,o)=>{const a=t.transaction([e.tableName],"readwrite").objectStore(e.tableName).clear();a.onsuccess=c=>{!e.noMessage&&e.debug&&i.success(A(e.tableName)),r(h("clear",200,!0))},a.onerror=c=>{o(new Error(l.TB_CLEAR_FAIL))}})}function ge(t,e,r){return new Promise((o,n)=>{let a=e.map(c=>M(t,{...r,key:he(c,r.primaryKey),value:c,noMessage:!0}));Promise.allSettled(a).then(c=>{const d=c.filter(g=>g.status==="fulfilled"&&g.value.code===200&&s(g.value.data)).length,f=c.map(g=>g.value.data),I={...j(e.length,d),data:f};r.debug&&i.success(l.DI_BATCH_ADD_SUCCESS),o(h("batchSetItem",200,I))}).catch(c=>{n(c)})})}function de(t,e,r){return new Promise((o,n)=>{if(e.length===0)le(t,{...r,noMessage:!0}).then(a=>{a.code===200&&s(a.data)?(r.debug&&i.success(l.DI_BATCH_QUERY_SUCCESS),o(h("batchGetItem",200,a.data))):o(h("batchGetItem",200,null))}).catch(a=>{n(a)});else{let a=e.filter(c=>s(c)).map(c=>L(t,{...r,key:c,noMessage:!0}));Promise.allSettled(a).then(c=>{let d=c.filter(g=>g.status==="fulfilled"&&g.value.code===200&&s(g.value.data)).filter(g=>r.filter?r.filter(g.value.data):!0).map(g=>g.value.data);const f=d.length,I={...j(e.length,f),data:d};r.debug&&i.success(l.DI_BATCH_QUERY_SUCCESS),o(h("batchGetItem",200,I))}).catch(c=>{n(c)})}})}function fe(t,e,r){return new Promise((o,n)=>{let a=e.filter(c=>s(c)).map(c=>Q(t,{...r,key:c,noMessage:!0}));Promise.allSettled(a).then(c=>{const d=c.filter(I=>I.status==="fulfilled"&&I.value.code===200&&s(I.value.data)).length,f={...j(e.length,d),data:d===e.length};r.debug&&i.success(l.DI_BATCH_DELETE_SUCCESS),o(h("batchRemoveItem",200,f))}).catch(c=>{n(c)})})}function Se(t,e){return t.filter(r=>e?e(r):!0)}function he(t,e){return m(t[e],null)}function j(t,e){return{total:t,success:e===t,process:{success:e,fail:t-e}}}const T=k[2],Ie={auto_cover:!1};class _e{constructor(e={}){b(this,"options",null);b(this,"db",null);b(this,"version",null);this.options=Object.assign(Ie,e)}async init(){const{databaseName:e,tableName:r}=this.options;this.db=await H(e,r,this.options);const o=await oe(this.db,this.options);return this.db=o,this.version=this.db.version,this.options.autoIncrement=this.db.transaction([r],"readonly").objectStore(r).autoIncrement,this.db}getItem(e){return s(e)||i.error(T("getItem",S.get(1))),L(this.db,{...this.options,key:e})}setItem(e,r){return this.options.autoIncrement||s(e)||i.error(T("setItem",S.get(1))),s(r)||i.error(T("setItem",S.get(4))),y.isObject(r)||i.error(T("setItem",S.get(5))),ie(this.db,{...this.options,key:e,value:r})}removeItem(e){return s(e)||i.error(T("removeItem",S.get(2))),!y.isString(e)&&!y.isNumber(e)&&i.error(T("removeItem",S.get(3))),Q(this.db,{...this.options,key:e})}length(){return q(this.db,this.options)}clear(){return me(this.db,this.options)}batchProcess(e,r,o={}){s(e)||i.error(T("batchProcess",S.get(6))),s(r)||i.error(T("batchProcess",S.get(7)));let n=null;switch(e){case"get":n=de;break;case"add":n=ge;break;case"delete":n=fe;break}return n||i.error(T("batchProcess",S.get(8))),n(this.db,r,{...this.options,...o})}}class Ee{constructor(e,r){J(this,D);b(this,"type",null);b(this,"kitTool",null);this.type=e,C(this,D,X).call(this,r)}init(){return this.type!==N?i.error(`当前模式:${this.type}下无需调用init方法`):(C(this,D,R).call(this,"init"),this.kitTool.init())}getItem(e){return C(this,D,R).call(this,"getItem"),this.kitTool.getItem(e)}setItem(e,r){return C(this,D,R).call(this,"setItem"),this.kitTool.setItem(e,r)}removeItem(e){return C(this,D,R).call(this,"removeItem"),this.kitTool.removeItem(e)}length(){return C(this,D,R).call(this,"length"),this.kitTool.length()}clear(){return C(this,D,R).call(this,"clear"),this.kitTool.clear()}getOptions(){return this.kitTool.options}batchProcess(e,r,o){return this.kitTool.batchProcess(e,r,o)}}return D=new WeakSet,X=function(e){switch(this.type){case P:if(!z())throw Error("抱歉,当前浏览器暂不支持使用localStorage");this.kitTool=new te(e.localStorage);break;case O:if(!W())throw Error("抱歉,当前浏览器暂不支持使用sessionStorage");this.kitTool=new re(e.sessionStorage);break;case N:if(!Z())throw Error("抱歉,当前浏览器暂不支持使用indexedDB");this.kitTool=new _e(e.indexedDB);break}},R=function(e){this.kitTool||i.error("抱歉,未正确实例化,暂时无法使用"+e+"方法")},window.BrowserStoreKitMode={LOCAL_STORAGE:P,SESSION_STORAGE:O,INDEXED_DB:N},Ee});
|
package/package.json
CHANGED
package/src/db/handle.js
CHANGED
|
@@ -376,7 +376,12 @@ export function getItem(db, options) {
|
|
|
376
376
|
* @returns {Promise<StorageKitResponse>} 操作结果
|
|
377
377
|
*/
|
|
378
378
|
export function getAllItem(db, options) {
|
|
379
|
-
return new Promise((resolve, reject) => {
|
|
379
|
+
return new Promise(async (resolve, reject) => {
|
|
380
|
+
const res = await getLength(db, options)
|
|
381
|
+
let count = null
|
|
382
|
+
if(res.code === 200 && isDefine(res.data)) {
|
|
383
|
+
count = res.data
|
|
384
|
+
}
|
|
380
385
|
const objectStore = db.transaction([options.tableName], "readonly").objectStore(options.tableName)
|
|
381
386
|
let request = objectStore.getAll()
|
|
382
387
|
request.onsuccess = (event) => {
|
|
@@ -385,14 +390,19 @@ export function getAllItem(db, options) {
|
|
|
385
390
|
if (!options.noMessage && options.debug) {
|
|
386
391
|
message.success(getTBMessage(options.tableName, INDEXED_DB_MESSAGE.TB_QUERY_SUCCESS))
|
|
387
392
|
}
|
|
388
|
-
|
|
393
|
+
const value = handleFilterData(target, options.filter)
|
|
394
|
+
const result = {
|
|
395
|
+
...getResponseDataProcess(count, value.length),
|
|
396
|
+
data: value
|
|
397
|
+
}
|
|
398
|
+
resolve(getIndexedDBResponse('getAllItem', 200, result))
|
|
389
399
|
} else {
|
|
390
400
|
resolve(getIndexedDBResponse('getAllItem', 200, null))
|
|
391
401
|
}
|
|
392
402
|
};
|
|
393
403
|
|
|
394
404
|
request.onerror = (event) => {
|
|
395
|
-
reject(new Error(INDEXED_DB_MESSAGE.
|
|
405
|
+
reject(new Error(INDEXED_DB_MESSAGE.TB_QUERY_ERROR))
|
|
396
406
|
}
|
|
397
407
|
})
|
|
398
408
|
}
|
|
@@ -475,7 +485,7 @@ export function clearItem(db, options) {
|
|
|
475
485
|
export function handleBatchSetItem(db, itemList, options) {
|
|
476
486
|
return new Promise((resolve, reject) => {
|
|
477
487
|
let promiseArr = itemList.map(item => {
|
|
478
|
-
return addItem(db, { ...options, key: getPrimaryKeyValue(item, options.primaryKey), value: item })
|
|
488
|
+
return addItem(db, { ...options, key: getPrimaryKeyValue(item, options.primaryKey), value: item, noMessage: true })
|
|
479
489
|
})
|
|
480
490
|
Promise.allSettled(promiseArr).then(r => {
|
|
481
491
|
const successNum = r.filter(i => {
|
|
@@ -485,9 +495,7 @@ export function handleBatchSetItem(db, itemList, options) {
|
|
|
485
495
|
return j.value.data
|
|
486
496
|
})
|
|
487
497
|
const result = {
|
|
488
|
-
|
|
489
|
-
success: successNum,
|
|
490
|
-
fail: itemList.length - successNum,
|
|
498
|
+
...getResponseDataProcess(itemList.length, successNum),
|
|
491
499
|
data: valueList
|
|
492
500
|
}
|
|
493
501
|
if (options.debug) {
|
|
@@ -511,12 +519,11 @@ export function handleBatchGetItem(db, itemList, options) {
|
|
|
511
519
|
// 获取全量数据
|
|
512
520
|
if (itemList.length === 0) {
|
|
513
521
|
getAllItem(db, { ...options, noMessage: true }).then(res => {
|
|
514
|
-
const value = handleFilterData(res.data, options.filter)
|
|
515
522
|
if (res.code === 200 && isDefine(res.data)) {
|
|
516
523
|
if (options.debug) {
|
|
517
524
|
message.success(INDEXED_DB_MESSAGE.DI_BATCH_QUERY_SUCCESS)
|
|
518
525
|
}
|
|
519
|
-
resolve(getIndexedDBResponse('batchGetItem', 200,
|
|
526
|
+
resolve(getIndexedDBResponse('batchGetItem', 200, res.data))
|
|
520
527
|
} else {
|
|
521
528
|
resolve(getIndexedDBResponse('batchGetItem', 200, null))
|
|
522
529
|
}
|
|
@@ -540,9 +547,7 @@ export function handleBatchGetItem(db, itemList, options) {
|
|
|
540
547
|
})
|
|
541
548
|
const successNum = valueList.length
|
|
542
549
|
const result = {
|
|
543
|
-
|
|
544
|
-
success: Boolean(successNum === itemList.length),
|
|
545
|
-
process: { success: successNum, fail: itemList.length - successNum }, // 成功数、失败数
|
|
550
|
+
...getResponseDataProcess(itemList.length, successNum),
|
|
546
551
|
data: valueList
|
|
547
552
|
}
|
|
548
553
|
if (options.debug) {
|
|
@@ -574,9 +579,7 @@ export function handleBatchRemoveItem(db, itemList, options) {
|
|
|
574
579
|
return i.status === "fulfilled" && i.value.code === 200 && isDefine(i.value.data)
|
|
575
580
|
}).length
|
|
576
581
|
const result = {
|
|
577
|
-
|
|
578
|
-
success: successNum,
|
|
579
|
-
fail: itemList.length - successNum,
|
|
582
|
+
...getResponseDataProcess(itemList.length, successNum),
|
|
580
583
|
data: Boolean(successNum === itemList.length)
|
|
581
584
|
}
|
|
582
585
|
if (options.debug) {
|
|
@@ -615,4 +618,18 @@ function handleFilterData(data, filter) {
|
|
|
615
618
|
*/
|
|
616
619
|
function getPrimaryKeyValue(data, primaryKey) {
|
|
617
620
|
return defValue(data[primaryKey], null)
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* 获取相应数据Process
|
|
625
|
+
* @param {number} total 总数
|
|
626
|
+
* @param {number} success 成功数
|
|
627
|
+
* @returns
|
|
628
|
+
*/
|
|
629
|
+
function getResponseDataProcess(total, success) {
|
|
630
|
+
return {
|
|
631
|
+
total: total,
|
|
632
|
+
success: Boolean(success === total),
|
|
633
|
+
process: { success: success, fail: total - success }
|
|
634
|
+
}
|
|
618
635
|
}
|
package/src/index.js
CHANGED
|
@@ -5,12 +5,6 @@ import { canUseIndexedDB, canUseLocalStorge, canUseSessionStorge } from './utils
|
|
|
5
5
|
import { message } from './utils/message.js'
|
|
6
6
|
import { LOCAL_STORAGE, SESSION_STORAGE, INDEXED_DB } from './constant.js'
|
|
7
7
|
|
|
8
|
-
window.BrowserStoreKitMode = {
|
|
9
|
-
LOCAL_STORAGE,
|
|
10
|
-
SESSION_STORAGE,
|
|
11
|
-
INDEXED_DB,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
8
|
/**
|
|
15
9
|
* 存储模式
|
|
16
10
|
* @typedef {typeof LOCAL_STORAGE | typeof SESSION_STORAGE | typeof INDEXED_DB} BrowserStoreKitModeOptions
|