indexedDB数据库实践

此生再无相见时 提交于 2020-01-19 03:54:16
export default {
  indexedDB: window.indexedDB || window.webkitindexedDB || window.msIndexedDB,

  openDb(dbName, version = 1, db, newStore, cb) {
    const request = this.indexedDB.open(dbName, version);
    request.onerror = function(e) {
      console.error("打开数据库错误", e);
    };
    request.onsuccess = function(e) {
      console.log("打开数据库成功", e);
      db = e.target.result;
      if (cb) cb(db);
    };

    // 建表
    request.onupgradeneeded = function(e) {
      var mdb = e.target.result;
      console.log(mdb);
      if (!mdb.objectStoreNames.contains(newStore.name)) {
        const objStore = mdb.createObjectStore(newStore.name, {
          // keyPath: newStore.key
          autoIncrement: true
        });
        const tableMap = {
          name: "",
          amount: 0,
          platform: "",
          num: 0,
          money: 0,
          date: "",
          method: 0
        };
        Object.keys(tableMap).forEach(key =>
          objStore.createIndex(key, k
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!