1.0.3 • Published 4 years ago
cindexdb v1.0.3
indexedDB 二次元封装
这是一个对indexedDB进行的二次封装,让你快速的使用indexedDB,并拥有丰富的二次读写接口!
安装
npm install --save-dev cindexdb
or
<script type="module">
import IndexDB from './lib/cindexdb.mjs';
</script>
or
<script src="./lib/cindexdb.js"></script>
- 实例化
// indexedDB 数据库资源json
SiteConfig.OBJECT_STORE = [
{
name: 'projects', // 项目存储
isKey: false, // 将此存储区设为键值存储区,可直接使用get(key)返回值。
keyPath: 'id', // 数据的对象的 key
indexs: [ // 索引
{
name: 'id', // 索引键的名称
keyPath: 'id', // 索引键
unique: false, // 是否允许重复
multiEntry: true // 如果为true,则索引的每个键数组中的每个项目都有一条记录。如果为false,则每个键都是一个数组有一个记录
}
]
}
];
new IndexDB({
DB_NAME: 'chint', // 数据库名称
DB_VERSION: 21, // 数据库版本
OBJECT_STORE: SiteConfig.OBJECT_STORE // 数据 存储对象
});
使用案例展示 ***
var indexDB = new IndexDB({
DB_NAME: 'chint', // 数据库名称
DB_VERSION: new Date().getTime(), // 数据库版本
OBJECT_STORE: SiteConfig.OBJECT_STORE // 数据 存储对象
});
// 向数据库对象中添加或者更新数据
indexDB.made('projects').put(arr|object).then(function(res){
console.log(res)
})
// 根据指定的key获取单条数据, key的入参可以是数组,获取数据list
indexDB.made('projects').get().then(function(res){
console.log(res)
})
// 获取所有的主键 与正式api使用一致
indexDB.made('projects').keys().then(function(res){
console.log(res)
})
// 获取对象存储下所有的数据
indexDB.made('projects').all().then(function(res){
console.log(res)
})
// 获取对象存储下所有的数据条数
indexDB.made('projects').count().then(function(res){
console.log(res)
})
// 迭代数据对象存储,`没有原生js的index属性(下面的迭代同理没有)`
indexDB.made('projects').each(function(item){
console.log(item)
})
// 反向迭代
indexDB.made('projects').reverse(function(item){
console.log(item)
})
// 根据条件进行迭代,有两个参数
// 第一个参数是要迭代的数据条数,默认值是10
// 第二个是要迭代的开始位置,默认值是0;如果大于0,正向迭代,如果小于0,反向迭代
indexDB.made('projects').some(5, -1).then(function(res){
console.log(res)
})
// 获取第一条数据
indexDB.made('projects').first().then(function (res) {
console.log(res)
})
// 获取最后一条数据
indexDB.made('projects').last().then(function (res) {
console.log(res)
})
// 根据索引和键值获取指定的数据
indexDB.made('projects').find('GROUP_SLT_ID', '3770').then(function (res) {
console.log(res)
})
根据索引和键值获取指定的数据
* @param {*索引名称} key
* @param {*索引键值} value
* @param {*比较符号} compare
*
* >: 索引键值大于所传的value的所有数据
* >=: 索引键值大于等于所传的value的所有数据
* <: 索引键值小于所传的value的所有数据
* <=: 索引键值小于等于所传的value的所有数据
* !=: 索引键值不等于所传的value的所有数据
* %: `索引键值包含所传的value的所有数据` ****切记仅限字符串且value也必须是字符串
* in: 索引键值在所传的value中
indexDB.made('projects').query('GROUP_SLT_ID', ['3770'], 'in').then(function (res) {
console.log(res)
})
多条件查询;等同于对 `query`方法的批量操作
但是 ****** 多了一个optional参数, 如果optional参数为true,表示多条件的关系是||,否则是&&;默认值: false
入参的方式是数组入参,参数顺序固定
入参方式1 (包含条件1和条件2的数据)
[
{ key: 'amount', value: 10, compare: '>' },
{ key: 'color', value: 'red' },
]
入参方式2 (包含条件1和条件2且条件1和条件3的数据)
[
{ key: 'amount', value: 10, compare: '>' },
{ key: 'color', value: 'red', optional: true },
{ key: 'color', value: 'blue', optional: true }
]
var conditions = [
{ key: 'SUB_ID', value: 2, optional: true },
// { key: 'GROUP_SLT_ID', value: '4812|3804|23|4813|4820|3783|3785|4818|2422|4819|2352', compare: 'in', optional: true }
]
var paging = {
pages: '',
num: ''
}
indexDB.made('projects').select(conditions, paging).then(function (res) {
console.log(res);
});