1.0.0 • Published 1 year ago

aiv367-idb v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

aiv367-idb

介绍

indexedDB库,包含常用的增删改查方法

Gitee

https://gitee.com/aiv367/aiv367-idb

示例

http://aiv367.gitee.io/aiv367-idb/demo

安装

npm i aiv367-idb --save

使用

初始化创建仓库及索引
import IDB from 'aiv367-idb';


/*
new IDB('demoDataBase', 1, {
    onupgradeneeded: (evt) => {},
    onsuccess: (evt) => {},
    onerror: (evt) = {},
    onblocked: (evt) => {},
};
*/
let idb = new IDB('demo', 1, {

    onupgradeneeded(evt) {

        const db: IDBDatabase = (evt.target as IDBOpenDBRequest).result;

        if (!db.objectStoreNames.contains('book')) {
            let objectStore = db.createObjectStore('book', { keyPath: 'id', autoIncrement: true });
            objectStore.createIndex('id', 'id', { unique: true });
            objectStore.createIndex('type', 'type', { unique: false });
        }

    }

});
添加一条数据
// 数据不带主键
idb.store('book').add({ name: 'book11', type: 'story' }).then(res => console.log(res));

// 数据中带主键
idb.store('book').add({ id: 1, name: 'book11', type: 'story' }).then(res => console.log(res)); 

// 通过第二个参数传递主键
idb.store('book').add({ name: 'book11', type: 'story' }, 1).then(res => console.log(res)); 
添加多条数据
idb.store('book').adds([
    { name: 'book11', type: 'story' },
    { name: 'book12', type: 'history' },
    { name: 'book13', type: 'history' },
    { name: 'book14', type: 'story' }
]).then(res => console.log(res));
修改数据
// 根据数据中主键查找数据并修改数据
idb.store('book').update({ id: 1, name: 'book22', type: 'story' }).then(res => console.log(res)); 

// 通过第二个参数传递主键,查找要修改的数据
idb.store('book').update({ name: 'book22', type: 'story' }, 1).then(res => console.log(res)); 
删除数据
// 根据主键删除数据
idb.store('book').delete(1).then(res => console.log(res));

// 通过 IDBKeyRange 对象删除数据
// IDBKeyRange API: https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange
idb.store('book').delete(IDBKeyRange.only(1)).then(res => console.log(res)); 

// 通过回调函数删除多个数据, 回调函数返回 true, 该条数据被删除
idb.store('book').delete(data => data.name === 'book2').then(res => console.log(res)); 
获得一条数据
// 根据主键获得数据
idb.store('book').get(1).then(res => console.log(res));

// 通过 IDBKeyRange 对象获得数据
idb.store('book').get(IDBKeyRange.only(1)).then(res => console.log(res)); 

// 通过回调函数获得数据
idb.store('book').get(data => data.name === 'book2').then(res => console.log(res)); 
获得多条数据
// 返回全部数据
idb.store('book').gets().then(res => console.log(res));

// 通过 IDBKeyRange 对象获得多条数据
idb.store('book').gets(IDBKeyRange.bound(1, 10)).then(res => console.log(res)); 

// 通过回调函数获得多条数据
idb.store('book').gets(data => data.id <= 10).then(res => console.log(res)); 
使用索引获得数据
idb.store('book').index('type').gets().then(res => console.log(res));
idb.store('book').index('type').gets(IDBKeyRange.only('story')).then(res => console.log(res)); 
idb.store('book').index('type').gets(data => data.type === 'story').then(res => console.log(res));
获得符合条件的数据数量
idb.store('book').count(IDBKeyRange.only('story')).then(res => console.log(res));
idb.store('book').count(data => data.type === 'story').then(res => console.log(res));
清空一个仓库数据
idb.store('book').clear();
获得 IDBDatabase 对象
idb.db.then(db => {
    // db: IDBDatabase
});
删除数据库
idb.deleteDataBase(); //删除当前IDB实例访问的数据库
// or
IDB.deleteDataBase('book') //通过IDB的静态方法删除任意数据库
关闭当前访问的数据库
idb.closeDataBase();