yux-storage
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "yux-storage", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "yux-storage 是一个基于 HTML5 IndexedDB 封装的 Web 本地数据离线存储库", | ||
@@ -5,0 +5,0 @@ "main": "yux-storage.js", |
@@ -47,3 +47,3 @@ # yux-storage | ||
```js | ||
import 'yux-storage'; | ||
import yuxStorage from 'yux-storage'; | ||
``` | ||
@@ -74,3 +74,3 @@ | ||
```js | ||
yuxStorage.getItem(key, successCallback) | ||
yuxStorage.getItem(key, callback) | ||
``` | ||
@@ -105,3 +105,3 @@ | ||
```js | ||
yuxStorage.setItem(key, value, successCallback) | ||
yuxStorage.setItem(key, value, callback) | ||
``` | ||
@@ -158,3 +158,3 @@ | ||
```js | ||
yuxStorage.removeItem(key, successCallback) | ||
yuxStorage.removeItem(key, callback) | ||
``` | ||
@@ -176,3 +176,3 @@ | ||
```js | ||
yuxStorage.clear(successCallback) | ||
yuxStorage.clear(callback) | ||
``` | ||
@@ -196,3 +196,3 @@ | ||
```js | ||
yuxStorage.key(keyIndex, successCallback) | ||
yuxStorage.key(keyIndex, callback) | ||
``` | ||
@@ -216,3 +216,3 @@ | ||
```js | ||
yuxStorage.keys(successCallback) | ||
yuxStorage.keys(callback) | ||
``` | ||
@@ -219,0 +219,0 @@ |
@@ -10,22 +10,27 @@ /** | ||
constructor(successCallback) { | ||
constructor() { | ||
this.ready() | ||
} | ||
ready(){ | ||
return new Promise((resolve, reject) => { | ||
this.objectStoreName = 'yux-store'; | ||
const request = window.indexedDB.open('yux-project', 1); | ||
request.onsuccess = (event) => { | ||
this.db = event.target.result; | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(this); | ||
} | ||
if( this.db ){ | ||
resolve(this) | ||
}; | ||
request.onupgradeneeded = (event) => { | ||
const db = event.target.result; | ||
if (!db.objectStoreNames.contains(this.objectStoreName)) { | ||
db.createObjectStore(this.objectStoreName); | ||
}else{ | ||
this.objectStoreName = 'yux-store'; | ||
const request = window.indexedDB.open('yux-project', 1); | ||
request.onsuccess = (event) => { | ||
this.db = event.target.result; | ||
resolve(this) | ||
}; | ||
request.onupgradeneeded = (event) => { | ||
const db = event.target.result; | ||
if (!db.objectStoreNames.contains(this.objectStoreName)) { | ||
db.createObjectStore(this.objectStoreName); | ||
} | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
@@ -35,100 +40,130 @@ }) | ||
setItem(key, value, successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key); | ||
request.onsuccess = (event) => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(value); | ||
setItem(key, value, callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false,value); | ||
} | ||
resolve(value); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true,null); | ||
} | ||
reject(event); | ||
} | ||
resolve(value); | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}) | ||
}) | ||
} | ||
getItem(key, successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key); | ||
request.onsuccess = (event) => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(request.result || null); | ||
getItem(key, callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
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); | ||
} | ||
resolve(request.result || null); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
resolve(request.result || null); | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}) | ||
}) | ||
} | ||
removeItem(key, successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key); | ||
request.onsuccess = (event) => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(equest.result || null); | ||
removeItem(key, callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
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); | ||
} | ||
resolve(); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
resolve(); | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}) | ||
}) | ||
} | ||
key(index, successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(request.result[index] || null); | ||
key(index, callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
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); | ||
} | ||
resolve(request.result[index]); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
resolve(request.result[index]); | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}) | ||
}) | ||
} | ||
keys(successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(request.result || []); | ||
keys(callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result || []); | ||
} | ||
resolve(request.result || []); | ||
}; | ||
request.onerror = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true, null); | ||
} | ||
reject(event); | ||
} | ||
resolve(request.result || []); | ||
}; | ||
request.onerror = (event) => { | ||
reject(event); | ||
} | ||
}) | ||
}) | ||
} | ||
clear(successCallback) { | ||
return new Promise((resolve, reject) => { | ||
const objectStore = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName); | ||
const request = objectStore.getAllKeys(); | ||
request.onsuccess = (event) => { | ||
const p = request.result.map(key => { | ||
return new Promise((resolve, reject) => { | ||
const request = objectStore.delete(key); | ||
request.onsuccess = (event) => { | ||
resolve(); | ||
}; | ||
clear(callback) { | ||
this.ready().then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
const objectStore = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName); | ||
const request = objectStore.getAllKeys(); | ||
request.onsuccess = (event) => { | ||
const p = request.result.map(key => { | ||
return new Promise((resolve, reject) => { | ||
const request = objectStore.delete(key); | ||
request.onsuccess = (event) => { | ||
resolve(); | ||
}; | ||
}) | ||
}) | ||
}) | ||
Promise.all(p).then(() => { | ||
if (successCallback && typeof successCallback === 'function') { | ||
successCallback(); | ||
} | ||
resolve(); | ||
}).catch(event => { | ||
reject(event); | ||
}); | ||
}; | ||
Promise.all(p).then(() => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false); | ||
} | ||
resolve(); | ||
}).catch(event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(true); | ||
} | ||
reject(event); | ||
}); | ||
}; | ||
}) | ||
}) | ||
@@ -139,9 +174,8 @@ } | ||
new yuxDB().then((yuxStorage) => { | ||
window.yuxStorage = yuxStorage; | ||
}) | ||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = yuxDB; | ||
module.exports = new yuxDB(); | ||
} else { | ||
window.yuxStorage = new yuxDB(); | ||
} | ||
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
14012
163