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