indexdb参考vue源码抽成对象使用

本文介绍了一个基于IndexDB的JavaScript类库实现,用于在浏览器环境中进行数据存储操作。包括数据库的打开、升级、读写及数据的增删改查等功能。

项目中用到了indexDB,做个笔记

const defaultOption = {
  name: 'test',
  version: 1,
  storeName: 'today'
}

function IndexedDB(option) {
  const indexdb = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB
  if (!indexdb) {
    console.warn('indexdb does not support in this browser!')
    return
  }
  this.indexedDB = indexdb
  this._init(option)
}

IndexedDB.prototype._init = function(option) {
  option = option || defaultOption
  this.dbName = option.name
  this.dbVersion = option.version
  this.storeName = option.storeName
  this.db = null
}

IndexedDB.prototype.open = function() {
  const request = this.indexedDB.open(this.dbName, this.dbVersion)
  const vm = this
  request.onerror = function(e) {
    console.log(e.currentTarget.error.message)
  }
  request.onsuccess = function(e) {
    vm.db = e.target.result
  }
  request.onupgradeneeded = function(e) {
    const db = e.target.result
    if (!db.objectStoreNames.contains(vm.storeName)) {
      // 创建object store
      const store = db.createObjectStore(vm.storeName, {
        keyPath: 'name'
      })
      // 创建索引
      // store.createIndex('indexname', 'attr', {
      //  unique: false
      // })
      vm.db = db
    }
  }
}

function getStore(vm) {
  // 事物只有一次,每次需要重新获得事务
  const transaction = vm.db.transaction(vm.storeName, 'readwrite')
  const store = transaction.objectStore(vm.storeName)
  return store
}

function getStoreReadOnly(vm) {
  // 事物只有一次,每次需要重新获得事务
  const transaction = vm.db.transaction(vm.storeName)
  const store = transaction.objectStore(vm.storeName)
  return store
}

IndexedDB.prototype.addData = function(data) {
  getStore(this).add(data)
}

IndexedDB.prototype.deleteDataByKey = function(key) {
  getStore(this).delete(key)
}

IndexedDB.prototype.updateData = function(data) {
  const request = getStore(this).put(data)
  request.onsuccess = (e) => {
    console.log('成功')
  }
}

IndexedDB.prototype.getDataByIndex = function(indexes) {
  const index = getStoreReadOnly(this).index(indexes.index)
  const request = index.openCursor(IDBKeyRange.only(indexes.value))
  request.onsuccess = (e) => {
    const cursor = e.target.result
    if (cursor) {
      const res = cursor.value
      console.log(res)
      cursor.continue()
    } else {
      console.log('没有了')
    }
  }
}
//TODO 这个方法是不是可以更简单点
IndexedDB.prototype.getDataByKey = function(key) {
  const request = getStoreReadOnly(this).get(key)
  request.onsuccess = (e) => {
    const res = e.target.result
    if (!res) {
      console.log('没有匹配的数据')
    } else {
      console.log(res)
    }
  }
}

IndexedDB.prototype.clearObjectStore = function() {
  getStore(this).clear()
}

IndexedDB.prototype.closeDB = function() {
  this.db.close()
}
IndexedDB.prototype.deleteDB = function() {
  this.indexedDB.deleteDatabase(this.name)
}

export default IndexedDB
//TODO 需要返回值的怎么进行包装
// 用久了vue 连this 的作用域都没想到,耽误了好长时间。。。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值