yach.util.indexeddb
Advanced tools
Comparing version 0.0.1 to 1.0.0
@@ -6,2 +6,2 @@ module.exports = function (api) { | ||
}; | ||
}; | ||
}; |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib/index.js'); | ||
module.exports = require('./lib/index.js'); |
/** | ||
* 基于dexie封装了下indexedDB的存取 | ||
* 基于localforage封装了下indexedDB的存取 | ||
* @author lcq | ||
*/ | ||
import Dexie from 'dexie'; | ||
import localforage from 'localforage'; | ||
class IndexedDB{ | ||
constructor(tableName, dbName = 'yach.cache'){ | ||
class IndexedDB { | ||
constructor(tableName, dbName = 'yach.cache.new') { | ||
this.tableName = tableName; | ||
this.dbName = dbName; | ||
this.valueKey = 'data'; | ||
this.db = new Dexie(dbName); | ||
this.db.version(2).stores({ [tableName]: `id` }); | ||
this.localforage = localforage; | ||
if(localStorage.getItem('__debugDB')){// 用于调试 | ||
window.db = this.db; | ||
localforage.config({ | ||
//driver : localforage.INDEXEDDB, // Force WebSQL; same as using setDriver() | ||
name : dbName, | ||
version : 2.0, | ||
// size : 4980736, // Size of database, in bytes. WebSQL-only for now. | ||
storeName : tableName, // Should be alphanumeric, with underscores. | ||
description : 'some description' | ||
}); | ||
if (localStorage.getItem('__debugDB')) { | ||
// 用于调试 | ||
window.localforage = localforage; | ||
} | ||
} | ||
setItem(key, value){ | ||
return this.db.table(this.tableName).put({id: key, [this.valueKey]: value}) | ||
async setItem(key, value) { | ||
try { | ||
return await localforage.setItem(key, value); | ||
} catch (error) { | ||
console.error('indexeddb setItem', error); | ||
} | ||
} | ||
async getItem(key){ | ||
const teamCache = await this.db[this.tableName].get({id: key}); | ||
return teamCache ? teamCache[this.valueKey] : null; | ||
async getItem(key) { | ||
try { | ||
const teamCache = await localforage.getItem(key); | ||
return teamCache; | ||
} catch (error) { | ||
console.error('indexeddb getItem', error); | ||
return null; | ||
} | ||
} | ||
deleteItem(key){ | ||
return this.db.table(this.tableName).delete(key); | ||
async deleteItem(key) { | ||
try { | ||
return await localforage.removeItem(key); | ||
} catch (error) { | ||
console.error('indexeddb deleteItem', error); | ||
} | ||
} | ||
clearItem(){ | ||
return this.db.table(this.tableName).clear(); | ||
async clear() { | ||
try { | ||
await localforage.clear(key); | ||
} catch (error) { | ||
console.error('indexeddb clear', error); | ||
} | ||
} | ||
} | ||
export default (tableName, dbName) => new IndexedDB(tableName, dbName); | ||
export default (tableName, dbName) => new IndexedDB(tableName, dbName); |
{ | ||
"name": "yach.util.indexeddb", | ||
"version": "0.0.1", | ||
"license": "MIT", | ||
"description": "封装indexeddb方法 用于yach的缓存", | ||
"main": "index.js", | ||
"author": "Beijing Yidu Huida Education Technology Co., Ltd.", | ||
"scripts": {}, | ||
"dependencies": { | ||
"dexie": "^3.0.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.10.4", | ||
"@babel/core": "^7.10.4", | ||
"@babel/preset-env": "^7.10.4" | ||
} | ||
"name": "yach.util.indexeddb", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"description": "封装indexeddb方法 用于yach的缓存", | ||
"main": "index.js", | ||
"author": "Beijing Yidu Huida Education Technology Co., Ltd.", | ||
"scripts": {}, | ||
"dependencies": { | ||
"localforage": "^1.9.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.10.4", | ||
"@babel/core": "^7.10.4", | ||
"@babel/preset-env": "^7.10.4" | ||
} | ||
} |
# yach.util.indexeddb | ||
封装indexeddb方法 用于yach的缓存 | ||
封装 indexeddb 方法 用于 yach 的缓存 | ||
## Owner | ||
Chuanqing.Li[lichuanqing@tal.com] | ||
## 安装 | ||
```sh | ||
@@ -13,2 +16,3 @@ $ npm i yach.util.indexeddb | ||
## 使用 | ||
```js | ||
@@ -23,4 +27,3 @@ import _yachDB from 'yach.util.indexeddb'; | ||
await yachDB.setItem(key, value); | ||
await yachDb.clear() | ||
await yachDb.clear(); | ||
``` | ||
@@ -31,2 +34,3 @@ | ||
### 0.0.1.2020.10.21 | ||
1. init project |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3912
6
95
1
34
+ Addedlocalforage@^1.9.0
+ Addedimmediate@3.0.6(transitive)
+ Addedlie@3.1.1(transitive)
+ Addedlocalforage@1.10.0(transitive)
- Removeddexie@^3.0.2
- Removeddexie@3.2.7(transitive)