yux-storage
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "yux-storage", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "yux-storage 是一个基于 HTML5 IndexedDB 封装的 Web 本地数据离线存储库", | ||
@@ -5,0 +5,0 @@ "main": "yux-storage.js", |
@@ -5,3 +5,2 @@ # yux-storage | ||
## 特点 | ||
@@ -11,9 +10,9 @@ | ||
1. 支持回调和 [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise) 两种方式,各凭所愿。 | ||
1. 非常轻量,100 行左右的源码,压缩后更小。 | ||
1. 无任何依赖,非常轻量,不到 200 行左右的源码,压缩后更小。 | ||
以下是继承 IndexedDB 的特点 | ||
1. 可以存储多种类型的数据,而不仅仅是字符串。 | ||
3. 储存空间大,一般来说不少于 250MB,甚至没有上限。 | ||
2. 异步操作,在进行大量数据存取时不会阻塞应用程序。 | ||
1. 可以存储多种类型的数据,而不仅仅是字符串,无需序列化处理。 | ||
1. 储存空间大,一般来说不少于 250MB,甚至没有上限。 | ||
1. 异步操作,在进行大量数据存取时不会阻塞应用程序。 | ||
@@ -24,3 +23,3 @@ ## 快速开始 | ||
1. 直接在 [github](https://github.com/yued-fe/yux-storage) 获取 yux-storage.js | ||
1. 直接在 [github](https://github.com/yued-fe/yux-storage) 获取 yux-storage.js (推荐) | ||
@@ -78,2 +77,45 @@ ```html | ||
## 错误处理 | ||
一般情况下报错都是参数不合法导致,例如设置添加一个键为`Object`的操作 | ||
```js | ||
DOMException: Failed to execute 'get' on 'IDBObjectStore': The parameter is not a valid key. | ||
``` | ||
> 以下 err 为错误信息 | ||
1. 回调函数直接通过第一个参数判断 | ||
```js | ||
// 回调函数 | ||
yuxStorage.getItem('key',function(err,value){ | ||
if (err) { | ||
console.log('出错了',err) | ||
} else { | ||
console.log(value) | ||
} | ||
}) | ||
``` | ||
2. promsie 可以通过catch来捕获 | ||
```js | ||
yuxStorage.getItem('key').then(function(key) { | ||
console.log(key); | ||
}).catch(err => { | ||
console.log('出错了',err) | ||
}) | ||
``` | ||
3. async/await 可以通过 try...catch 来捕获 | ||
```js | ||
try { | ||
const key = await yuxStorage.getItem('key'); | ||
} catch (error) { | ||
console.log('出错了',err) | ||
} | ||
``` | ||
## API | ||
@@ -89,5 +131,5 @@ | ||
从仓库中获取 key 对应的值并将结果提供给回调函数。如果 `key` 不存在,`getItem()` 将返回 `null`。 | ||
从仓库中获取 key 对应的值并将结果提供给回调函数。如果 `key` 不存在,`getItem()` 将返回 `undefined`。 | ||
> 所有回调函数的第一个参数为**错误标识**,如果为 `true`,说明出错 | ||
> 所有回调函数的第一个参数为**错误信息**,如果为 `false`,说明设置正常 | ||
@@ -139,3 +181,3 @@ *示例* | ||
> 可能不太完整,理论上支持任意格式的数据 | ||
> 可能不太完整,理论上支持任意格式的数据,不能是 function | ||
@@ -150,3 +192,3 @@ *示例* | ||
// 不同于 localStorage,你可以存储非字符串类型 | ||
// 不同于 localStorage,你可以存储非字符串类型(强烈推荐,无需序列化处理) | ||
yuxStorage.setItem('my array', [1, 2, 'three']).then(function(value) { | ||
@@ -157,3 +199,3 @@ // 如下输出 `1` | ||
// 键名也可以是非字符串,比如一个数组 | ||
// 键名也可以是数组或者数字(不推荐,一般用字符串就足够了) | ||
yuxStorage.setItem([1,2,3], [1, 2, 'three']) | ||
@@ -169,2 +211,4 @@ | ||
// 报错,不能是function | ||
yuxStorage.setItem('file', function(){}) | ||
``` | ||
@@ -214,3 +258,3 @@ | ||
根据 key 的索引获取其名 | ||
根据 key 的索引获取其名,如果不存在返回 `undefined` | ||
@@ -234,3 +278,3 @@ > 有些鸡肋的方法,很多时候我们不知道键的索引。 | ||
获取数据仓库中所有的 key。 | ||
获取数据仓库中所有的 key,如果不存在返回空数组`[]`。 | ||
@@ -257,2 +301,2 @@ > localStorage API 并没有这个方法,但比上面的 key 要有用的多。 | ||
有相关问题或者意见可与我联系 yanwenbin@yuewen.com、yanwenbin1991@live.com | ||
有相关问题或者意见可与我联系 yanwenbin@yuewen.com |
@@ -6,2 +6,3 @@ /** | ||
* Created: 20-11-10 | ||
* Update: 21-03-13 | ||
*/ | ||
@@ -41,4 +42,10 @@ | ||
setItem(key, value, callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key); | ||
@@ -51,9 +58,4 @@ request.onsuccess = (event) => { | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
}) | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
@@ -63,18 +65,19 @@ } | ||
getItem(key, callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result || null); | ||
callback(false, request.result); | ||
} | ||
resolve(request.result || null); | ||
resolve(request.result); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
}) | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
@@ -84,18 +87,19 @@ } | ||
removeItem(key, callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, equest.result || null); | ||
callback(false, request.result); | ||
} | ||
resolve(); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
}) | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
@@ -105,18 +109,19 @@ } | ||
key(index, callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result[index] || null); | ||
callback(false, request.result[index]); | ||
} | ||
resolve(request.result[index] || null); | ||
resolve(request.result[index]); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
}) | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
@@ -126,4 +131,10 @@ } | ||
keys(callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
@@ -136,9 +147,4 @@ request.onsuccess = (event) => { | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
}) | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
@@ -148,4 +154,10 @@ } | ||
clear(callback) { | ||
return this.ready().then(() => { | ||
return new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const objectStore = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName); | ||
@@ -167,17 +179,9 @@ const request = objectStore.getAllKeys(); | ||
resolve(); | ||
}).catch(event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true); | ||
} | ||
reject(event); | ||
}); | ||
}).catch(fail); | ||
}; | ||
}) | ||
}).catch(fail) | ||
}) | ||
} | ||
} | ||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
@@ -184,0 +188,0 @@ module.exports = new yuxDB(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14666
169
290