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