import LionDB from "../index"; import Schema from "./schema"; import { EventEmitter } from "eventemitter3"; export declare type Index = { name: string; fields: string[]; }; declare type EventType = { /** 清空数据库 */ clear: () => void; /** 构建索引完成 */ index: (data: { table: string; total: number; ttl: number; }) => void; }; export declare class Model extends EventEmitter { private static _app; private static _Readys; readonly masterdb: LionDB; readonly indexdb: LionDB; readonly table: string; readonly indexs: Index[]; private SchemaClass; /** * 构造 * @param opts * @param opts.table 表名 * @param opts.indexs 索引 * @param opts.SchemaClass Schema类 */ constructor(opts: { readonly table: string; readonly indexs: Index[]; readonly SchemaClass: any; }); static setApp(app: string): void; static get app(): string; static onReady(handle: () => void): void; /** * 检测定义是否合法 */ private checkDefine; /** * 初始化数据库 * @returns */ private initDB; protected toSchema(data: { [key: string]: any; }): T; /** * 生成索引 key * @param args */ protected indexKey(indexName: string, ...args: string[]): string; /** * 生成主key * @param id */ protected masterKey(id: string): string; get(id: string): Promise; gets(...ids: string[]): Promise; /** * 统计 * @param opts * @param opts.id: 根据主键id查询 * @param opts.index: 根据自定义索引查询 * @param opts.filter: 查询过滤方法,异步, async(item, key) => Promise * @returns */ count(opts: { id?: string; index?: Index; filter?: (entity: T, key: string) => Promise; }): Promise; /** * 查询 * @param opts * @param opts.id: 根据主键id查询 * @param opts.index: 根据自定义索引查询 * @param opts.start: 开始位置 * @param opts.limit: 查询结果限制条数 * @param opts.reverse: 是否倒序,默认true * @param opts.filter: 查询过滤方法,异步, async(item, key) => Promise * @returns */ find(opts: { id?: string; index?: Index; start?: number; limit?: number; reverse?: boolean; filter?: (entity: T, key: string) => boolean | Promise; }): Promise; findOne(opts: { id?: string; index?: Index; start?: number; limit?: number; filter?: (entity: T, key: string) => Promise; }): Promise; insert(data: T): Promise; /** * 保存数据 * @deprecated * @param data * @param ttl */ create(data: T): Promise; /** * 更新数据 * @param id * @param data * @returns */ update(id: string, data: { [key: string]: any; }): Promise; /** * 保存数据, 不存在就插入新的 * @param data * @param ttl */ save(id: string, data: { [key: string]: any; }): Promise; /** * 删除字段信息 * @param id * @param indexs */ delete(...ids: string[]): Promise; /** * 保存索引 * @param data * @param ttl */ protected saveIndexs(data: T, indexs?: Index[]): Promise; protected deleteIndexs(id: string | T, indexs?: Index[]): Promise; clear(): Promise; private patch; valid(data: any): true | undefined; } export default Model;