1.0.2 • Published 4 years ago
xq-idb v1.0.2
xq-idb
一个简单实用的indexeddb库
安装
可以使用npm进行安装
npm i xq-idb
使用教程
因为indexeddb都是异步操作,所以本项目大量使用了Promise
数据库管理 IDBStore
打开数据库
使用
IDBStore.open(dbName,version) : Promise
方法参数 方法传入参数名称 参数类型 是否必填 dbName 数据库名字 String yes version 数据库版本 Number yes IDBStore.open("mydb",1).then(()=>{ console.log("数据打开成功") }).catch((err)=>{ console.log(err) })
获取数据库实例
使用
IDBStore.database() : StoreTable
方法参数 方法传入参数名称 参数类型 是否必填 dbName 数据库名字 String yes version 数据库版本 Number yes IDBStore.open("mydb",1).then(()=>{ console.log("数据打开成功") }).catch((err)=>{ console.log(err) })
数据表操作 StoreTable
判断数据表是否存在
使用
StoreDatabase.hasTable(dbName) : Boolean
方法参数 方法传入参数名称 参数类型 是否必填 dbName 数据库名字 String yes IDBStore.open("mydb",1).then((res)=>{ res.database().hasTable("table") }).catch((err)=>{ console.log(err) })
创建表
使用
StoreDatabase.createTable(tableOption)
tableOption 参数数组
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
tableName | 表名称 | String | yes |
primary | 主键id | String | yes |
index | 索引 | Array | no |
IDBStore.open("mydb", 1).then((res) => {
res.database().createTable({
tableName: "mydb",
primary: "id",
index: [
{
keyName: "name",
keyPath: "name",
options: {}
}, {
keyName: "email",
keyPath: "email"
}
]
})
}).catch((err) => {
console.log(err)
})
获取表对象
使用
StoreDatabase.table(tableName) : StoreTable
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
tableName | 表名称 | String | yes |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb")
}).catch((err) => {
console.log(err)
})
表对象 StoreTable
添加数据
使用
StoreTable.add(data) : Promise
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
data | 数据 | String,Array | yes |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").add({
id:1,
name:"小王",
email:"aa@aa.com"
})
}).catch((err) => {
console.log(err)
})
设置数据
使用
StoreTable.add(data) : Promise
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
data | 数据 | String,Array | yes |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").put({
id:1,
name:"小红",
email:"aa@aa.com"
})
}).catch((err) => {
console.log(err)
})
主键条件
使用
StoreTable.keyRange(condition,val) : StoreTable
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
condition | 条件 | String,Array | yes |
val | 数据 | Number,Array | yes |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").keyRange(">=",15).get();
res.database().table("mydb").keyRange(">=&&<",[15,20]).get();
}).catch((err) => {
console.log(err)
})
清空数据表
使用
StoreTable.clear() : Promise
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").clear();
}).catch((err) => {
console.log(err)
})
删除数据
使用
StoreTable.delete(data) : Promise
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
data | 删除的数据 | Number,Function | no |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").keyRange(">=",15).delete();
res.database().table("mydb").delete(15);
res.database().table("mydb").delete((val)=>{
return val.id > 12;
});
}).catch((err) => {
console.log(err)
})
获取数据
使用
StoreTable.get(data) : Promise
方法参数 | 方法传入参数名称 | 参数类型 | 是否必填 |
---|---|---|---|
data | 获取的回调函数 | Function | no |
IDBStore.open("mydb", 1).then((res) => {
res.database().table("mydb").keyRange(">=",15).get();
res.database().table("mydb").get((val)=>{
return val.id > 12;
});
}).catch((err) => {
console.log(err)
})