UNPKG

@ostore/db

Version:

基于文件系统的轻量级 NoSQL 数据库【Lightweight file-based NoSQL database】

158 lines (153 loc) 4.23 kB
interface ICopyFileResult { code: 200 | 500; data?: IDocument[]; error?: any; } interface IReadDatabaseOptions { _$id?: number; [key: string]: any; } interface IDocumentData { _$id: number; [key: string]: any; } interface IFindResult { code: 200 | 500; data?: IDocumentData[]; count: number; error?: any; } interface IWriteDatabaseResult { code: 200 | 500; error?: any; doc?: IDocument; } interface IWriteDatabaseOptions { copy?: boolean; } interface IDocument { _$status: number; _$id: number; content: any; } interface IDBConfig { base?: string; model: string; } interface IQueryParams { [key: string]: any; } interface IFindOptions { skip?: number; limit?: number; sort?: number; } interface IUpdateResult { code: 200 | 500; status?: boolean; errlist?: any[]; doc?: IDocument; error?: any; } interface IInternalFindResult { code: 200 | 500; data: IDocument[]; count: number; error?: any; } declare const TOP_LEVEL_FIELDS: readonly ["_$id", "_$status"]; declare class FileHandler { protected model: string; protected databasePath: string; constructor(config: IDBConfig); /** * 初始化数据库目录 */ private initDatabase; /** * 复制文件 */ protected copyFile(source: string, target: string): Promise<ICopyFileResult>; /** * 删除文件 */ protected unlinkFile(target: string): Promise<ICopyFileResult>; /** * 读取数据库文档 */ protected readDatabase(options?: IReadDatabaseOptions): Promise<ICopyFileResult>; /** * 写入数据库文档 */ protected writeDatabase(data: any, options?: IWriteDatabaseOptions): Promise<IWriteDatabaseResult>; /** * 批量写入数据库(并发优化) */ protected writeDatabaseBatch(dataList: any[], options?: IWriteDatabaseOptions): Promise<{ successCount: number; errorList: any[]; }>; } declare class Document implements IDocument { _$status: number; _$id: number; content: any; constructor(data: IDocument | any, isExisting?: boolean); } declare class DB extends FileHandler { private _id; constructor(config: IDBConfig); /** * 创建文档 */ create(data: IQueryParams): Promise<IWriteDatabaseResult>; /** * 内部查询方法(返回未扁平化的 IDocument[]) */ private _findInternal; /** * 查询文档(公开方法,保持向后兼容) */ find(params: IQueryParams, options?: IFindOptions): Promise<ICopyFileResult | IFindResult>; /** * 查询单个文档 */ findOne(query: IQueryParams, options?: IFindOptions): Promise<IFindResult>; /** * 查询所有文档 */ findMany(query: IQueryParams, options?: IFindOptions): Promise<IFindResult>; /** * 更新单个文档 * @param target - 更新的数据 * @param source - 查询条件(可选,默认使用 target) */ updateOne(target: IQueryParams, source?: IQueryParams): Promise<IUpdateResult>; /** * 批量更新文档 * @param target - 更新的数据 * @param source - 查询条件(可选,默认使用 target) */ updateMany(target: IQueryParams, source?: IQueryParams): Promise<IUpdateResult>; /** * 删除单个文档(软删除) */ removeOne(query: IQueryParams, options?: IFindOptions): Promise<IUpdateResult>; /** * 批量删除文档(软删除) */ removeMany(query: IQueryParams): Promise<IUpdateResult>; /** * 获取文档字段值(兼容顶层字段和 content 字段) */ private getDocumentFieldValue; /** * 构建查询条件(过滤 undefined 值) */ private buildQuery; /** * 文档排序 */ private sortDocuments; } export { DB, Document, FileHandler, type ICopyFileResult, type IDBConfig, type IDocument, type IDocumentData, type IFindOptions, type IFindResult, type IInternalFindResult, type IQueryParams, type IReadDatabaseOptions, type IUpdateResult, type IWriteDatabaseOptions, type IWriteDatabaseResult, TOP_LEVEL_FIELDS };