IndexedDB的redux封装
对浏览器的IndexedDB插件进行的用于redux的封装。
IndexedDB:https://github.com/duan602728596/IndexedDB
引入方法
;
在使用前,需要注册redux-thunk中间件。
初始化
;; ; const db = name version;
创建action
const db = dbName version; /** * 参数 * @param { string } objectStoreName: ObjectStore名字 * @param { Function } successAction: 成功的Action * @param { Function } failAction : 失败的Action */const getAction = db;
方法
- db.getAction: 查询数据
- db.addAction: 添加数据
- db.putAction: 更新数据
- db.deleteAction: 删除数据
- db.clearAction: 清除数据
- db.cursorAction: 根据索引查询
传递参数{ object }:
- { string } objectStoreName: ObjectStore名字
- { Function } successAction: 成功的Action
- { Function } failAction: 失败的Action
调用dispatch
;;;; const state = { return {};}; const dispatch = action: ; @ { thispropsaction; thispropsaction; thispropsaction; thispropsaction; thispropsaction; thispropsaction; }
在reducer函数内可以获取
{ const result = actionresult; // action.result为数据库获取数据成功后,获得到的数据 // action.xxx // action内也包含你传递的其他数据 } ;
测试用例
运行yarn test
或npm run test
,在浏览器打开http://127.0.0.1:6060
运行测试用例。