xdriver
v2.0.21
Published
A simple driver for IndexDB
Readme
说明
初始化Connection
import {KeyRange, Driver} from 'xdriver';
// 初始化数据库驱动
const driver = new Driver('demo', version);
// 定义表结构以及初始化数据
driver.defineTables({
name: 'player',
primaryKey: 'id',
autoIncrement: true,
indexes: [
{name: 'age_index', column: 'age'},
{name: 'name_index', column: 'name'},
{name: 'double_index', column: ['sex', 'age']},
],
rows: [{name: 'xxx', age: '', sex: ''}] //初始化数据
});
const connect = await driver.connect();查询操作
const player = connect.table('player');
let results = await player.queryByIndex('age_index', KeyRange.between(10, 20));
results = await player.queryByIndex('double_index', KeyRange.eq(['女3', 0]));
results = await player.query(KeyRange.between(10, 20))
results = connect.queryByIndex('tableName', 'indexName', KeyRange.between(10, 20))API
Connection
/**
* 查询数据
* @param table 表名
* @param query 查询条件
* @param count 查询数量
*/
query<R extends Row>(table: string, query?: IDBValidKey | IDBKeyRange, count: number = 1000): Promise<RowPacket<R>>;
/**
* 查询数据
* @param table 表名
* @param key 查询条件
* @param index 索引名称
* @param count 查询数量
*/
select<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, index?: string, count: number = 1000): Promise<RowPacket<R>>;
/**
* 获取数据
* @param table 表名
* @param key 查询条件
*/
queryByKey<R extends Row>(table: string, key: IDBValidKey | IDBKeyRange): Promise<RowPacket<R>>;
/**
*
* @param table
* @param indexName 索引名称
* @param key key or keyRange (对应的是索引包含的列的数据)
* @param count 返回的列数
*/
async queryByIndex<R extends Row>(table: string, indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<R>>;
/**
* 获取数据
* @param table
* @param id
* @param indexName
*/
async queryById<R extends Row>(table: string, id: any | Array<any>, indexName?: string): Promise<R>;
/**
* 获取数据
* @param table 表名
* @param key 查询条件
*/
count(table: string, key: IDBValidKey | IDBKeyRange);
/**
* 插入数据
* @param table
* @param data
*/
insert(table: string, data: Array<Record<string, any>>);
/**
* 更新数据
* @param table 表名
* @param modify 更新内容
* @param key 条件
*/
update(table: string, modify: Row, key?: IDBValidKey | IDBKeyRange | null);
/**
* 删除数据
* @param table 表名
* @param key 索引名称
*/
delete(table: string, key: IDBValidKey | IDBKeyRange);
/**
* 清空表
* @param table 表名
*/
truncate(table: string);
/**
* 获取内存分页模型对象
* @param table
* @param pageSize
*/
getPagination<R extends Row>(table: string, pageSize: number = 10): Promise<Pagination<R>>;
/**
* 扫描数据
* @param table 表名
* @param key key or keyRange (对应的是主键的数据)
* @param direction 游标方向
* @param indexName 索引名称
*/
async scan<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<R>>;
/**
* 获取所有数据
* @param table
*/
getAllData<R extends Row>(table: string): Promise<Array<R>>;
/**
* 分页查询
* @param table
* @param pageNo 页码
* @param pageSize 每页数量
* @param key (对应的是主键的数据)
* @param indexName 索引名称
*/
async paginate<R extends Row>(table: string, pageNo: number = 1, pageSize: number = 10, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>>;