# aiv367-idb **Repository Path**: magb/aiv367-idb ## Basic Information - **Project Name**: aiv367-idb - **Description**: 一个精致的 IndexDB 库,不要那么复杂,不要那么臃肿,简简单单,灵活好用。 内部采用 Promise 技术对原生 IndexDB 各种回调进行封装,降低了 indexedDB 使用难度, 让你的代码更简洁易读。 - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-04-03 - **Last Updated**: 2023-04-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # aiv367-idb #### 介绍 indexedDB库,包含常用的增删改查方法 #### Gitee [https://gitee.com/aiv367/aiv367-idb](https://gitee.com/aiv367/aiv367-idb) #### 示例 [http://aiv367.gitee.io/aiv367-idb/demo](http://aiv367.gitee.io/aiv367-idb/demo) #### 安装 ``` npm i aiv367-idb --save ``` #### 使用 ##### 初始化创建仓库及索引 ```js 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 }); } } }); ``` ##### 添加一条数据 ```js // 数据不带主键 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)); ``` ##### 添加多条数据 ```js 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)); ``` ##### 修改数据 ```js // 根据数据中主键查找数据并修改数据 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)); ``` ##### 删除数据 ```js // 根据主键删除数据 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)); ``` ##### 获得一条数据 ```js // 根据主键获得数据 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)); ``` ##### 获得多条数据 ```js // 返回全部数据 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)); ``` ##### 使用索引获得数据 ```js 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)); ``` ##### 获得符合条件的数据数量 ```js 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)); ``` ##### 清空一个仓库数据 ```js idb.store('book').clear(); ``` ##### 获得 IDBDatabase 对象 ```js idb.db.then(db => { // db: IDBDatabase }); ``` ##### 删除数据库 ```js idb.deleteDataBase(); //删除当前IDB实例访问的数据库 // or IDB.deleteDataBase('book') //通过IDB的静态方法删除任意数据库 ``` ##### 关闭当前访问的数据库 ```js idb.closeDataBase(); ```