TableHelper.js 959 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import DB from './DB'
  2. class TableHelper {
  3. constructor (tableName) {
  4. this.tableName = tableName
  5. }
  6. async getObjTable () {
  7. return DB.init(this.tableName)
  8. }
  9. async replaceObject (obj) {
  10. let objTable = await this.getObjTable()
  11. if (!objTable) return
  12. objTable.put(obj)
  13. }
  14. async replaceObjects (list) {
  15. let objTable = await this.getObjTable()
  16. if (!objTable) return
  17. // 解密数据
  18. for (let key in list) {
  19. objTable.put(list[key])
  20. }
  21. }
  22. async updateObject (newObject, where) {
  23. let objTable = await this.getObjTable()
  24. if (!objTable) return
  25. for (let key in where) {
  26. let obj = null
  27. if (key == objTable.keyPath) {
  28. obj = await objTable.get(where[key])
  29. } else {
  30. obj = await objTable.index(key).get(where[key])
  31. }
  32. if (obj) {
  33. obj = Object.assign(obj, newObject)
  34. objTable.put(obj)
  35. }
  36. break
  37. }
  38. }
  39. }
  40. export default TableHelper