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