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