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
相关推荐
pele几秒前
Python Tkinter如何实现组件拖拽交换位置_计算鼠标坐标重排布局hua872224 分钟前
Spring Boot 中使用 @Transactional 注解配置事务管理2301_8166602115 分钟前
CSS实现盒子倒角不规则效果_利用border-radius多个值为什么要做囚徒16 分钟前
MongoDB 设置开机自启李少兄17 分钟前
如何创建MySQL索引2201_7610405917 分钟前
CSS如何根据父级容器宽度调整子项_利用容器查询container选择器cssuNke DEPH20 分钟前
Redis 安装及配置教程(Windows)【安装】weixin_4585801223 分钟前
如何在 Python Fabric 中正确执行 EdgeOS 配置命令m0_7375393724 分钟前
mysql的理论和使用ZC跨境爬虫25 分钟前
3D地球卫星轨道可视化平台开发 Day15(添加卫星系列模糊搜索功能)