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