aomd-web-db
v1.0.1
Published
浏览器端 数据库操作助手
Downloads
1
Readme
webDB
- 简易封装 接口均采用 Promise 一步操作
- 支持 增删改查(以及模糊查询)
- 使用
npm i aomd-web-db
events
| name | Parameters | Description | |:------------------|:----------:|:-----------------------------:| | init | ... | 初始化 | | upgradeneeded | ... | indexedDB 对应的upgradeneeded | | createObjectStore | ... | 创建存储表 | | deleteObjectStore | ... | 删除存储表 | | error | ... | 错误 | | get | ... | 取数据时 | | set | ... | 存数据时 | | del | ... | 删除数据时 | | clear | ... | 清楚表时 | | search | ... | 查询时 | | close | ... | 关闭数据库时 |
methods
| name | Description | |:------------------------------------------------------------------------------------------------------------------------------------------|:----------------------:| | getInstance(option?: WebDBOption): WebDB; | 单例获取 WebDB实例 | | init(): Promise; | 初始化 | | deleteDatabase(name: string): boolean; | 删除 indexedDB 库 | | createObjectStore(tableName: string, option?: CreateObjectStoreOption): IDBObjectStore; | 创建表 | | deleteObjectStore(tableName: string): boolean; | 删除表 | | getObjectStore(tableName: string, type?: transactionMode): IDBObjectStore; | 获取表事务操作 | | getIndexNames(tableName: string): DOMStringList | 获取所有 索引名称 | | saveKey(tableName: string, entity: any, key?: number | string): Promise<ReturnEntity>; | 通过 key 设置数据 | | getByKey(tableName: string, key: number | string): Promise<ReturnEntity>; | 通过 key 获取数据 | | getByIndexName(tableName: string, indexName: string, value: number | string): Promise<ReturnEntity>; | 通过 索引 获取数据 | | getByIndexNameAll(tableName: string, indexName: string, value: number | string, limit?: number): Promise<ReturnEntity>; | 通过 索引 获取所有数据 | | delByKey(tableName: string, key): Promise<ReturnEntity>; | 通过 索引 删除数据 | | clear(tableName: string): Promise<ReturnEntity>; | 清除单一表的所有数据 | | close(): Promise<ReturnEntity>; | 销毁 WebDB 实例 | | fuzzySearch(tableName: string, indexName: string, value: number | string): Promise<ReturnEntity>; | 模糊查询 | | on(eventName: EventName, cb: (en?: ReturnEntity) => unknown): void; | 监听事件 | | off(eventName: EventName, cb: (en?: ReturnEntity) => unknown): void; | 解除监听 |
model
WebDBOption
WebDBOption {
name?: string;
version?: number;
}
CreateObjectStoreOption
CreateObjectStoreOption {
keyPath?: string;
autoIncrement?: boolean;
}
ReturnEntity
ReturnEntity {
// 调用事件
eventName: string;
// 调用表
tableName: string;
// 调用参数
option: object;
// 取出的数据
entity: any;
}
example
//@ts-check
import { WebDB } from '../src/WebDB'
var db = WebDB.getInstance({ name: 'hiwebpage', version: 1 })
// db.deleteDatabase('hiwebpage')
db.on('upgradeneeded', () => {
db.deleteObjectStore("test");
// 创建一个表
var objectStore = db.createObjectStore("test");
// 此处最好取一样名称
objectStore.createIndex("id", 'id')
console.log('upgradeneeded')
})
db.init().then(() => {
// 获取所有索引
db.getIndexNames('test')
// 根据下标进行创建 ro 更新
db.saveKey('test', { name: 'xj', id: 4399 }, '5')
db.saveKey('test', { name: 'xj2', id: 4399 }, '6')
db.saveKey('test', { name: 'xj3', id: 4399 }, '7')
db.saveKey('test', { name: 'xj4', id: '4399' }, '8')
db.saveKey('test', { name: 'xj5', id: '4399' }, '9')
// 更新 9 这个key
db.saveKey('test', { name: 'xj2', id: 'id999' }, '9')
// 自增长
// db.saveKey('test', { id: 'xj1' })
// 通过 key 获取单个
db.getByKey('test', '8').then((data) => {
console.error('getByKey', data.entity);
})
// 通过索引获取 一个 重复多个也取一个
db.getByIndexName('test', 'id', 4399).then((data) => {
console.error('getByIndexName', data.entity);
})
// 通过索引获取多个 limit可以筛选
db.getByIndexNameAll('test', 'id', 4399).then((data) => {
console.error('getByIndexNameAll', data.entity);
})
db.getByIndexNameAll('test', 'id', 4399, 1).then((data) => {
console.error('getByIndexNameAll:limit:1', data.entity);
})
// 模糊查询
db.fuzzySearch('test', 'name', 'x').then((data) => {
console.error('fuzzySearch', data.entity);
})
// 根据key删除
db.delByKey('test', '5').then((data) => {
console.error('delByKey', data.entity);
})
// 清理表
// db.clear('test')
})