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)
})
}
dexie 前端数据库封装
只有干货2025-07-12 19:41
相关推荐
字节跳动开源1 小时前
OpenViking 上线原生 3D 内容理解模型CCIE-Yasuo3 小时前
网络安全-学习数据库-Mysql-2这个DBA有点耶3 小时前
内核级AI与多模融合:AI数据库在海外场景的落地要点涛思数据(TDengine)3 小时前
从工业本体到 AI 原生:TDengine 出席 2026 QCon 分享工业数据基础设施路径Jesse_EC3 小时前
换乘次数最少的路径 SQL 查询详解与实战经验——从 WITH RECURSIVE 到 pgRouting工控丫丫3 小时前
帆软看板中经常使用的公式函数及经常搭的组合这个DBA有点耶5 小时前
云数据库半夜卡顿,CPU才30%?深挖云盘IOPS突发限制与内核挂起机制无言听风雨丶6 小时前
Redis 运维实战与排错小天源7 小时前
智叟云集系统 · 架构与职责分层herinspace8 小时前
管家婆实用贴-SQL 2008如何修改成混合模式