dexie 前端数据库封装

复制代码
import Dexie from "dexie";

/**
 *
 * @param dbName 数据库名称
 * @param storeName 对象存储区域
 * @param keyPath 主键
 * @param indexConfig 索引配置
 * @returns {Dexie}
 */
export function createDB(dbName, storeName, keyPath, indexConfig) {
  const db = new Dexie(dbName)
  // 定义对象存储区域
  db.version(1).stores({
    [storeName]: keyPath
  })
  // 添加索引或其他配置
  if (indexConfig) {
    Object.keys(indexConfig).forEach(indexName => {
      const indexProperties = indexConfig[indexName];
      db[storeName].createIndex(indexName, indexProperties.keyPath, {unique: indexProperties.unique});
    })
  }
  return db
}

/**
 * 添加数据
 * @param db 数据库实例
 * @param storeName 对象存储区域
 * @param data 添加的数据
 */
export function addData(db, storeName, data) {
  db.open().then(() => {
    db.transaction('rw', db[storeName], async () => {
      await db[storeName].add(data);
    }).then(() => {
      console.log('Data added successfully.');
    }).catch(error => {
      console.error('Error adding data:', error)
    })
  }).catch(error => {
    console.error('Error opening database:', error)
  })
}

/**
 * 获取数据
 * @param db 数据库
 * @param storeName 对象存储区对象
 * @param keyValue 主键值
 */
export function getData(db, storeName, keyValue) {
  return new Promise((resolve, reject) => {
    db.open().then(() => {
      db.transaction('rw', db[storeName], async () => {
        const matched = await db[storeName].get(keyValue)
        resolve(matched)
      }).catch(error => {
        console.error('Error getting data:', error)
      })
    }).catch(error => {
      console.error('Error opening database:', error)
    })
  })
}

/**
 * 更新数据
 * @param db 数据库
 * @param storeName 对象存储区对象
 * @param keyValue 主键值
 * @param data 更新值
 */
export function updateData(db, storeName, keyValue, data) {
  db.open().then(() => {
    db[storeName].get(keyValue).then(matched => {
      if (matched) {
        db.transaction('rw', db[storeName], async () => {
          await db[storeName].update(keyValue, data)
        }).then(() => {
          console.log('Data updated successfully.');
        }).catch(error => {
          console.error('Error updating data:', error)
        })
      } else {
        addData(db, storeName, data)
      }
    }).catch(error => {
      console.error('Error getting data:', error)
    })
  }).catch(error => {
    console.error('Error opening database:', error)
  })
}

/**
 * 删除数据
 * @param db
 * @param storeName
 * @param keyValue
 */
export function deleteData(db, storeName, keyValue) {
  db.open().then(() => {
    db.transaction('rw', db[storeName], async () => {
      await db[storeName].delete(keyValue)
    }).then(() => {
      console.log('Data deleted successfully.');
    }).catch(error => {
      console.error('Error deleting data:', error)
    })
  }).catch(error => {
    console.error('Error opening database:', error)
  })
}

/**
 * 删除数据库
 * @param dbName
 */
export function deleteDb(dbName) {
  Dexie.exists(dbName).then(exists => {
    // console.log(exists)
    if (exists) {
      Dexie.delete(dbName).then(() => {
        console.log('Database deleted successfully.')
      }).catch(error => {
        console.error('Error deleting database', error)
      })
    } else {
      console.log(`Database 【${dbName}】 not exist.`)
    }
  }).catch(error => {
    console.error('Error checking database', error)
  })
}
相关推荐
多弗朗皮卡丘12 小时前
MySQL表的约束
数据库·mysql
Flynn Liang16 小时前
AI Agent Redis 生产踩坑实录:13 类故障的根因、复现与调优
数据库·人工智能·redis·缓存·ai agent·故障分析
SelectDB17 小时前
开源日志平台怎么选?ELK、Loki、ClickHouse、Apache Doris 四大方案对比与选型指南
数据库
SelectDB17 小时前
ELK 磁盘空间不足怎么办?Elasticsearch 存储膨胀原因、排查步骤与 3 种降本方案
数据库
满小满17 小时前
为什么我先 ANALYZE,而不是先加索引
数据库·dba
SelectDB18 小时前
日志分析用什么数据库好?Elasticsearch、ClickHouse、Doris 选型对比与 3 个企业案例
数据库
inferno20 小时前
十三、存储过程(PROCEDURE)
数据库·sql·mysql
数据库急诊室_阿哲20 小时前
一周慢查询日志审计:1200 条里有多少真问题
运维·数据库·dba
zhangjw3421 小时前
2-4 集合运算 union /union all /intersect/except
数据库·postgresql
Decay__21 小时前
PostgreSQL DBA 日常运维手册 —— 会话、锁阻塞、慢查询、膨胀与每日巡检
运维·数据库·sql·postgresql