代码封装:
/**
* 封装的方法以及用法
*/
/**
* 创建数据库
* @param dbName 库名
* @param storeName 表名(对象存储区)
* @param version 版本号(根据下面的代码,添加新表时需修改)
* @returns 创建出的数据库对象
*/
export function openDB(dbName: any, storeName: any, version = 1) {
return new Promise((resolve, reject) => {
let indexedDB = window.indexedDB;
if (!indexedDB) {
console.log("你的浏览器不支持IndexedDB");
}
let db;
// 创建或者打开一个数据库
const request = indexedDB.open(dbName, version);
// 打开数据库成功
request.onsuccess = function (event: any) {
db = event.target.result; // 数据库对象
resolve(db);
};
// 打开数据库报错
request.onerror = function (event) {
reject(event);
};
// 数据库创建或升级的时候会触发
request.onupgradeneeded = function (event: any) {
db = event.target.result; // 数据库对象
let objectStore;
if (!db.objectStoreNames.contains(storeName)) {
// 创建一个指定名称的对象存储空间,通过keyPath指定主键;第一个参数是名称 ,第二个参数是配置参数
objectStore = db.createObjectStore(storeName, { keyPath: "key" });
// objectStore.createIndex('name', 'name', { unique: true }) // 创建索引 可以让你搜索任意字段
}
};
});
}
/**
* 新增数据
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param data 存储的数据
* @returns
*/
export function addData(db: any, storeName: any, data: any) {
return new Promise((resolve, reject) => {
let request = db
.transaction([storeName], "readwrite") // 事务对象 指定表格名称和操作模式("只读"或"读写")
.objectStore(storeName) // 仓库对象
.add(data);
request.onsuccess = function (event: any) {
resolve(event);
};
request.onerror = function (event: any) {
throw new Error(event.target.error);
reject(event);
};
});
}
/**
* 通过主键读取数据
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param key key
* @returns
*/
export function getDataByKey(db: any, storeName: any, key: any) {
return new Promise((resolve, reject) => {
let transaction = db.transaction([storeName]); // 事务
let objectStore = transaction.objectStore(storeName); // 仓库对象
let request = objectStore.get(key);
request.onerror = function (event: any) {
reject(event);
};
request.onsuccess = function (event: any) {
resolve(request.result);
};
});
}
/**
* 通过游标读取数据(全部数据)
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @returns
*/
export function cursorGetData(db: any, storeName: any) {
let list: any = [];
let store = db
.transaction(storeName, "readwrite") // 事务
.objectStore(storeName); // 仓库对象
let request = store.openCursor(); // 指针对象
return new Promise((resolve, reject) => {
request.onsuccess = function (e: any) {
let cursor = e.target.result;
if (cursor) {
// 必须要检查
list.push(cursor.value);
cursor.continue(); // 遍历了存储对象中的所有内容
} else {
resolve(list);
}
};
request.onerror = function (e: any) {
reject(e);
};
});
}
/**
* 通过索引读取数据
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param indexName 索引名
* @param indexValue 索引值
* @returns
*/
export function getDataByIndex(db: any, storeName: any, indexName: any, indexValue: any) {
let store = db.transaction(storeName, "readwrite").objectStore(storeName);
let request = store.index(indexName).get(indexValue);
return new Promise((resolve, reject) => {
request.onerror = function (e: any) {
reject(e);
};
request.onsuccess = function (e: any) {
resolve(e.target.result);
};
});
}
/**
* 通过索引和游标查询记录
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param indexName 索引名
* @param indexValue 索引值
* @returns
*/
export function cursorGetDataByIndex(db: any, storeName: any, indexName: any, indexValue: any) {
let list: any = [];
let store = db.transaction(storeName, "readwrite").objectStore(storeName); // 仓库对象
let request = store
.index(indexName) // 索引对象
.openCursor(IDBKeyRange.only(indexValue)); // 指针对象
return new Promise((resolve, reject) => {
request.onsuccess = function (e: any) {
let cursor = e.target.result;
if (cursor) {
list.push(cursor.value);
cursor.continue(); // 遍历了存储对象中的所有内容
} else {
resolve(list);
}
};
request.onerror = function (ev: any) {
reject(ev);
};
});
}
/**
* 更新数据(根据主键来更新,如果主键数据不存在则新建)
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param data 数据
* @returns
*/
export function updateDB(db: any, storeName: any, data: any) {
let request = db
.transaction([storeName], "readwrite") // 事务对象
.objectStore(storeName) // 仓库对象
.put(data);
return new Promise((resolve, reject) => {
request.onsuccess = function (ev: any) {
resolve(ev);
};
request.onerror = function (ev: any) {
resolve(ev);
};
});
}
/**
* 删除数据
* @param db 数据库对象
* @param storeName 表名(对象存储区)
* @param key 主键
* @returns
*/
export function deleteDB(db: any, storeName: any, key: any) {
let request = db.transaction([storeName], "readwrite").objectStore(storeName).delete(key);
return new Promise((resolve, reject) => {
request.onsuccess = function (ev: any) {
resolve(ev);
};
request.onerror = function (ev: any) {
resolve(ev);
};
});
}
/**
* 删除数据库
* @param dbName 数据库名称
* @returns
*/
export function deleteDBAll(dbName: any) {
console.log(dbName);
let deleteRequest = window.indexedDB.deleteDatabase(dbName);
return new Promise((resolve, reject) => {
deleteRequest.onerror = function (event) {
console.log("删除失败");
};
deleteRequest.onsuccess = function (event) {
console.log("删除成功");
};
});
}
/**
* 关闭数据库
* @param db 数据库对象
*/
export function closeDB(db: any) {
db.close();
console.log("数据库已关闭");
}
export default {
openDB,
addData,
getDataByKey,
cursorGetData,
getDataByIndex,
cursorGetDataByIndex,
updateDB,
deleteDB,
deleteDBAll,
closeDB,
};
使用方法:
const test1 = async () => {
const db = await indexedDB.openDB("dbName","tableName");
indexedDB.addData(db,"tableName",{key:"1",value:'2'})
}