@whitesev/utils 2.1.2 → 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 +131 -98
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +131 -98
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +131 -98
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +131 -98
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +131 -98
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +131 -98
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/indexedDB.d.ts +47 -3
- package/package.json +1 -1
- package/src/indexedDB.ts +189 -116
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,76 @@ 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,
|
|
2652
|
+
event: event,
|
|
2653
|
+
});
|
|
2654
|
+
};
|
|
2655
|
+
}
|
|
2656
|
+
}, dbName);
|
|
2657
|
+
});
|
|
2658
|
+
}
|
|
2659
|
+
/**
|
|
2660
|
+
* 判断是否存在该数据
|
|
2661
|
+
* @param key 数据key
|
|
2662
|
+
*/
|
|
2663
|
+
async has(key) {
|
|
2664
|
+
let that = this;
|
|
2665
|
+
return new Promise((resolve) => {
|
|
2666
|
+
let dbName = this.#dbName;
|
|
2667
|
+
this.open(function (idbStore) {
|
|
2668
|
+
/* 判断返回的数据中是否有error字段 */
|
|
2669
|
+
if (idbStore == null) {
|
|
2670
|
+
resolve({
|
|
2671
|
+
success: false,
|
|
2672
|
+
code: that.#statusCode.getFailed.code,
|
|
2673
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2674
|
+
});
|
|
2675
|
+
}
|
|
2676
|
+
else {
|
|
2677
|
+
let request = idbStore.get(key);
|
|
2678
|
+
request.onsuccess = function (event) {
|
|
2679
|
+
/* result 返回的是 {key: string, value: any} */
|
|
2680
|
+
/* 键值对存储 */
|
|
2681
|
+
resolve({
|
|
2682
|
+
success: true,
|
|
2683
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2684
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2685
|
+
event: event,
|
|
2686
|
+
});
|
|
2687
|
+
};
|
|
2688
|
+
request.onerror = function (event) {
|
|
2689
|
+
resolve({
|
|
2690
|
+
success: false,
|
|
2691
|
+
code: that.#statusCode.getFailed.code,
|
|
2692
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2653
2693
|
event: event,
|
|
2654
2694
|
});
|
|
2655
2695
|
};
|
|
@@ -2665,18 +2705,17 @@ System.register('Utils', [], (function (exports) {
|
|
|
2665
2705
|
let that = this;
|
|
2666
2706
|
return new Promise((resolve) => {
|
|
2667
2707
|
let dbName = this.#dbName;
|
|
2668
|
-
this.open(function (idbStore
|
|
2708
|
+
this.open(function (idbStore) {
|
|
2669
2709
|
/* 判断返回的数据中是否有error字段 */
|
|
2670
|
-
if (
|
|
2710
|
+
if (idbStore == null) {
|
|
2671
2711
|
resolve({
|
|
2672
2712
|
success: false,
|
|
2673
|
-
code: that.#
|
|
2674
|
-
msg: that.#
|
|
2713
|
+
code: that.#statusCode.getFailed.code,
|
|
2714
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2675
2715
|
data: void 0,
|
|
2676
2716
|
});
|
|
2677
2717
|
}
|
|
2678
2718
|
else {
|
|
2679
|
-
idbStore = idbStore;
|
|
2680
2719
|
let request = idbStore.get(key);
|
|
2681
2720
|
request.onsuccess = function (event) {
|
|
2682
2721
|
let target = event.target;
|
|
@@ -2687,8 +2726,8 @@ System.register('Utils', [], (function (exports) {
|
|
|
2687
2726
|
if (data) {
|
|
2688
2727
|
resolve({
|
|
2689
2728
|
success: true,
|
|
2690
|
-
code: that.#
|
|
2691
|
-
msg: that.#
|
|
2729
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2730
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2692
2731
|
data: data,
|
|
2693
2732
|
event: event,
|
|
2694
2733
|
result: result,
|
|
@@ -2697,8 +2736,8 @@ System.register('Utils', [], (function (exports) {
|
|
|
2697
2736
|
else {
|
|
2698
2737
|
resolve({
|
|
2699
2738
|
success: false,
|
|
2700
|
-
code: that.#
|
|
2701
|
-
msg: that.#
|
|
2739
|
+
code: that.#statusCode.operationFailed.code,
|
|
2740
|
+
msg: that.#statusCode.operationFailed.msg,
|
|
2702
2741
|
data: void 0,
|
|
2703
2742
|
event: event,
|
|
2704
2743
|
result: result,
|
|
@@ -2706,12 +2745,10 @@ System.register('Utils', [], (function (exports) {
|
|
|
2706
2745
|
}
|
|
2707
2746
|
};
|
|
2708
2747
|
request.onerror = function (event) {
|
|
2709
|
-
// @ts-ignore
|
|
2710
|
-
event.target;
|
|
2711
2748
|
resolve({
|
|
2712
2749
|
success: false,
|
|
2713
|
-
code: that.#
|
|
2714
|
-
msg: that.#
|
|
2750
|
+
code: that.#statusCode.getFailed.code,
|
|
2751
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2715
2752
|
data: void 0,
|
|
2716
2753
|
event: event,
|
|
2717
2754
|
});
|
|
@@ -2722,7 +2759,7 @@ System.register('Utils', [], (function (exports) {
|
|
|
2722
2759
|
}
|
|
2723
2760
|
/**
|
|
2724
2761
|
* 正则获取数据
|
|
2725
|
-
* @param key
|
|
2762
|
+
* @param key 数据key,可以是正则
|
|
2726
2763
|
*/
|
|
2727
2764
|
async regexpGet(key) {
|
|
2728
2765
|
let list = [];
|
|
@@ -2730,46 +2767,45 @@ System.register('Utils', [], (function (exports) {
|
|
|
2730
2767
|
return new Promise((resolve) => {
|
|
2731
2768
|
/* 正则查询 */
|
|
2732
2769
|
let dbName = that.#dbName;
|
|
2733
|
-
|
|
2770
|
+
this.open(function (idbStore) {
|
|
2734
2771
|
/* 判断返回的数据中是否有error字段 */
|
|
2735
|
-
if (
|
|
2772
|
+
if (idbStore == null) {
|
|
2736
2773
|
resolve({
|
|
2737
2774
|
success: false,
|
|
2738
|
-
code: that.#
|
|
2739
|
-
msg: that.#
|
|
2775
|
+
code: that.#statusCode.regexpGetFailed.code,
|
|
2776
|
+
msg: that.#statusCode.regexpGetFailed.msg,
|
|
2740
2777
|
data: [],
|
|
2741
2778
|
});
|
|
2742
2779
|
}
|
|
2743
2780
|
else {
|
|
2744
|
-
idbStore = idbStore;
|
|
2745
2781
|
let request = idbStore.getAll();
|
|
2746
2782
|
request.onsuccess = function (event) {
|
|
2747
2783
|
let target = event.target;
|
|
2748
2784
|
let result = target.result;
|
|
2749
2785
|
if (result.length !== 0) {
|
|
2750
|
-
result.forEach((
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2786
|
+
result.forEach((dataItem, index) => {
|
|
2787
|
+
// 当前项的key
|
|
2788
|
+
let __key = dataItem["key"];
|
|
2789
|
+
// 当前项的value
|
|
2790
|
+
let __value = dataItem["value"];
|
|
2791
|
+
if (__key.match(key)) {
|
|
2792
|
+
list = list.concat(__value);
|
|
2755
2793
|
}
|
|
2756
2794
|
});
|
|
2757
2795
|
}
|
|
2758
2796
|
resolve({
|
|
2759
2797
|
success: true,
|
|
2760
|
-
code: that.#
|
|
2761
|
-
msg: that.#
|
|
2798
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2799
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2762
2800
|
data: list,
|
|
2763
2801
|
event: event,
|
|
2764
2802
|
});
|
|
2765
2803
|
};
|
|
2766
2804
|
request.onerror = function (event) {
|
|
2767
|
-
// @ts-ignore
|
|
2768
|
-
event.target;
|
|
2769
2805
|
resolve({
|
|
2770
2806
|
success: false,
|
|
2771
|
-
code: that.#
|
|
2772
|
-
msg: that.#
|
|
2807
|
+
code: that.#statusCode.getFailed.code,
|
|
2808
|
+
msg: that.#statusCode.getFailed.msg,
|
|
2773
2809
|
data: [],
|
|
2774
2810
|
event: event,
|
|
2775
2811
|
});
|
|
@@ -2780,51 +2816,37 @@ System.register('Utils', [], (function (exports) {
|
|
|
2780
2816
|
}
|
|
2781
2817
|
/**
|
|
2782
2818
|
* 删除数据
|
|
2783
|
-
* @param
|
|
2819
|
+
* @param key 数据key
|
|
2784
2820
|
*/
|
|
2785
2821
|
async delete(key) {
|
|
2786
2822
|
let that = this;
|
|
2787
2823
|
return new Promise((resolve) => {
|
|
2788
2824
|
/* 根据key删除某条数据 */
|
|
2789
2825
|
let dbName = that.#dbName;
|
|
2790
|
-
|
|
2791
|
-
if (
|
|
2826
|
+
this.open(function (idbStore) {
|
|
2827
|
+
if (idbStore == null) {
|
|
2792
2828
|
resolve({
|
|
2793
2829
|
success: false,
|
|
2794
|
-
code: that.#
|
|
2795
|
-
msg: that.#
|
|
2830
|
+
code: that.#statusCode.deleteFailed.code,
|
|
2831
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
2796
2832
|
});
|
|
2797
2833
|
}
|
|
2798
2834
|
else {
|
|
2799
|
-
|
|
2800
|
-
let request = idbStore.
|
|
2835
|
+
// 删除键
|
|
2836
|
+
let request = idbStore.delete(key);
|
|
2801
2837
|
request.onsuccess = function (event) {
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
success: true,
|
|
2809
|
-
code: that.#errorCode.success.code,
|
|
2810
|
-
msg: that.#errorCode.success.msg,
|
|
2811
|
-
});
|
|
2812
|
-
}
|
|
2813
|
-
else {
|
|
2814
|
-
resolve({
|
|
2815
|
-
success: false,
|
|
2816
|
-
code: that.#errorCode.error.code,
|
|
2817
|
-
msg: that.#errorCode.error.msg,
|
|
2818
|
-
});
|
|
2819
|
-
}
|
|
2838
|
+
resolve({
|
|
2839
|
+
success: true,
|
|
2840
|
+
code: that.#statusCode.operationSuccess.code,
|
|
2841
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
2842
|
+
event: event,
|
|
2843
|
+
});
|
|
2820
2844
|
};
|
|
2821
2845
|
request.onerror = function (event) {
|
|
2822
|
-
// @ts-ignore
|
|
2823
|
-
event.target;
|
|
2824
2846
|
resolve({
|
|
2825
2847
|
success: false,
|
|
2826
|
-
code: that.#
|
|
2827
|
-
msg: that.#
|
|
2848
|
+
code: that.#statusCode.deleteFailed.code,
|
|
2849
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
2828
2850
|
event: event,
|
|
2829
2851
|
});
|
|
2830
2852
|
};
|
|
@@ -2840,22 +2862,33 @@ System.register('Utils', [], (function (exports) {
|
|
|
2840
2862
|
return new Promise((resolve) => {
|
|
2841
2863
|
/* 清空数据库 */
|
|
2842
2864
|
let dbName = that.#dbName;
|
|
2843
|
-
|
|
2844
|
-
if (
|
|
2865
|
+
this.open(function (idbStore) {
|
|
2866
|
+
if (idbStore == null) {
|
|
2845
2867
|
resolve({
|
|
2846
2868
|
success: false,
|
|
2847
|
-
code: that.#
|
|
2848
|
-
msg: that.#
|
|
2869
|
+
code: that.#statusCode.deleteAllFailed.code,
|
|
2870
|
+
msg: that.#statusCode.deleteAllFailed.msg,
|
|
2849
2871
|
});
|
|
2850
2872
|
}
|
|
2851
2873
|
else {
|
|
2852
|
-
|
|
2853
|
-
idbStore.clear();
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
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
|
+
};
|
|
2859
2892
|
}
|
|
2860
2893
|
}, dbName);
|
|
2861
2894
|
});
|