1.基于idb-js进行二次封装
1.1.安装
npm install idb-js --save
1.2.引入
import Idb from 'idb-js' // 引入Idb
1.3.db配置
idb_lab_testing_config.js代码如下
export default {
dbName: "lab_testing",
version: 1,
tables: [
{
tableName: "qc_trf",
option: { keyPath: "id" },
indexs: [
{
key: "id",
options:{
unique: true
}
},
{
key: "trfId",
options:{
unique: true
}
},
{
key: "bno"
},
{
key: "workingNo"
},
{
key: "season"
},
{
key: "article"
},
{
key: "children"
}
]
},
{
tableName: "qc_test_code",
option: { keyPath: "id" },
indexs: [
{
key: "id",
options:{
unique: true
}
},
{
key: "testCode"
}
],
}
]
};
1.4.封装
/**
* IndexedDB数据库操作
* @author max teng
* @version 2021-11-19 11:11:11
*/
import Idb from 'idb-js'
import idb_lab_testing_config from './idb_lab_testing_config.js'
var IndexedDBUtil = {}
IndexedDBUtil.init = Idb(idb_lab_testing_config)
/**
* 关闭数据库
*/
IndexedDBUtil.IdbClose = function (){
IndexedDBUtil.init.then(
db => {
db.close_db();
}
)
}
/**
* 新增数据
* @param {Object} tableName 表名
* @param {Object} data 新增数据
*/
IndexedDBUtil.IdbInsert = function (tableName,data){
IndexedDBUtil.init.then(
db => {
db.insert({
tableName: tableName,
data: data
});
},
err => {
console.log(err);
}
)
}
/**
* 删除达成条件的数据
* @param {Object} tableName 表名
* @param {Object} conditionFun Lambda表达式,删除数据匹配条件
*/
IndexedDBUtil.IdbDelete = function (tableName,conditionFun){
IndexedDBUtil.init.then(
db => {
db.delete({
tableName: tableName,
condition: conditionFun,
success: (res) => {
console.log("删除成功");
}
});
},
err => {
console.log(err);
}
)
}
/**
* 更新数据
* @param {Object} tableName 表名
* @param {Object} conditionFun Lambda表达式,更新数据匹配条件
* @param {Object} handleFun Lambda表达式,更新数据修改方式
*/
IndexedDBUtil.IdbUpdate = function (tableName,conditionFun,handleFun){
IndexedDBUtil.init.then(
db => {
db.update({
tableName: tableName,
condition: conditionFun,
handle: handleFun,
success: r => {
console.log("修改成功", r);
},
error: msg => console.log(msg)
});
},
err => {
console.log(err);
}
)
}
/**
* 根据游标查询
* @param {Object} tableName 表名
* @param {Object} conditionFun Lambda表达式,查询数据匹配条件
*/
IndexedDBUtil.IdbQuery = function (tableName,conditionFun){
let _that = this;
return new Promise(function(resolve, reject) {
IndexedDBUtil.init.then(
db => {
db.query({
tableName: tableName,
condition: conditionFun,
success: res => {
resolve(res)
}
});
}
)
})
}
/**
* 查询表所有的数据
* @param {Object} tableName 表名
*/
IndexedDBUtil.IdbQueryAll = function (tableName){
return new Promise(function(resolve, reject) {
IndexedDBUtil.init.then(
db => {
db.queryAll({
tableName: tableName,
success: res => {
resolve(res)
}
});
},
err => {
console.log(err);
}
)
});
}
/**
* @param {Object} tableName 表名
* @param {Object} indexName 索引字段
* @param {Object} target 查询值
*/
IndexedDBUtil.IdbQueryByIndex = function (tableName,indexName,target){
return new Promise(function(resolve, reject) {
IndexedDBUtil.init.then(
db => {
db.query_by_index({
tableName: tableName,
indexName: indexName,
target: target,
success: res => {
resolve(res)
}
});
},
err => {
console.log(err);
}
)
})
}
export default IndexedDBUtil