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
相关推荐
IAtlantiscsdn42 分钟前
Redis Stack扩展功能没有bug.的程序员1 小时前
Redis 大 Key 与热 Key:生产环境的风险与解决方案王维志1 小时前
LiteDB详解2301_815357701 小时前
parameterType和@Param注解的区别零雲2 小时前
除了缓存,我们还可以用redis做什么?cyforkk2 小时前
MySQL 唯一约束:从基础到实战,解决数据重复的核心工具不想被吃掉氩2 小时前
MySQL的事务特性和高可用架构万添裁2 小时前
关系模型的数据结构「QT(C++)开发工程师」3 小时前
UML | 最好的类图设计工具结合CSDN天启呈现-领路架构师Damon小智3 小时前
玩转ClaudeCode:用Database-MCP实现自然语言操作数据库