browser-localstorge 1.0.0-beta.0
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/LICENSE +21 -0
- package/README.md +291 -0
- package/dist/es/Browser_StoreKit.es.js +662 -0
- package/dist/umd/Browser_StoreKit.umd.js +1 -0
- package/index.js +1 -0
- package/package.json +25 -0
- package/src/constant.js +9 -0
- package/src/db/handle.js +610 -0
- package/src/db/index.js +137 -0
- package/src/index.js +191 -0
- package/src/localAndsession/index.js +130 -0
- package/src/localAndsession/local.js +11 -0
- package/src/localAndsession/session.js +11 -0
- package/src/utils/index.js +87 -0
- package/src/utils/message.js +80 -0
- package/src/utils/storageResponse.js +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2025] [Aurora]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# browser-localstorge
|
|
2
|
+
|
|
3
|
+
1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣
|
|
4
|
+
|
|
5
|
+
## 📌localStorage 存储
|
|
6
|
+
|
|
7
|
+
### 初始化
|
|
8
|
+
|
|
9
|
+
`new BrowserStoreKit(type, options)`
|
|
10
|
+
|
|
11
|
+
| 属性名 | 含义 | 格式、默认值 |
|
|
12
|
+
| :----:| :----: | :----: |
|
|
13
|
+
| type | 存储模块类型 | `string`,目前支持三种模式:LOCAL_STORAGE, SESSION_STORAGE, INDEXED_DB(请从库中导入使用)。 |
|
|
14
|
+
| options | 配置参数 | `object`,根据模式的不同设置项的属性也不同。添加localStorage下的属性,会在LOCAL_STORAGE模式下生效。 |
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
示例参考:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import BrowserStoreKit, { LOCAL_STORAGE, SESSION_STORAGE, INDEXED_DB } from 'browser-localstorage'
|
|
21
|
+
|
|
22
|
+
const localKit = new BrowserStoreKit(LOCAL_STORAGE, {
|
|
23
|
+
// 使用localStorage的时候,这边配置项参数就给属性localStorage赋值即可(session同理)
|
|
24
|
+
localStorage: {
|
|
25
|
+
auto_cover: true,
|
|
26
|
+
auto_parse: true,
|
|
27
|
+
auto_stringify: true,
|
|
28
|
+
debug: false,
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// 实例化之后,就可以执行localKit的方法了
|
|
33
|
+
localKit.setItem(...)
|
|
34
|
+
localKit.getItem(...)
|
|
35
|
+
...
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
下面介绍一下配置项参数的含义:
|
|
39
|
+
|
|
40
|
+
> 目前,**localStorage** 和 **sessionStorage** 的配置项参数是一致的。
|
|
41
|
+
|
|
42
|
+
| 属性名 | 含义 | 格式、默认值 |
|
|
43
|
+
| :----:| :----: | :----: |
|
|
44
|
+
| auto_cover | setItem是否自动将结果覆盖 | `boolean`,默认为`true`。因为localStorage本身的逻辑也是自动覆盖。 |
|
|
45
|
+
| auto_parse | 是否自动将结果反序列化(`JSON.parse`) | `boolean`,默认为`false`。通过`getItem`获取到的数据不会自动被parse |
|
|
46
|
+
| auto_stringify | 是否自动将非字符串的对象序列化(`JSON.stringify`) | `boolean`,默认为`true`。 |
|
|
47
|
+
| debug | 是否开启debug(调试)模式 | `boolean`,默认为`false`。如果打开,则会在控制台输出存储信息。调试信息格式见下。 |
|
|
48
|
+
|
|
49
|
+
控制台输出的调试信息格式大致如下:
|
|
50
|
+
```javascript
|
|
51
|
+
[2025-04-22 10:16:42] 🔗localStorage 📎[getItem]: 数据项获取成功➡️(key: 2)➡️值为{"id":"2","name":"咪咪","age":"3.56"}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
这其实没有什么实质性的大作用,但是console能够给到你一些足够的安全感~
|
|
55
|
+
|
|
56
|
+
### 1️⃣新增 / 修改数据 `setItem`
|
|
57
|
+
|
|
58
|
+
- key:`string`类型,**必填项**,表示键。
|
|
59
|
+
- value:`any`类型,**必填项**,表示存储的值。
|
|
60
|
+
|
|
61
|
+
> 注意:value的类型可以是任意类型,但是在存储的时候可能会自动将其序列化为字符串。
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
localKit.setItem(key, value)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2️⃣删除数据 `removeItem`
|
|
68
|
+
根据键删除数据
|
|
69
|
+
|
|
70
|
+
- key:`string`类型,**必填项**,表示键。
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
localKit.removeItem(key)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 3️⃣查找数据 `getItem`
|
|
77
|
+
根据键查找数据
|
|
78
|
+
|
|
79
|
+
- key:`string`类型,**必填项**,表示键。
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
localKit.getItem(key)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 4️⃣清空数据 `clear`
|
|
86
|
+
清空本地localStorage中的所有数据,无参数。不可撤销请谨慎使用。
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
localKit.clear()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 5️⃣获取数据长度 `length`
|
|
93
|
+
获取localStorage中的所有数据的长度,无参数。
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
localKit.length()
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 📌sessionStorage 存储
|
|
100
|
+
|
|
101
|
+
`sessionStorage`不做过多的赘述,因为它和`localStorage`的使用是**完全一致**的。
|
|
102
|
+
请仔细阅读上面的localStorage文档部分即可。
|
|
103
|
+
|
|
104
|
+
## 📌indexedDB 存储🔥🔥🔥
|
|
105
|
+
|
|
106
|
+
简单介绍一下`indexedDB`的使用方法。它与前面的localStorage和sessionStorage的使用方式是**完全不同**的。因此在使用之前,希望你仔细阅读下面的文档。
|
|
107
|
+
|
|
108
|
+
### 1️⃣初始化
|
|
109
|
+
|
|
110
|
+
首先初始化 `indexedDB`
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
import BrowserStoreKit, { INDEXED_DB } from 'browser-localstorage'
|
|
114
|
+
|
|
115
|
+
const DBKit = new BrowserStoreKit(INDEXED_DB, {
|
|
116
|
+
// 在使用indexedDB的时候,需要配置indexedDB的参数
|
|
117
|
+
indexedDB: {
|
|
118
|
+
databaseName: "testDB",
|
|
119
|
+
tableName: "testTable",
|
|
120
|
+
primaryKey: "id",
|
|
121
|
+
autoIncrement: true,
|
|
122
|
+
auto_cover: false,
|
|
123
|
+
debug: false
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// 这一步十分重要❗❗❗
|
|
128
|
+
// 请注意,因为indexedDB是异步操作,因此在实例化之后需要再执行init方法
|
|
129
|
+
// 这一步保证你的数据库和数据表已经处于打开状态,以此来进行后续的操作
|
|
130
|
+
|
|
131
|
+
await DBKit.init()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
| 属性名 | 含义 | 格式、默认值 |
|
|
135
|
+
| :----:| :----: | :----: |
|
|
136
|
+
| `databaseName` | 数据库名称(用来指定打开的数据库) | `string`,必填。建议尽量保证唯一,纯英文。 |
|
|
137
|
+
| `tableName` | 数据表名称(用来指定打开的数据表) | `string`,必填。同一个数据库下面可以有多张数据表~ |
|
|
138
|
+
| `primaryKey` | 主键(学过数据表的应该都知道) | `string`,必填,默认为`id`。作为数据表的唯一主键,必须保持唯一性。 |
|
|
139
|
+
| `autoIncrement` | 是否自动递增 | `boolean`,默认为`true`。如果打开,每个数据项的主键(默认为id)会保持自动递增。 |
|
|
140
|
+
| `auto_cover` | 是否开启自动覆盖 | `boolean`,默认为`false`。如果打开,当你指定`setItem`并且相同键已经有值的情况下,会提醒警告并停止执行。 |
|
|
141
|
+
| `debug` | 是否开启debug(调试)模式 | `boolean`,默认为`false`。如果打开,则会在控制台输出存储信息。 |
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
请注意 `primaryKey(主键)` 和 `autoIncrement(是否自动递增)` 两者之间的关系:
|
|
146
|
+
|
|
147
|
+
`primaryKey`为必需参数,并且要保证**唯一性**,主要作用如下:
|
|
148
|
+
|
|
149
|
+
1. 在新增数据的时候,作为唯一主键
|
|
150
|
+
2. 在<u>修改、查找、删除数据</u>的时候作为查找线索
|
|
151
|
+
|
|
152
|
+
当你申明了`autoIncrement`参数值为true的时候,这意味着:
|
|
153
|
+
|
|
154
|
+
BrowserStoreKit会在你每次插入数据(`setItem`)的时候,为你的数据增加一个主键(也就是参数primaryKey),这个过程是自动的,且遵循的逻辑是递增(i++)
|
|
155
|
+
|
|
156
|
+
> 例如,当你调用`setItem`新增数据的时候是这样的:
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
await DBKit.setItem({ name: '老王', age: 48 })
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
> 当你后续调用getItem的时候,会拿到`{ id: 2, name: '老王', age: 48 }`
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
> 请注意,indexedDB模式下,所有的操作(下面几种)都是**异步**的,因此在使用的时候需要使用`await`关键字。
|
|
166
|
+
|
|
167
|
+
### 2️⃣新增 / 修改数据 `setItem`
|
|
168
|
+
|
|
169
|
+
`DBKit.setItem(key, value)`
|
|
170
|
+
|
|
171
|
+
- key:`string | number`类型,**必填项**,表示键。
|
|
172
|
+
- value:`Object`类型,**必填项**,表示存储的值。
|
|
173
|
+
|
|
174
|
+
在自增模式下,key可以传入`null`,因为此时会自动生成主键。
|
|
175
|
+
|
|
176
|
+
```javascript
|
|
177
|
+
DBKit.setItem(id, { name, age }).then(r => {
|
|
178
|
+
console.log("-----setItem返回结果-----")
|
|
179
|
+
console.log(r)
|
|
180
|
+
})
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 3️⃣删除数据 `removeItem`
|
|
184
|
+
|
|
185
|
+
- key:`string | number`类型,**必填项**,表示键。
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
DBKit.removeItem(key).then(r => {
|
|
189
|
+
console.log("-----removeItem返回结果-----")
|
|
190
|
+
console.log(r)
|
|
191
|
+
})
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### 4️⃣查找数据 `getItem`
|
|
195
|
+
|
|
196
|
+
- key:`string | number`类型,**必填项**,表示键。
|
|
197
|
+
|
|
198
|
+
```javascript
|
|
199
|
+
DBKit.getItem(key).then(r => {
|
|
200
|
+
console.log("-----getItem返回结果-----")
|
|
201
|
+
console.log(r)
|
|
202
|
+
})
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### 5️⃣清空数据 `clear`
|
|
206
|
+
|
|
207
|
+
无参数,谨慎使用。
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
DBKit.clear().then(r => {
|
|
211
|
+
console.log("-----clear返回结果-----")
|
|
212
|
+
console.log(r)
|
|
213
|
+
})
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### 6️⃣获取数据长度 `length`
|
|
217
|
+
|
|
218
|
+
无参数。
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
DBKit.length().then(r => {
|
|
222
|
+
console.log("-----length返回结果-----")
|
|
223
|
+
console.log(r)
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## 📌批量操作 `BatchProcess`
|
|
228
|
+
|
|
229
|
+
考虑到**indexedDB**存储的特性,在<u>批量处理、数据量比较大</u>的场景下会有比较高的应用频率。因此在原来的基础上,设计了**批量操作的API**,可以对数据进行批量**新增**、**删除**以及**查找**。请注意,批量操作目前仅支持**indexedDB**模式。
|
|
230
|
+
|
|
231
|
+
批量操作统一使用`batchProcess`方法,该方法接受三个参数
|
|
232
|
+
|
|
233
|
+
- `mode`:`<string>`类型:批量操作的模式,目前支持三种模式,分别是新增(`add`)、查找(`get`)以及删除(`delete`)。
|
|
234
|
+
|
|
235
|
+
- `itemList`:`Array<string | number | Object>`类型:批量操作的数据集合。
|
|
236
|
+
|
|
237
|
+
- `options`:`Object`类型:批量操作的配置项。
|
|
238
|
+
|
|
239
|
+
### 批量新增操作 `ADD`
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
let startIndex = 1
|
|
243
|
+
const dataList = new Array(5).fill(0).map((item, index) => {
|
|
244
|
+
return { id: startIndex + index, name: `我是第${startIndex+index}个name`, age: startIndex + index }
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
// 当我们执行批量新增的时候,数组的Item项为Object类型
|
|
248
|
+
// 并且,如果是自增模式下,Item不需要指定主键属性(id),因为会自动生成主键
|
|
249
|
+
// 此示例演示的是,非自增模式下,Item需要指定主键属性(id)
|
|
250
|
+
DBKit.batchProcess("add", dataList).then(r => {
|
|
251
|
+
console.log("-----batchSetItem返回结果-----")
|
|
252
|
+
console.log(r)
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### 批量删除操作 `DELETE`
|
|
257
|
+
|
|
258
|
+
根据主键删除数据
|
|
259
|
+
|
|
260
|
+
```javascript
|
|
261
|
+
// 当我们执行批量新增的时候,数组的Item项为number或string类型
|
|
262
|
+
DBKit.batchProcess("delete", [1, 2, 3, 4, 5]).then(r => {
|
|
263
|
+
console.log("-----batchRemoveItem返回结果-----")
|
|
264
|
+
console.log(r)
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### 批量查询操作 `GET`
|
|
269
|
+
|
|
270
|
+
根据主键查询数据
|
|
271
|
+
|
|
272
|
+
```javascript
|
|
273
|
+
// 当我们执行批量新增的时候,数组的Item项为number或string类型
|
|
274
|
+
DBKit.batchProcess("get", [1, 2, 3, 4, 5]).then(r => {
|
|
275
|
+
console.log("-----batchGetItem返回结果-----")
|
|
276
|
+
console.log(r)
|
|
277
|
+
})
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
# 写在最后💻
|
|
281
|
+
|
|
282
|
+
如果觉得这个库对你有帮助,欢迎给我一个star⭐️⭐️⭐️
|
|
283
|
+
如果有什么问题,欢迎提issue。
|
|
284
|
+
|
|
285
|
+
我会尽力在后面迭代升级中完善这个库,希望能够给你带来帮助~
|
|
286
|
+
|
|
287
|
+
# 版本更新记录📋
|
|
288
|
+
|
|
289
|
+
## 1.0.0 (2025-05)
|
|
290
|
+
|
|
291
|
+
1.0.0 首个正式版上线
|