tbdb 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/MongoDB/MongoDBAdapter.d.ts +59 -0
- package/dist/adapter/MongoDB/index.d.ts +1 -0
- package/dist/adapter/MongoDB/lib/docType.d.ts +9 -0
- package/dist/adapter/MongoDB/lib/normalizeProjection.d.ts +13 -0
- package/dist/adapter/MongoDB/lib/normalizeSort.d.ts +3 -0
- package/dist/adapter/MongoDB/test/base.test.d.ts +1 -0
- package/dist/adapter/MongoDB/test/docType.test.d.ts +1 -0
- package/dist/adapter/MongoDB/test/fitler.test.d.ts +1 -0
- package/dist/adapter/MongoDB/test/getTestMongo.d.ts +1 -0
- package/dist/adapter/MongoDB/test/setMany.test.d.ts +1 -0
- package/dist/adapter/MongoDB/test/updateOne.test.d.ts +1 -0
- package/dist/adapter/MongoDB/useMongoDB.d.ts +4 -0
- package/dist/adapter/SQLite/SQLiteAdapter.d.ts +49 -0
- package/dist/adapter/SQLite/index.d.ts +1 -0
- package/dist/adapter/SQLite/test/sqlite-find-sort.test.d.ts +1 -0
- package/dist/adapter/SQLite/test/sqlite-merge-union.test.d.ts +1 -0
- package/dist/adapter/SQLite/test/sqlite-overwrite.test.d.ts +1 -0
- package/dist/adapter/SQLite/test/sqlite-zstd.test.d.ts +1 -0
- package/dist/adapter/SQLite/test/sqlite.test.d.ts +1 -0
- package/dist/adapter/SQLite/utils/matcher.d.ts +9 -0
- package/dist/adapter/SQLite/utils/mongoToSql.d.ts +10 -0
- package/dist/adapter/SQLite/utils/patch.d.ts +10 -0
- package/dist/adapter/SQLite/utils/serializer.d.ts +14 -0
- package/dist/adapter/SQLite/utils/zstdHelper.d.ts +12 -0
- package/dist/adapter/adapter.d.ts +237 -0
- package/dist/benchmark/1-single-crud.bench.d.ts +1 -0
- package/dist/benchmark/2-batch-crud.bench.d.ts +1 -0
- package/dist/benchmark/3-paging.bench.d.ts +1 -0
- package/dist/benchmark/base.try.d.ts +1 -0
- package/dist/benchmark/utils.d.ts +15 -0
- package/dist/core/Table.d.ts +240 -0
- package/dist/core/backup.d.ts +38 -0
- package/dist/core/check.d.ts +28 -0
- package/dist/core/defineTable.d.ts +26 -0
- package/dist/core/event.d.ts +0 -0
- package/dist/core/join.d.ts +23 -0
- package/dist/core/list.d.ts +87 -0
- package/dist/core/schema.d.ts +9 -0
- package/dist/core/types.d.ts +280 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6287 -0
- package/dist/rslib-runtime.js +18 -0
- package/dist/test/backup/backup.try.d.ts +1 -0
- package/dist/test/backup-test.test.d.ts +1 -0
- package/dist/test/bulk-update.test.d.ts +1 -0
- package/dist/test/getTestTable.d.ts +5 -0
- package/dist/test/map-set-types.test.d.ts +1 -0
- package/dist/test/null-undefined-query.test.d.ts +1 -0
- package/dist/test/sort-type.test.d.ts +1 -0
- package/dist/test/table-doc-api.test.d.ts +1 -0
- package/dist/test/table-doc.test.d.ts +1 -0
- package/dist/test/table-filter.test.d.ts +1 -0
- package/dist/test/table-join.test.d.ts +1 -0
- package/dist/test/table-kv.test.d.ts +1 -0
- package/dist/test/table-list.test.d.ts +1 -0
- package/dist/test/table-markdelete.test.d.ts +1 -0
- package/dist/test/table-undfined.test.d.ts +1 -0
- package/dist/test/table-update.test.d.ts +1 -0
- package/dist/test/try/delete.try.d.ts +1 -0
- package/dist/test/try/doc.try.d.ts +5 -0
- package/dist/test/try/getTestUserTable.d.ts +5 -0
- package/dist/test/try/insert.try.d.ts +5 -0
- package/dist/test/try//345/210/233/345/273/272350/344/270/207/346/225/260/346/215/256.d.ts +2 -0
- package/dist/test/try//346/223/215/344/275/234350/344/270/207/346/225/260/346/215/256.d.ts +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { ITableDBAdapterInstance, ITableDBAdapter, ITableDefineIndexesOptions, ITableDeletedResult, ITableDeleteOptions, ITableDoc, ITableFindOptions, ITableIndexConfig, ITableInsertResult, ITableSetOptions, ITableSetResult, ITableUpdateOptions, ITableUpdateResult } from "../adapter/adapter";
|
|
2
|
+
import { exportBinary, exportBinaryToFile, importBinary, importBinaryFromFile } from "./backup";
|
|
3
|
+
import { __check_filter, __check_find_options, __check_input_doc, __check_output_doc, __check_update_op } from "./check";
|
|
4
|
+
import { ICursorPagingOptions, IReCursorPaging, IReSkipPaging, ISkipPagingOptions } from "./list";
|
|
5
|
+
import { __schema_init, ISchemaHints } from "./schema";
|
|
6
|
+
import { ITableFilter, ITableUpdateOp } from "./types";
|
|
7
|
+
export interface ITableOptions<TSchema, TPlv extends IPlvMap = IPlvMap> {
|
|
8
|
+
/** Talbe 名称
|
|
9
|
+
*
|
|
10
|
+
* 会作为存储时的表名或集合名使用
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/** 文档 schema 定义
|
|
14
|
+
*
|
|
15
|
+
* 使用 `fzz dto` 来定义,可以用来校验文档结构,定义索引等
|
|
16
|
+
*/
|
|
17
|
+
schema?: TSchema;
|
|
18
|
+
/** 索引定义
|
|
19
|
+
*
|
|
20
|
+
* 优先级大于 schema 的索引定义
|
|
21
|
+
*/
|
|
22
|
+
indexes?: ITableIndexConfig[];
|
|
23
|
+
/** 指定 Adapter */
|
|
24
|
+
adapter?: ITableDBAdapter;
|
|
25
|
+
/** 是否启用标记删除功能 */
|
|
26
|
+
enableMarkDelete?: boolean;
|
|
27
|
+
/** 是否启用目录树功能 */
|
|
28
|
+
enableTree?: boolean;
|
|
29
|
+
/** 预设投影列表 */
|
|
30
|
+
projections?: TPlv;
|
|
31
|
+
}
|
|
32
|
+
export interface ITableGetBaseOptions {
|
|
33
|
+
/** 是否忽略标记删除的文档 */
|
|
34
|
+
ignoreMarkDelete?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface IPlvMap {
|
|
37
|
+
[plvName: string]: string[] | {
|
|
38
|
+
[field: string]: 1 | -1;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 数据表对象
|
|
43
|
+
* 实现类似 MongoDB 的 NoSQL 数据存储 API
|
|
44
|
+
*
|
|
45
|
+
* 底层可以使用 SQLite, indexedDB, MongoDB
|
|
46
|
+
*/
|
|
47
|
+
export declare class Table<TSchema extends ITableDoc = ITableDoc, TPlv extends IPlvMap = IPlvMap> {
|
|
48
|
+
static globalAdapter?: ITableDBAdapter;
|
|
49
|
+
name: string;
|
|
50
|
+
options: ITableOptions<TSchema, TPlv>;
|
|
51
|
+
adapter: ITableDBAdapterInstance;
|
|
52
|
+
schema: TSchema;
|
|
53
|
+
inited: Promise<boolean>;
|
|
54
|
+
constructor(tableOptions: ITableOptions<TSchema, TPlv>);
|
|
55
|
+
init(): Promise<boolean>;
|
|
56
|
+
__schema_hints: ISchemaHints;
|
|
57
|
+
__schema_init: typeof __schema_init;
|
|
58
|
+
__check_filter: typeof __check_filter;
|
|
59
|
+
__check_output_doc: typeof __check_output_doc;
|
|
60
|
+
__check_find_options: typeof __check_find_options;
|
|
61
|
+
__check_input_doc: typeof __check_input_doc;
|
|
62
|
+
__check_update_op: typeof __check_update_op;
|
|
63
|
+
/** 获取单个文档 */
|
|
64
|
+
get(id: any, options?: ITableGetBaseOptions): Promise<TSchema | void>;
|
|
65
|
+
/** 设置单个文档 */
|
|
66
|
+
set(id: any, doc: Partial<TSchema>): Promise<void>;
|
|
67
|
+
/** 删除单个文档 */
|
|
68
|
+
delete(id: any, options?: ITableDeleteOptions): Promise<void>;
|
|
69
|
+
/** 检查单个文档是否存在 */
|
|
70
|
+
has(id: any, options?: ITableGetBaseOptions): Promise<boolean>;
|
|
71
|
+
/** 统计文档数量 */
|
|
72
|
+
count(filter?: ITableFilter, options?: ITableGetBaseOptions): Promise<number>;
|
|
73
|
+
/** 清空所有文档 */
|
|
74
|
+
clear(): Promise<void>;
|
|
75
|
+
/** 清除所有文档和索引 */
|
|
76
|
+
clearAll(): Promise<void>;
|
|
77
|
+
/** 关闭连接 */
|
|
78
|
+
close(): Promise<void>;
|
|
79
|
+
/** 根据 filter 查找多个文档
|
|
80
|
+
*
|
|
81
|
+
* 可以使用选项中的 `limit`,`offset`, `pageIndex`, `sort` 来分页查询结果,但更
|
|
82
|
+
* 推荐使用 `listPaging` 和 `listIdPaging` 来进行分页查询
|
|
83
|
+
*
|
|
84
|
+
* fitler 用法:
|
|
85
|
+
* 1. 精确匹配: `{ field: value }`, `$eq`
|
|
86
|
+
* 2. 比较操作符:`$gt`, `$gte`, `$lt`, `$lte`, `$ne`
|
|
87
|
+
* 3. 包含操作符: `$in`, `$nin`
|
|
88
|
+
* 4. 逻辑操作符: `$and`, `$or`, `$not`,`$nor`
|
|
89
|
+
* 5. 字符串匹配: `$like`, `$regex`
|
|
90
|
+
* 6. 数组操作: `$elemMatch`, `$all`, `$size`
|
|
91
|
+
* 7. 存在操作: `$exists`
|
|
92
|
+
*/
|
|
93
|
+
findMany(filter: ITableFilter, options?: ITableFindOptions): Promise<TSchema[]>;
|
|
94
|
+
/** 根据 filter 查找单个文档
|
|
95
|
+
* 如果有多个匹配的文档,则只返回第一个匹配的文档
|
|
96
|
+
*
|
|
97
|
+
* fitler 用法:
|
|
98
|
+
* 1. 精确匹配: `{ field: value }`, `$eq`
|
|
99
|
+
* 2. 比较操作符:`$gt`, `$gte`, `$lt`, `$lte`, `$ne`
|
|
100
|
+
* 3. 包含操作符: `$in`, `$nin`
|
|
101
|
+
* 4. 逻辑操作符: `$and`, `$or`, `$not`,`$nor`
|
|
102
|
+
* 5. 字符串匹配: `$like`, `$regex`
|
|
103
|
+
* 6. 数组操作: `$elemMatch`, `$all`, `$size`
|
|
104
|
+
* 7. 存在操作: `$exists`
|
|
105
|
+
*/
|
|
106
|
+
findOne(filter: ITableFilter, options?: ITableFindOptions): Promise<TSchema | void>;
|
|
107
|
+
/** 修改单个现有文档
|
|
108
|
+
*
|
|
109
|
+
* 根据 filter 的匹配第一个文档,再根据 updateOp 进行更新
|
|
110
|
+
*
|
|
111
|
+
* updateOp 用法:
|
|
112
|
+
* 1. 设置字段: `$set`,`$unset`,`$setOnInsert`,`$rename`
|
|
113
|
+
* 2. 数值运算: `$inc`, `$mul`, `$min`, `$max`
|
|
114
|
+
* 3. 数组运算: `$push`, `$pop`, `$addToSet`, `$pull`
|
|
115
|
+
*
|
|
116
|
+
* @param options.upsert 如果没有匹配的文档,是否插入新文档,默认 false
|
|
117
|
+
* @param options.sort 如果 fitler 匹配多个文档,使用 sort 来决定更新哪一个文档
|
|
118
|
+
*
|
|
119
|
+
*/
|
|
120
|
+
updateOne(filter: ITableFilter, updateOp: ITableUpdateOp<TSchema>, options?: ITableUpdateOptions): Promise<ITableUpdateResult>;
|
|
121
|
+
/** 修改多个现有文档
|
|
122
|
+
*
|
|
123
|
+
* 根据 filter 的匹配多个文档,再根据 updateOp 进行更新
|
|
124
|
+
*
|
|
125
|
+
* updateOp 用法:
|
|
126
|
+
* 1. 设置字段: `$set`,`$unset`,`$setOnInsert`,`$rename`
|
|
127
|
+
* 2. 数值运算: `$inc`, `$mul`, `$min`, `$max`
|
|
128
|
+
* 3. 数组运算: `$push`, `$pop`, `$addToSet`, `$pull`
|
|
129
|
+
*
|
|
130
|
+
*
|
|
131
|
+
* @param options.upsert 如果没有匹配的文档,是否插入新文档,默认 false
|
|
132
|
+
*/
|
|
133
|
+
updateMany(filter: ITableFilter, updateOp: ITableUpdateOp<TSchema>, options?: ITableUpdateOptions): Promise<ITableUpdateResult>;
|
|
134
|
+
/**
|
|
135
|
+
* 批量进行多次更新操作
|
|
136
|
+
*/
|
|
137
|
+
bulkUpdate(updates: {
|
|
138
|
+
filter: ITableFilter;
|
|
139
|
+
updateOp: ITableUpdateOp<TSchema>;
|
|
140
|
+
options?: ITableUpdateOptions;
|
|
141
|
+
}[]): Promise<ITableUpdateResult>;
|
|
142
|
+
/** 插入新文档
|
|
143
|
+
*
|
|
144
|
+
* 如果目标文档的 ID 已存在,会忽略,不会进行覆盖,并且继续插入其他文档\n
|
|
145
|
+
* 文档必须有 ID 字段,如果没有 ID 字段会自动生成一个唯一 ID
|
|
146
|
+
*/
|
|
147
|
+
insertMany(docs: Partial<TSchema>[]): Promise<ITableInsertResult>;
|
|
148
|
+
/** 插入单个新文档
|
|
149
|
+
*
|
|
150
|
+
* 如果目标文档的 ID 已存在,会忽略,不会进行覆盖\
|
|
151
|
+
* 相当于调用 `insertMany([doc])`
|
|
152
|
+
*/
|
|
153
|
+
insertOne(doc: Partial<TSchema>): Promise<ITableInsertResult>;
|
|
154
|
+
/** 设置多个文档
|
|
155
|
+
*
|
|
156
|
+
* 根据 doc 的 id 匹配,如果已存在则进行 update 否则进行 insert\
|
|
157
|
+
*
|
|
158
|
+
* @param options.merge 是对存在 doc 进行深度合并而不是浅合并(Object.assign),深度合并时可以使用 `__overwrite__` 标记来强制覆盖子对象
|
|
159
|
+
* @param options.overwrite 是否对 doc 进行覆盖而不是浅合并,把新文档完全替换掉已存在的文档
|
|
160
|
+
* @param options.insertOnly 是否只插入新文档,已存在的文档不进行更新
|
|
161
|
+
* @param options.updateOnly 是否只更新已存在的文档,未存在的文档不进行插入
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
setMany(docs: Partial<TSchema>[], options?: ITableSetOptions): Promise<ITableSetResult>;
|
|
165
|
+
/** 删除多个文档
|
|
166
|
+
*
|
|
167
|
+
* 根据 filter 的匹配结果,可能会删除多个文档
|
|
168
|
+
*
|
|
169
|
+
* fitler 用法:
|
|
170
|
+
* 1. 精确匹配: `{ field: value }`, `$eq`
|
|
171
|
+
* 2. 比较操作符:`$gt`, `$gte`, `$lt`, `$lte`, `$ne`
|
|
172
|
+
* 3. 包含操作符: `$in`, `$nin`
|
|
173
|
+
* 4. 逻辑操作符: `$and`, `$or`, `$not`,`$nor`
|
|
174
|
+
* 5. 字符串匹配: `$like`, `$regex`
|
|
175
|
+
* 6. 数组操作: `$elemMatch`, `$all`, `$size`
|
|
176
|
+
* 7. 存在操作: `$exists`
|
|
177
|
+
*/
|
|
178
|
+
deleteMany(filter: ITableFilter, options?: ITableDeleteOptions): Promise<ITableDeletedResult>;
|
|
179
|
+
/** 删除单个文档
|
|
180
|
+
*
|
|
181
|
+
* 只会删除第一个匹配的文档
|
|
182
|
+
*
|
|
183
|
+
* fitler 用法:
|
|
184
|
+
* 1. 精确匹配: `{ field: value }`, `$eq`
|
|
185
|
+
* 2. 比较操作符:`$gt`, `$gte`, `$lt`, `$lte`, `$ne`
|
|
186
|
+
* 3. 包含操作符: `$in`, `$nin`
|
|
187
|
+
* 4. 逻辑操作符: `$and`, `$or`, `$not`,`$nor`
|
|
188
|
+
* 5. 字符串匹配: `$like`, `$regex`
|
|
189
|
+
* 6. 数组操作: `$elemMatch`, `$all`, `$size`
|
|
190
|
+
* 7. 存在操作: `$exists`
|
|
191
|
+
*
|
|
192
|
+
* @param options.sort 如果 fitler 匹配多个文档,使用 sort 来决定删除哪一个文档
|
|
193
|
+
*/
|
|
194
|
+
deleteOne(filter: ITableFilter, options?: ITableDeleteOptions): Promise<ITableDeletedResult>;
|
|
195
|
+
/** 定义索引
|
|
196
|
+
*
|
|
197
|
+
* 普通索引:\
|
|
198
|
+
* [{ key: "fieldName"}]\
|
|
199
|
+
* 唯一索引:\
|
|
200
|
+
* [{ key: "md5", unique: true }]\
|
|
201
|
+
* 复合索引:\
|
|
202
|
+
* [{ key: { fieldA: 1, 'b.a': -1 }, unique: false }]\
|
|
203
|
+
* 禁用索引:\
|
|
204
|
+
* [{ key: "fieldName", disabled: true }]\
|
|
205
|
+
* 自然排序:\
|
|
206
|
+
* [{ key: "num", naturalSort: true }]\
|
|
207
|
+
*/
|
|
208
|
+
defineIndexes(indexes: ITableIndexConfig[], options?: ITableDefineIndexesOptions): Promise<void>;
|
|
209
|
+
/** 清除所有索引 */
|
|
210
|
+
dropIndexes(): Promise<void>;
|
|
211
|
+
/** 压缩数据文件,清理碎片 */
|
|
212
|
+
compact(): Promise<void>;
|
|
213
|
+
/** 列出分页加载(使用 skip limit) */
|
|
214
|
+
listPaging<T = TSchema>(filter: any, options: ISkipPagingOptions & ITableGetBaseOptions): Promise<IReSkipPaging<T>>;
|
|
215
|
+
/** 列出分页加载(使用 cursor) */
|
|
216
|
+
listPagingByCursor<T = TSchema>(filter: any, options: ICursorPagingOptions): Promise<IReCursorPaging<T>>;
|
|
217
|
+
/** 遍历所有文档,批量处理 */
|
|
218
|
+
eachBatch(filter: any, options: {
|
|
219
|
+
pageSize?: number;
|
|
220
|
+
projection?: any;
|
|
221
|
+
sortKey?: string;
|
|
222
|
+
sortOrder?: 1 | -1;
|
|
223
|
+
} & ITableGetBaseOptions, eachFunc: (list: TSchema[], stop: () => void, batch: number) => Promise<void>): Promise<void>;
|
|
224
|
+
/** 遍历所有文档 */
|
|
225
|
+
forEach<T = TSchema>(filter: any, callbackfn: (value: T, index: number) => Promise<void>): Promise<void>;
|
|
226
|
+
/**
|
|
227
|
+
* 获取预设投影列表
|
|
228
|
+
*/
|
|
229
|
+
plv(plvName: keyof TPlv): string[] | {
|
|
230
|
+
[field: string]: 1 | -1;
|
|
231
|
+
} | undefined;
|
|
232
|
+
/** 导出表数据为二进制格式 */
|
|
233
|
+
exportBinary: typeof exportBinary;
|
|
234
|
+
/** 从二进制格式导入表数据 */
|
|
235
|
+
importBinary: typeof importBinary;
|
|
236
|
+
/** 导出表数据为二进制文件 (Node.js) */
|
|
237
|
+
exportBinaryToFile: typeof exportBinaryToFile;
|
|
238
|
+
/** 从二进制文件导入表数据 (Node.js) */
|
|
239
|
+
importBinaryFromFile: typeof importBinaryFromFile;
|
|
240
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Table } from "./Table";
|
|
2
|
+
export interface IBackupOptions {
|
|
3
|
+
/** 是否压缩备份数据 gzip,默认 true */
|
|
4
|
+
compress?: boolean;
|
|
5
|
+
/** 备份进度回调,百分比 0~100 */
|
|
6
|
+
progress?: (percent: number) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface IImportOptions {
|
|
9
|
+
/** 恢复进度回调,百分比 0~100 */
|
|
10
|
+
progress?: (percent: number) => void;
|
|
11
|
+
/**
|
|
12
|
+
* 是否在导入前清空现有数据,默认 false,即合并导入
|
|
13
|
+
* 如果设置为 true,会先清空表数据和索引,再导入
|
|
14
|
+
*/
|
|
15
|
+
clear?: boolean;
|
|
16
|
+
/** 指定只恢复某些文档的 ID 列表,默认恢复所有文档*/
|
|
17
|
+
docIds?: string[];
|
|
18
|
+
/** 是否忽略索引恢复,默认 false,即恢复索引 */
|
|
19
|
+
ignoreIndexes?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** 备份为 ReadableStream (Web Standard) */
|
|
22
|
+
export declare function exportBinaryStream(this: Table, options?: IBackupOptions): ReadableStream<Uint8Array>;
|
|
23
|
+
/** 从 ReadableStream 恢复 (Web Standard) */
|
|
24
|
+
export declare function importBinaryStream(this: Table, stream: ReadableStream<Uint8Array>, options?: IImportOptions): Promise<{
|
|
25
|
+
docsCount: number;
|
|
26
|
+
}>;
|
|
27
|
+
/** 备份 Table 到的数据和索引配置到二进制格式 (In-Memory) */
|
|
28
|
+
export declare function exportBinary(this: Table, options?: IBackupOptions): Promise<Uint8Array>;
|
|
29
|
+
/** 从二进制格式的数据恢复 Table 数据和索引配置 (In-Memory) */
|
|
30
|
+
export declare function importBinary(this: Table, data: Uint8Array, options?: IImportOptions): Promise<{
|
|
31
|
+
docsCount: number;
|
|
32
|
+
}>;
|
|
33
|
+
/** 备份到文件 (Node.js Stream) */
|
|
34
|
+
export declare function exportBinaryToFile(this: Table, filePath: string, options?: IBackupOptions): Promise<void>;
|
|
35
|
+
/** 从文件恢复 (Node.js Stream) */
|
|
36
|
+
export declare function importBinaryFromFile(this: Table, filePath: string, options?: IImportOptions): Promise<{
|
|
37
|
+
docsCount: number;
|
|
38
|
+
}>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ITableDoc } from "../adapter/adapter";
|
|
2
|
+
import { Table } from "./Table";
|
|
3
|
+
import { ITableFilter, ITableUpdateOp } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* 在 fitler 实际使用前,进行检查和修正
|
|
6
|
+
*/
|
|
7
|
+
export declare function __check_filter(this: Table, filter: ITableFilter, options?: {
|
|
8
|
+
ignoreMarkDelete?: boolean;
|
|
9
|
+
realDelete?: boolean;
|
|
10
|
+
} & Record<string, any>): void;
|
|
11
|
+
/**
|
|
12
|
+
* 在插入或更新文档前,进行检查和修正
|
|
13
|
+
*/
|
|
14
|
+
export declare function __check_input_doc(this: Table, doc: Partial<ITableDoc> | void): void;
|
|
15
|
+
/**
|
|
16
|
+
* 在插入或更新文档前,进行检查和修正
|
|
17
|
+
*/
|
|
18
|
+
export declare function __check_find_options(this: Table, options?: {
|
|
19
|
+
projection?: any;
|
|
20
|
+
}): void;
|
|
21
|
+
/**
|
|
22
|
+
* 在输出文档前,进行检查和修正
|
|
23
|
+
*/
|
|
24
|
+
export declare function __check_output_doc(this: Table, doc: Partial<ITableDoc> | void): void;
|
|
25
|
+
/**
|
|
26
|
+
* 在更新操作前,进行检查和修正
|
|
27
|
+
*/
|
|
28
|
+
export declare function __check_update_op(this: Table, updateOp: ITableUpdateOp | void): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ITableDBAdapter, ITableDoc } from "../adapter/adapter";
|
|
2
|
+
import { ITableOptions, Table } from "./Table";
|
|
3
|
+
/** 定义一个 Table,返回一个 useTable 函数
|
|
4
|
+
*
|
|
5
|
+
* useTable 函数调用后返回表实例(单例)
|
|
6
|
+
*
|
|
7
|
+
* @param tableOptions Table 的配置选项
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
*
|
|
12
|
+
* const useUserTable = defineTable({name: "user"})
|
|
13
|
+
*
|
|
14
|
+
* // 在需要使用的地方
|
|
15
|
+
* const userTable = await useUserTable()
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
* // 如果要指定 adapter
|
|
19
|
+
* const useCustomTable = defineTable({adapter : SQLiteAdapter({filename: ":memory:"})})
|
|
20
|
+
*/
|
|
21
|
+
export declare function defineTable<TSchema extends ITableDoc = ITableDoc>(tableOptions: ITableOptions<TSchema>): UseTalbeFunction<TSchema>;
|
|
22
|
+
export type UseTalbeFunction<TSchema extends ITableDoc = ITableDoc> = (opitons?: {
|
|
23
|
+
adapter?: ITableDBAdapter;
|
|
24
|
+
}) => Promise<Table<TSchema>>;
|
|
25
|
+
/** 定义全局的数据库适配器(SQLite/MongoDB) */
|
|
26
|
+
export declare function defineGlobalDBAdapter(adapter: ITableDBAdapter): void;
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Table } from "./Table";
|
|
2
|
+
export interface ITableJoinOp {
|
|
3
|
+
/** 目标表 */
|
|
4
|
+
table: Table;
|
|
5
|
+
/** 本地键的字段名 */
|
|
6
|
+
localKey: string;
|
|
7
|
+
/** 目标键的字段名 */
|
|
8
|
+
targetKey: string;
|
|
9
|
+
/** 新本地键字段名
|
|
10
|
+
* 默认与 localKey 相同,会覆盖 localKey 字段。\
|
|
11
|
+
* 设置了新本地键字段名后,会新增该字段,保留原 localKey 字段。
|
|
12
|
+
*/
|
|
13
|
+
newLocalKey?: string;
|
|
14
|
+
/** 目标表的投影字段列表 */
|
|
15
|
+
projection?: string[] | Record<string, 1 | -1>;
|
|
16
|
+
}
|
|
17
|
+
/** 连接多个 Table 的文档
|
|
18
|
+
*
|
|
19
|
+
* 根据文档中的 localKey 字段值,去目标表中查找对应的文档,并将目标文档添加到本地文档中。\
|
|
20
|
+
* 支持一对一和一对多(数组)的连接关系。
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
export declare function joinListWithTable(list: any[], joinOps: ITableJoinOp[]): Promise<any[]>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Table } from "./Table";
|
|
2
|
+
import { ITableJoinOp } from "./join";
|
|
3
|
+
import { ITableFilter } from "./types";
|
|
4
|
+
export interface ISkipPagingOptions {
|
|
5
|
+
/** 当前页码 */
|
|
6
|
+
pageIndex?: number;
|
|
7
|
+
/** 每页数量 */
|
|
8
|
+
pageSize?: number;
|
|
9
|
+
/** 投影字段列表
|
|
10
|
+
*
|
|
11
|
+
* 可以是字符串数组,表示包含的字段列表\
|
|
12
|
+
* 也可以是字段映射对象,1 表示包含该字段,-1 表示排除该字段\
|
|
13
|
+
* 不能同时包含和排除字段
|
|
14
|
+
*/
|
|
15
|
+
projection?: string[] | Record<string, 1 | -1>;
|
|
16
|
+
/** 排序字段列表 */
|
|
17
|
+
sort?: any;
|
|
18
|
+
/** 获取总数 */
|
|
19
|
+
getTotal?: boolean;
|
|
20
|
+
/** 排序时是否区分大小写,默认区分 */
|
|
21
|
+
caseSensitive?: boolean;
|
|
22
|
+
/** 强制自然排序 */
|
|
23
|
+
naturalSort?: boolean;
|
|
24
|
+
/** 连接其他表格,根据 result.list 中的字段,把其他表的字典添加到 result */
|
|
25
|
+
join?: ITableJoinOp[];
|
|
26
|
+
}
|
|
27
|
+
export interface IReSkipPaging<T> {
|
|
28
|
+
/** 当前页码 */
|
|
29
|
+
pageIndex: number;
|
|
30
|
+
/** 每页记录数 */
|
|
31
|
+
pageSize: number;
|
|
32
|
+
/** 总记录数,仅当 options.getTotal 为 true 时返回 */
|
|
33
|
+
total?: number;
|
|
34
|
+
/** 是否有下一页 */
|
|
35
|
+
hasNext?: boolean;
|
|
36
|
+
/** 记录列表 */
|
|
37
|
+
list: T[];
|
|
38
|
+
}
|
|
39
|
+
/** 分页列表查询 (skip/limit)
|
|
40
|
+
*
|
|
41
|
+
* 采用分页查询,会排序全部数据然后跳过前面的数据,效率较低,不推荐在大数据量时使用
|
|
42
|
+
*/
|
|
43
|
+
export declare function listPagingBySkip<T>(table: Table, filter: ITableFilter, options: ISkipPagingOptions & {
|
|
44
|
+
ignoreMarkDelete?: boolean;
|
|
45
|
+
}): Promise<IReSkipPaging<T>>;
|
|
46
|
+
export interface ICursorPagingOptions {
|
|
47
|
+
/** 每页数量 */
|
|
48
|
+
pageSize?: number;
|
|
49
|
+
/**
|
|
50
|
+
* 下一页的游标
|
|
51
|
+
* (第一页传 undefined/null)
|
|
52
|
+
*/
|
|
53
|
+
cursor?: any;
|
|
54
|
+
/**
|
|
55
|
+
* 排序字段
|
|
56
|
+
* ⚠️ 注意:游标分页要求排序字段的值必须是唯一的,或者结合 ID 使用。
|
|
57
|
+
* 为了性能和实现简单,这里建议使用具有唯一性的字段(如 id, createdTime(高精度), 或 uuid)
|
|
58
|
+
* 默认为 '_id'
|
|
59
|
+
*/
|
|
60
|
+
sortKey?: string;
|
|
61
|
+
/** 排序方向: 1 升序, -1 降序 (默认 1) */
|
|
62
|
+
sortOrder?: 1 | -1;
|
|
63
|
+
/** 投影字段 */
|
|
64
|
+
projection?: string[] | Record<string, 1 | -1>;
|
|
65
|
+
/** 是否忽略标记删除 */
|
|
66
|
+
ignoreMarkDelete?: boolean;
|
|
67
|
+
/** 连接其他表格,根据 result.list 中的字段,把其他表的字典添加到 result */
|
|
68
|
+
join?: ITableJoinOp[];
|
|
69
|
+
}
|
|
70
|
+
export interface IReCursorPaging<T> {
|
|
71
|
+
/** 记录列表 */
|
|
72
|
+
list: T[];
|
|
73
|
+
/** 下一页的游标 (如果没有下一页则为 null) */
|
|
74
|
+
nextCursor: any | null;
|
|
75
|
+
/** 是否有下一页 */
|
|
76
|
+
hasNext: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 游标分页查询 (Cursor Based)
|
|
80
|
+
*
|
|
81
|
+
* 🚀 高性能分页方案
|
|
82
|
+
*
|
|
83
|
+
* ⚠️ 限制:
|
|
84
|
+
* 1. 只能支持单字段排序(或依赖该字段的唯一性)
|
|
85
|
+
* 2. 无法直接跳转到特定页码(只能“下一页”)
|
|
86
|
+
*/
|
|
87
|
+
export declare function listPagingByCursor<T>(table: Table, filter: ITableFilter, options: ICursorPagingOptions): Promise<IReCursorPaging<T>>;
|