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