@whitesev/utils 2.1.3 → 2.1.4
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/dist/index.amd.js +89 -100
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +89 -100
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +89 -100
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +89 -100
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +89 -100
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +89 -100
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/indexedDB.d.ts +31 -0
- package/package.json +1 -1
- package/src/indexedDB.ts +129 -114
package/dist/index.cjs.js
CHANGED
|
@@ -2522,22 +2522,22 @@ class indexedDB {
|
|
|
2522
2522
|
#db = {};
|
|
2523
2523
|
// @ts-ignore
|
|
2524
2524
|
#store = null;
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2525
|
+
/** 状态码 */
|
|
2526
|
+
#statusCode = {
|
|
2527
|
+
operationSuccess: {
|
|
2528
2528
|
code: 200,
|
|
2529
2529
|
msg: "操作成功",
|
|
2530
2530
|
},
|
|
2531
|
-
|
|
2531
|
+
operationFailed: {
|
|
2532
2532
|
code: 401,
|
|
2533
2533
|
msg: "操作失败",
|
|
2534
2534
|
},
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2535
|
+
openFailed: { code: 91001, msg: "打开数据库失败" },
|
|
2536
|
+
saveFailed: { code: 91002, msg: "保存数据失败" },
|
|
2537
|
+
getFailed: { code: 91003, msg: "获取数据失败" },
|
|
2538
|
+
deleteFailed: { code: 91004, msg: "删除数据失败" },
|
|
2539
|
+
deleteAllFailed: { code: 91005, msg: "清空数据库失败" },
|
|
2540
|
+
regexpGetFailed: { code: 91006, msg: "正则获取数据失败" },
|
|
2541
2541
|
};
|
|
2542
2542
|
/**
|
|
2543
2543
|
* @param dbName 数据存储名,默认为:default_db
|
|
@@ -2578,11 +2578,11 @@ class indexedDB {
|
|
|
2578
2578
|
/* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
|
|
2579
2579
|
let request = that.#indexedDB.open(dbName, that.#dbVersion);
|
|
2580
2580
|
request.onerror = function (event) {
|
|
2581
|
-
callback({
|
|
2582
|
-
code: that.#
|
|
2583
|
-
msg: that.#
|
|
2581
|
+
callback(null, {
|
|
2582
|
+
code: that.#statusCode.openFailed.code,
|
|
2583
|
+
msg: that.#statusCode.openFailed.msg,
|
|
2584
2584
|
event: event,
|
|
2585
|
-
}
|
|
2585
|
+
});
|
|
2586
2586
|
};
|
|
2587
2587
|
request.onsuccess = function (event) {
|
|
2588
2588
|
if (!that.#db[dbName]) {
|
|
@@ -2590,7 +2590,7 @@ class indexedDB {
|
|
|
2590
2590
|
that.#db[dbName] = target.result;
|
|
2591
2591
|
}
|
|
2592
2592
|
let store = that.createStore(dbName);
|
|
2593
|
-
callback(store
|
|
2593
|
+
callback(store);
|
|
2594
2594
|
};
|
|
2595
2595
|
request.onupgradeneeded = function (event) {
|
|
2596
2596
|
let target = event.target;
|
|
@@ -2599,14 +2599,14 @@ class indexedDB {
|
|
|
2599
2599
|
keyPath: "key",
|
|
2600
2600
|
});
|
|
2601
2601
|
store.transaction.oncomplete = function (event) {
|
|
2602
|
-
callback(store
|
|
2602
|
+
callback(store);
|
|
2603
2603
|
};
|
|
2604
2604
|
};
|
|
2605
2605
|
}
|
|
2606
2606
|
else {
|
|
2607
2607
|
/* 如果缓存中已经打开了数据库,就直接使用 */
|
|
2608
|
-
let store =
|
|
2609
|
-
callback(store
|
|
2608
|
+
let store = this.createStore(dbName);
|
|
2609
|
+
callback(store);
|
|
2610
2610
|
}
|
|
2611
2611
|
}
|
|
2612
2612
|
/**
|
|
@@ -2617,36 +2617,35 @@ class indexedDB {
|
|
|
2617
2617
|
async save(key, value) {
|
|
2618
2618
|
let that = this;
|
|
2619
2619
|
return new Promise((resolve) => {
|
|
2620
|
-
let dbName =
|
|
2620
|
+
let dbName = this.#dbName;
|
|
2621
2621
|
let inData = {
|
|
2622
2622
|
key: key,
|
|
2623
2623
|
value: value,
|
|
2624
2624
|
};
|
|
2625
|
-
|
|
2626
|
-
if (
|
|
2625
|
+
this.open(function (idbStore) {
|
|
2626
|
+
if (idbStore == null) {
|
|
2627
2627
|
resolve({
|
|
2628
2628
|
success: false,
|
|
2629
|
-
code: that.#
|
|
2630
|
-
msg: that.#
|
|
2629
|
+
code: that.#statusCode.saveFailed.code,
|
|
2630
|
+
msg: that.#statusCode.saveFailed.msg,
|
|
2631
2631
|
});
|
|
2632
2632
|
}
|
|
2633
2633
|
else {
|
|
2634
|
-
idbStore = idbStore;
|
|
2635
2634
|
let request = idbStore.put(inData);
|
|
2636
2635
|
request.onsuccess = function (event) {
|
|
2637
2636
|
/* 保存成功有success 字段 */
|
|
2638
2637
|
resolve({
|
|
2639
2638
|
success: true,
|
|
2640
|
-
code: that.#
|
|
2641
|
-
msg: that.#
|
|
2639
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2640
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2642
2641
|
event: event,
|
|
2643
2642
|
});
|
|
2644
2643
|
};
|
|
2645
2644
|
request.onerror = function (event) {
|
|
2646
2645
|
resolve({
|
|
2647
2646
|
success: false,
|
|
2648
|
-
code: that.#
|
|
2649
|
-
msg: that.#
|
|
2647
|
+
code: that.#statusCode.saveFailed.code,
|
|
2648
|
+
msg: that.#statusCode.saveFailed.msg,
|
|
2650
2649
|
event: event,
|
|
2651
2650
|
});
|
|
2652
2651
|
};
|
|
@@ -2662,33 +2661,32 @@ class indexedDB {
|
|
|
2662
2661
|
let that = this;
|
|
2663
2662
|
return new Promise((resolve) => {
|
|
2664
2663
|
let dbName = this.#dbName;
|
|
2665
|
-
this.open(function (idbStore
|
|
2664
|
+
this.open(function (idbStore) {
|
|
2666
2665
|
/* 判断返回的数据中是否有error字段 */
|
|
2667
|
-
if (
|
|
2666
|
+
if (idbStore == null) {
|
|
2668
2667
|
resolve({
|
|
2669
2668
|
success: false,
|
|
2670
|
-
code: that.#
|
|
2671
|
-
msg: that.#
|
|
2669
|
+
code: that.#statusCode.getFailed.code,
|
|
2670
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2672
2671
|
});
|
|
2673
2672
|
}
|
|
2674
2673
|
else {
|
|
2675
|
-
idbStore = idbStore;
|
|
2676
2674
|
let request = idbStore.get(key);
|
|
2677
2675
|
request.onsuccess = function (event) {
|
|
2678
2676
|
/* result 返回的是 {key: string, value: any} */
|
|
2679
2677
|
/* 键值对存储 */
|
|
2680
2678
|
resolve({
|
|
2681
2679
|
success: true,
|
|
2682
|
-
code: that.#
|
|
2683
|
-
msg: that.#
|
|
2680
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2681
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2684
2682
|
event: event,
|
|
2685
2683
|
});
|
|
2686
2684
|
};
|
|
2687
2685
|
request.onerror = function (event) {
|
|
2688
2686
|
resolve({
|
|
2689
2687
|
success: false,
|
|
2690
|
-
code: that.#
|
|
2691
|
-
msg: that.#
|
|
2688
|
+
code: that.#statusCode.getFailed.code,
|
|
2689
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2692
2690
|
event: event,
|
|
2693
2691
|
});
|
|
2694
2692
|
};
|
|
@@ -2704,18 +2702,17 @@ class indexedDB {
|
|
|
2704
2702
|
let that = this;
|
|
2705
2703
|
return new Promise((resolve) => {
|
|
2706
2704
|
let dbName = this.#dbName;
|
|
2707
|
-
this.open(function (idbStore
|
|
2705
|
+
this.open(function (idbStore) {
|
|
2708
2706
|
/* 判断返回的数据中是否有error字段 */
|
|
2709
|
-
if (
|
|
2707
|
+
if (idbStore == null) {
|
|
2710
2708
|
resolve({
|
|
2711
2709
|
success: false,
|
|
2712
|
-
code: that.#
|
|
2713
|
-
msg: that.#
|
|
2710
|
+
code: that.#statusCode.getFailed.code,
|
|
2711
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2714
2712
|
data: void 0,
|
|
2715
2713
|
});
|
|
2716
2714
|
}
|
|
2717
2715
|
else {
|
|
2718
|
-
idbStore = idbStore;
|
|
2719
2716
|
let request = idbStore.get(key);
|
|
2720
2717
|
request.onsuccess = function (event) {
|
|
2721
2718
|
let target = event.target;
|
|
@@ -2726,8 +2723,8 @@ class indexedDB {
|
|
|
2726
2723
|
if (data) {
|
|
2727
2724
|
resolve({
|
|
2728
2725
|
success: true,
|
|
2729
|
-
code: that.#
|
|
2730
|
-
msg: that.#
|
|
2726
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2727
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2731
2728
|
data: data,
|
|
2732
2729
|
event: event,
|
|
2733
2730
|
result: result,
|
|
@@ -2736,8 +2733,8 @@ class indexedDB {
|
|
|
2736
2733
|
else {
|
|
2737
2734
|
resolve({
|
|
2738
2735
|
success: false,
|
|
2739
|
-
code: that.#
|
|
2740
|
-
msg: that.#
|
|
2736
|
+
code: that.#statusCode.operationFailed.code,
|
|
2737
|
+
msg: that.#statusCode.operationFailed.msg,
|
|
2741
2738
|
data: void 0,
|
|
2742
2739
|
event: event,
|
|
2743
2740
|
result: result,
|
|
@@ -2745,12 +2742,10 @@ class indexedDB {
|
|
|
2745
2742
|
}
|
|
2746
2743
|
};
|
|
2747
2744
|
request.onerror = function (event) {
|
|
2748
|
-
// @ts-ignore
|
|
2749
|
-
event.target;
|
|
2750
2745
|
resolve({
|
|
2751
2746
|
success: false,
|
|
2752
|
-
code: that.#
|
|
2753
|
-
msg: that.#
|
|
2747
|
+
code: that.#statusCode.getFailed.code,
|
|
2748
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2754
2749
|
data: void 0,
|
|
2755
2750
|
event: event,
|
|
2756
2751
|
});
|
|
@@ -2769,18 +2764,17 @@ class indexedDB {
|
|
|
2769
2764
|
return new Promise((resolve) => {
|
|
2770
2765
|
/* 正则查询 */
|
|
2771
2766
|
let dbName = that.#dbName;
|
|
2772
|
-
|
|
2767
|
+
this.open(function (idbStore) {
|
|
2773
2768
|
/* 判断返回的数据中是否有error字段 */
|
|
2774
|
-
if (
|
|
2769
|
+
if (idbStore == null) {
|
|
2775
2770
|
resolve({
|
|
2776
2771
|
success: false,
|
|
2777
|
-
code: that.#
|
|
2778
|
-
msg: that.#
|
|
2772
|
+
code: that.#statusCode.regexpGetFailed.code,
|
|
2773
|
+
msg: that.#statusCode.regexpGetFailed.msg,
|
|
2779
2774
|
data: [],
|
|
2780
2775
|
});
|
|
2781
2776
|
}
|
|
2782
2777
|
else {
|
|
2783
|
-
idbStore = idbStore;
|
|
2784
2778
|
let request = idbStore.getAll();
|
|
2785
2779
|
request.onsuccess = function (event) {
|
|
2786
2780
|
let target = event.target;
|
|
@@ -2798,19 +2792,17 @@ class indexedDB {
|
|
|
2798
2792
|
}
|
|
2799
2793
|
resolve({
|
|
2800
2794
|
success: true,
|
|
2801
|
-
code: that.#
|
|
2802
|
-
msg: that.#
|
|
2795
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2796
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2803
2797
|
data: list,
|
|
2804
2798
|
event: event,
|
|
2805
2799
|
});
|
|
2806
2800
|
};
|
|
2807
2801
|
request.onerror = function (event) {
|
|
2808
|
-
// @ts-ignore
|
|
2809
|
-
event.target;
|
|
2810
2802
|
resolve({
|
|
2811
2803
|
success: false,
|
|
2812
|
-
code: that.#
|
|
2813
|
-
msg: that.#
|
|
2804
|
+
code: that.#statusCode.getFailed.code,
|
|
2805
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2814
2806
|
data: [],
|
|
2815
2807
|
event: event,
|
|
2816
2808
|
});
|
|
@@ -2828,44 +2820,30 @@ class indexedDB {
|
|
|
2828
2820
|
return new Promise((resolve) => {
|
|
2829
2821
|
/* 根据key删除某条数据 */
|
|
2830
2822
|
let dbName = that.#dbName;
|
|
2831
|
-
|
|
2832
|
-
if (
|
|
2823
|
+
this.open(function (idbStore) {
|
|
2824
|
+
if (idbStore == null) {
|
|
2833
2825
|
resolve({
|
|
2834
2826
|
success: false,
|
|
2835
|
-
code: that.#
|
|
2836
|
-
msg: that.#
|
|
2827
|
+
code: that.#statusCode.deleteFailed.code,
|
|
2828
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
2837
2829
|
});
|
|
2838
2830
|
}
|
|
2839
2831
|
else {
|
|
2840
|
-
|
|
2841
|
-
let request = idbStore.
|
|
2832
|
+
// 删除键
|
|
2833
|
+
let request = idbStore.delete(key);
|
|
2842
2834
|
request.onsuccess = function (event) {
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
success: true,
|
|
2850
|
-
code: that.#errorCode.success.code,
|
|
2851
|
-
msg: that.#errorCode.success.msg,
|
|
2852
|
-
});
|
|
2853
|
-
}
|
|
2854
|
-
else {
|
|
2855
|
-
resolve({
|
|
2856
|
-
success: false,
|
|
2857
|
-
code: that.#errorCode.error.code,
|
|
2858
|
-
msg: that.#errorCode.error.msg,
|
|
2859
|
-
});
|
|
2860
|
-
}
|
|
2835
|
+
resolve({
|
|
2836
|
+
success: true,
|
|
2837
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2838
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2839
|
+
event: event,
|
|
2840
|
+
});
|
|
2861
2841
|
};
|
|
2862
2842
|
request.onerror = function (event) {
|
|
2863
|
-
// @ts-ignore
|
|
2864
|
-
event.target;
|
|
2865
2843
|
resolve({
|
|
2866
2844
|
success: false,
|
|
2867
|
-
code: that.#
|
|
2868
|
-
msg: that.#
|
|
2845
|
+
code: that.#statusCode.deleteFailed.code,
|
|
2846
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
2869
2847
|
event: event,
|
|
2870
2848
|
});
|
|
2871
2849
|
};
|
|
@@ -2881,22 +2859,33 @@ class indexedDB {
|
|
|
2881
2859
|
return new Promise((resolve) => {
|
|
2882
2860
|
/* 清空数据库 */
|
|
2883
2861
|
let dbName = that.#dbName;
|
|
2884
|
-
|
|
2885
|
-
if (
|
|
2862
|
+
this.open(function (idbStore) {
|
|
2863
|
+
if (idbStore == null) {
|
|
2886
2864
|
resolve({
|
|
2887
2865
|
success: false,
|
|
2888
|
-
code: that.#
|
|
2889
|
-
msg: that.#
|
|
2866
|
+
code: that.#statusCode.deleteAllFailed.code,
|
|
2867
|
+
msg: that.#statusCode.deleteAllFailed.msg,
|
|
2890
2868
|
});
|
|
2891
2869
|
}
|
|
2892
2870
|
else {
|
|
2893
|
-
|
|
2894
|
-
idbStore.clear();
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2871
|
+
// 清空
|
|
2872
|
+
let operateResult = idbStore.clear();
|
|
2873
|
+
operateResult.onsuccess = function (event) {
|
|
2874
|
+
resolve({
|
|
2875
|
+
success: true,
|
|
2876
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2877
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2878
|
+
event: event,
|
|
2879
|
+
});
|
|
2880
|
+
};
|
|
2881
|
+
operateResult.onerror = function (event) {
|
|
2882
|
+
resolve({
|
|
2883
|
+
success: false,
|
|
2884
|
+
code: that.#statusCode.deleteAllFailed.code,
|
|
2885
|
+
msg: that.#statusCode.deleteAllFailed.msg,
|
|
2886
|
+
event: event,
|
|
2887
|
+
});
|
|
2888
|
+
};
|
|
2900
2889
|
}
|
|
2901
2890
|
}, dbName);
|
|
2902
2891
|
});
|