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