monsqlize 1.1.8 → 1.2.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/CHANGELOG.md +3 -1
- package/lib/connect.js +99 -99
- package/lib/mongodb/common/agg-pipeline.js +4 -1
- package/lib/mongodb/queries/find-page.js +44 -4
- package/lib/multi-level-cache.js +39 -8
- package/lib/redis-cache-adapter.js +29 -0
- package/package.json +121 -121
- package/types/collection.ts +357 -357
- package/types/pagination.ts +5 -0
package/types/collection.ts
CHANGED
|
@@ -1,357 +1,357 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Collection API 相关类型定义
|
|
3
|
-
* @module types/collection
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
FindOptions,
|
|
8
|
-
CountOptions,
|
|
9
|
-
AggregateOptions,
|
|
10
|
-
DistinctOptions,
|
|
11
|
-
} from "./query";
|
|
12
|
-
import type {
|
|
13
|
-
InsertOneSimplifiedOptions,
|
|
14
|
-
InsertOneOptions,
|
|
15
|
-
InsertOneResult,
|
|
16
|
-
InsertManySimplifiedOptions,
|
|
17
|
-
InsertManyOptions,
|
|
18
|
-
InsertManyResult,
|
|
19
|
-
} from "./write";
|
|
20
|
-
import type {
|
|
21
|
-
InsertBatchOptions,
|
|
22
|
-
InsertBatchResult,
|
|
23
|
-
UpdateBatchOptions,
|
|
24
|
-
UpdateBatchResult,
|
|
25
|
-
DeleteBatchOptions,
|
|
26
|
-
DeleteBatchResult,
|
|
27
|
-
} from "./batch";
|
|
28
|
-
import type {
|
|
29
|
-
FindPageOptions,
|
|
30
|
-
PageResult,
|
|
31
|
-
ResultWithMeta,
|
|
32
|
-
MetaOptions,
|
|
33
|
-
BookmarkKeyDims,
|
|
34
|
-
PrewarmBookmarksResult,
|
|
35
|
-
ListBookmarksResult,
|
|
36
|
-
ClearBookmarksResult,
|
|
37
|
-
} from "./pagination";
|
|
38
|
-
import type { StreamOptions, ExplainOptions } from "./stream";
|
|
39
|
-
import type { FindChain, AggregateChain } from "./chain";
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 健康视图
|
|
43
|
-
*/
|
|
44
|
-
export interface HealthView {
|
|
45
|
-
status: "up" | "down";
|
|
46
|
-
connected: boolean;
|
|
47
|
-
defaults?: any;
|
|
48
|
-
cache?: any;
|
|
49
|
-
driver?: { connected: boolean };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Collection 访问器接口
|
|
54
|
-
* 提供所有 Collection 操作方法
|
|
55
|
-
*/
|
|
56
|
-
export interface CollectionAccessor<TSchema = any> {
|
|
57
|
-
/**
|
|
58
|
-
* 获取命名空间信息
|
|
59
|
-
*/
|
|
60
|
-
getNamespace(): { iid: string; type: string; db: string; collection: string };
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 删除集合
|
|
64
|
-
*/
|
|
65
|
-
dropCollection(): Promise<boolean>;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 创建集合
|
|
69
|
-
*/
|
|
70
|
-
createCollection(name?: string | null, options?: any): Promise<boolean>;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 创建视图
|
|
74
|
-
*/
|
|
75
|
-
createView(
|
|
76
|
-
viewName: string,
|
|
77
|
-
source: string,
|
|
78
|
-
pipeline?: any[],
|
|
79
|
-
): Promise<boolean>;
|
|
80
|
-
|
|
81
|
-
// ============================================================================
|
|
82
|
-
// 查询方法
|
|
83
|
-
// ============================================================================
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 查询单个文档
|
|
87
|
-
* 支持 meta 参数和泛型
|
|
88
|
-
*/
|
|
89
|
-
findOne<T = TSchema>(
|
|
90
|
-
query?: any,
|
|
91
|
-
options?: Omit<FindOptions, "meta">,
|
|
92
|
-
): Promise<T | null>;
|
|
93
|
-
findOne<T = TSchema>(
|
|
94
|
-
query: any,
|
|
95
|
-
options: FindOptions & { meta: true | MetaOptions },
|
|
96
|
-
): Promise<ResultWithMeta<T | null>>;
|
|
97
|
-
findOne<T = TSchema>(
|
|
98
|
-
query?: any,
|
|
99
|
-
options?: FindOptions,
|
|
100
|
-
): Promise<T | null | ResultWithMeta<T | null>>;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 通过 _id 查询单个文档(便利方法)
|
|
104
|
-
* @param id - 文档的 _id(字符串会自动转换为 ObjectId)
|
|
105
|
-
* @param options - 查询选项
|
|
106
|
-
*/
|
|
107
|
-
findOneById(
|
|
108
|
-
id: string | any,
|
|
109
|
-
options?: Omit<FindOptions, "meta">,
|
|
110
|
-
): Promise<any | null>;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 批量通过 _id 查询多个文档(便利方法)
|
|
114
|
-
* @param ids - _id 数组
|
|
115
|
-
* @param options - 查询选项
|
|
116
|
-
*/
|
|
117
|
-
findByIds(
|
|
118
|
-
ids: Array<string | any>,
|
|
119
|
-
options?: {
|
|
120
|
-
projection?: Record<string, any>;
|
|
121
|
-
sort?: Record<string, 1 | -1>;
|
|
122
|
-
cache?: number;
|
|
123
|
-
maxTimeMS?: number;
|
|
124
|
-
comment?: string;
|
|
125
|
-
preserveOrder?: boolean;
|
|
126
|
-
},
|
|
127
|
-
): Promise<any[]>;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 查询多个文档
|
|
131
|
-
* 支持 meta 参数和链式调用
|
|
132
|
-
*/
|
|
133
|
-
find<T = TSchema>(query?: any): FindChain<T>;
|
|
134
|
-
find<T = TSchema>(
|
|
135
|
-
query: any,
|
|
136
|
-
options: FindOptions & { meta: true | MetaOptions },
|
|
137
|
-
): Promise<ResultWithMeta<T[]>>;
|
|
138
|
-
find<T = TSchema>(
|
|
139
|
-
query?: any,
|
|
140
|
-
options?: FindOptions,
|
|
141
|
-
): Promise<T[]> | FindChain<T> | ResultWithMeta<T[]>;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* 计数
|
|
145
|
-
* 支持 meta 参数
|
|
146
|
-
*/
|
|
147
|
-
count(query?: any, options?: Omit<CountOptions, "meta">): Promise<number>;
|
|
148
|
-
count(
|
|
149
|
-
query: any,
|
|
150
|
-
options: CountOptions & { meta: true | MetaOptions },
|
|
151
|
-
): Promise<ResultWithMeta<number>>;
|
|
152
|
-
count(
|
|
153
|
-
query?: any,
|
|
154
|
-
options?: CountOptions,
|
|
155
|
-
): Promise<number | ResultWithMeta<number>>;
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* 聚合查询
|
|
159
|
-
* 支持 meta 参数和链式调用
|
|
160
|
-
*/
|
|
161
|
-
aggregate<T = TSchema>(pipeline?: any[]): AggregateChain<T>;
|
|
162
|
-
aggregate<T = TSchema>(
|
|
163
|
-
pipeline: any[],
|
|
164
|
-
options: AggregateOptions & { meta: true | MetaOptions },
|
|
165
|
-
): Promise<ResultWithMeta<T[]>>;
|
|
166
|
-
aggregate<T = TSchema>(
|
|
167
|
-
pipeline?: any[],
|
|
168
|
-
options?: AggregateOptions,
|
|
169
|
-
): Promise<T[]> | AggregateChain<T> | ResultWithMeta<T[]>;
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 去重查询
|
|
173
|
-
* 支持 meta 参数
|
|
174
|
-
*/
|
|
175
|
-
distinct<T = any>(
|
|
176
|
-
field: string,
|
|
177
|
-
query?: any,
|
|
178
|
-
options?: Omit<DistinctOptions, "meta">,
|
|
179
|
-
): Promise<T[]>;
|
|
180
|
-
distinct<T = any>(
|
|
181
|
-
field: string,
|
|
182
|
-
query: any,
|
|
183
|
-
options: DistinctOptions & { meta: true | MetaOptions },
|
|
184
|
-
): Promise<ResultWithMeta<T[]>>;
|
|
185
|
-
distinct<T = any>(
|
|
186
|
-
field: string,
|
|
187
|
-
query?: any,
|
|
188
|
-
options?: DistinctOptions,
|
|
189
|
-
): Promise<T[] | ResultWithMeta<T[]>>;
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* 流式查询
|
|
193
|
-
* 返回 Node.js 可读流
|
|
194
|
-
*/
|
|
195
|
-
stream(query?: any, options?: StreamOptions): NodeJS.ReadableStream;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* 查询执行计划诊断
|
|
199
|
-
*/
|
|
200
|
-
explain(query?: any, options?: ExplainOptions): Promise<any>;
|
|
201
|
-
|
|
202
|
-
// ============================================================================
|
|
203
|
-
// 分页相关
|
|
204
|
-
// ============================================================================
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 深度分页
|
|
208
|
-
*/
|
|
209
|
-
findPage<T = TSchema>(options: FindPageOptions): Promise<PageResult<T>>;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Bookmark 维护 APIs
|
|
213
|
-
*/
|
|
214
|
-
prewarmBookmarks(
|
|
215
|
-
keyDims: BookmarkKeyDims,
|
|
216
|
-
pages: number[],
|
|
217
|
-
): Promise<PrewarmBookmarksResult>;
|
|
218
|
-
listBookmarks(keyDims?: BookmarkKeyDims): Promise<ListBookmarksResult>;
|
|
219
|
-
clearBookmarks(keyDims?: BookmarkKeyDims): Promise<ClearBookmarksResult>;
|
|
220
|
-
|
|
221
|
-
// ============================================================================
|
|
222
|
-
// 写入操作
|
|
223
|
-
// ============================================================================
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* 插入单个文档
|
|
227
|
-
* 支持简化调用和完整配置
|
|
228
|
-
*/
|
|
229
|
-
insertOne<T = TSchema>(
|
|
230
|
-
document: T,
|
|
231
|
-
options?: InsertOneSimplifiedOptions,
|
|
232
|
-
): Promise<InsertOneResult>;
|
|
233
|
-
insertOne(options: InsertOneOptions): Promise<InsertOneResult>;
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* 插入多个文档
|
|
237
|
-
* 支持简化调用和完整配置
|
|
238
|
-
*/
|
|
239
|
-
insertMany<T = TSchema>(
|
|
240
|
-
documents: T[],
|
|
241
|
-
options?: InsertManySimplifiedOptions,
|
|
242
|
-
): Promise<InsertManyResult>;
|
|
243
|
-
insertMany(options: InsertManyOptions): Promise<InsertManyResult>;
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Upsert 单个文档(存在则更新,不存在则插入)
|
|
247
|
-
* @param filter - 查询条件
|
|
248
|
-
* @param update - 更新内容
|
|
249
|
-
* @param options - 操作选项
|
|
250
|
-
*/
|
|
251
|
-
upsertOne(
|
|
252
|
-
filter: Record<string, any>,
|
|
253
|
-
update: Record<string, any>,
|
|
254
|
-
options?: {
|
|
255
|
-
maxTimeMS?: number;
|
|
256
|
-
comment?: string;
|
|
257
|
-
},
|
|
258
|
-
): Promise<{
|
|
259
|
-
acknowledged: boolean;
|
|
260
|
-
matchedCount: number;
|
|
261
|
-
modifiedCount: number;
|
|
262
|
-
upsertedId?: any;
|
|
263
|
-
upsertedCount: number;
|
|
264
|
-
}>;
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* 原子递增/递减字段值(便利方法)
|
|
268
|
-
* @param filter - 查询条件
|
|
269
|
-
* @param field - 字段名或字段-增量对象
|
|
270
|
-
* @param increment - 增量(默认 1,负数为递减)
|
|
271
|
-
* @param options - 操作选项
|
|
272
|
-
*/
|
|
273
|
-
incrementOne(
|
|
274
|
-
filter: Record<string, any>,
|
|
275
|
-
field: string | Record<string, number>,
|
|
276
|
-
increment?: number,
|
|
277
|
-
options?: {
|
|
278
|
-
returnDocument?: "before" | "after";
|
|
279
|
-
projection?: Record<string, any>;
|
|
280
|
-
maxTimeMS?: number;
|
|
281
|
-
comment?: string;
|
|
282
|
-
},
|
|
283
|
-
): Promise<{
|
|
284
|
-
acknowledged: boolean;
|
|
285
|
-
matchedCount: number;
|
|
286
|
-
modifiedCount: number;
|
|
287
|
-
value: any | null;
|
|
288
|
-
}>;
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* 使缓存失效
|
|
292
|
-
*/
|
|
293
|
-
invalidate(
|
|
294
|
-
op?: "find" | "findOne" | "count" | "findPage" | "aggregate" | "distinct",
|
|
295
|
-
): Promise<number>;
|
|
296
|
-
|
|
297
|
-
// ============================================================================
|
|
298
|
-
// 批量操作
|
|
299
|
-
// ============================================================================
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* 大批量插入(自动分批+重试)
|
|
303
|
-
* @since v1.0.0
|
|
304
|
-
*/
|
|
305
|
-
insertBatch<T = TSchema>(
|
|
306
|
-
documents: T[],
|
|
307
|
-
options?: InsertBatchOptions,
|
|
308
|
-
): Promise<InsertBatchResult>;
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* 批量更新文档(流式查询+分批更新)
|
|
312
|
-
* @since v1.0.0
|
|
313
|
-
*/
|
|
314
|
-
updateBatch(
|
|
315
|
-
filter: Record<string, any>,
|
|
316
|
-
update: Record<string, any>,
|
|
317
|
-
options?: UpdateBatchOptions,
|
|
318
|
-
): Promise<UpdateBatchResult>;
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* 批量删除文档(流式查询+分批删除)
|
|
322
|
-
* @since v1.0.0
|
|
323
|
-
*/
|
|
324
|
-
deleteBatch(
|
|
325
|
-
filter: Record<string, any>,
|
|
326
|
-
options?: DeleteBatchOptions,
|
|
327
|
-
): Promise<DeleteBatchResult>;
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* 查询文档并返回总数(便利方法)
|
|
331
|
-
* @since v1.0.0
|
|
332
|
-
*/
|
|
333
|
-
findAndCount<T = TSchema>(
|
|
334
|
-
filter?: Record<string, any>,
|
|
335
|
-
options?: FindOptions,
|
|
336
|
-
): Promise<{ documents: T[]; total: number }>;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Collection 类型别名(与 CollectionAccessor 等价)
|
|
341
|
-
* @since v1.0.4
|
|
342
|
-
*/
|
|
343
|
-
export type Collection<TSchema = any> = CollectionAccessor<TSchema>;
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* 数据库访问器
|
|
347
|
-
*/
|
|
348
|
-
export type DbAccessor = {
|
|
349
|
-
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
350
|
-
db(dbName: string): {
|
|
351
|
-
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
352
|
-
};
|
|
353
|
-
/** `db()` 的别名,语义更清晰:`app.db.use('logs')` */
|
|
354
|
-
use(dbName: string): {
|
|
355
|
-
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
356
|
-
};
|
|
357
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Collection API 相关类型定义
|
|
3
|
+
* @module types/collection
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
FindOptions,
|
|
8
|
+
CountOptions,
|
|
9
|
+
AggregateOptions,
|
|
10
|
+
DistinctOptions,
|
|
11
|
+
} from "./query";
|
|
12
|
+
import type {
|
|
13
|
+
InsertOneSimplifiedOptions,
|
|
14
|
+
InsertOneOptions,
|
|
15
|
+
InsertOneResult,
|
|
16
|
+
InsertManySimplifiedOptions,
|
|
17
|
+
InsertManyOptions,
|
|
18
|
+
InsertManyResult,
|
|
19
|
+
} from "./write";
|
|
20
|
+
import type {
|
|
21
|
+
InsertBatchOptions,
|
|
22
|
+
InsertBatchResult,
|
|
23
|
+
UpdateBatchOptions,
|
|
24
|
+
UpdateBatchResult,
|
|
25
|
+
DeleteBatchOptions,
|
|
26
|
+
DeleteBatchResult,
|
|
27
|
+
} from "./batch";
|
|
28
|
+
import type {
|
|
29
|
+
FindPageOptions,
|
|
30
|
+
PageResult,
|
|
31
|
+
ResultWithMeta,
|
|
32
|
+
MetaOptions,
|
|
33
|
+
BookmarkKeyDims,
|
|
34
|
+
PrewarmBookmarksResult,
|
|
35
|
+
ListBookmarksResult,
|
|
36
|
+
ClearBookmarksResult,
|
|
37
|
+
} from "./pagination";
|
|
38
|
+
import type { StreamOptions, ExplainOptions } from "./stream";
|
|
39
|
+
import type { FindChain, AggregateChain } from "./chain";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 健康视图
|
|
43
|
+
*/
|
|
44
|
+
export interface HealthView {
|
|
45
|
+
status: "up" | "down";
|
|
46
|
+
connected: boolean;
|
|
47
|
+
defaults?: any;
|
|
48
|
+
cache?: any;
|
|
49
|
+
driver?: { connected: boolean };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Collection 访问器接口
|
|
54
|
+
* 提供所有 Collection 操作方法
|
|
55
|
+
*/
|
|
56
|
+
export interface CollectionAccessor<TSchema = any> {
|
|
57
|
+
/**
|
|
58
|
+
* 获取命名空间信息
|
|
59
|
+
*/
|
|
60
|
+
getNamespace(): { iid: string; type: string; db: string; collection: string };
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 删除集合
|
|
64
|
+
*/
|
|
65
|
+
dropCollection(): Promise<boolean>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 创建集合
|
|
69
|
+
*/
|
|
70
|
+
createCollection(name?: string | null, options?: any): Promise<boolean>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 创建视图
|
|
74
|
+
*/
|
|
75
|
+
createView(
|
|
76
|
+
viewName: string,
|
|
77
|
+
source: string,
|
|
78
|
+
pipeline?: any[],
|
|
79
|
+
): Promise<boolean>;
|
|
80
|
+
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// 查询方法
|
|
83
|
+
// ============================================================================
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 查询单个文档
|
|
87
|
+
* 支持 meta 参数和泛型
|
|
88
|
+
*/
|
|
89
|
+
findOne<T = TSchema>(
|
|
90
|
+
query?: any,
|
|
91
|
+
options?: Omit<FindOptions, "meta">,
|
|
92
|
+
): Promise<T | null>;
|
|
93
|
+
findOne<T = TSchema>(
|
|
94
|
+
query: any,
|
|
95
|
+
options: FindOptions & { meta: true | MetaOptions },
|
|
96
|
+
): Promise<ResultWithMeta<T | null>>;
|
|
97
|
+
findOne<T = TSchema>(
|
|
98
|
+
query?: any,
|
|
99
|
+
options?: FindOptions,
|
|
100
|
+
): Promise<T | null | ResultWithMeta<T | null>>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 通过 _id 查询单个文档(便利方法)
|
|
104
|
+
* @param id - 文档的 _id(字符串会自动转换为 ObjectId)
|
|
105
|
+
* @param options - 查询选项
|
|
106
|
+
*/
|
|
107
|
+
findOneById(
|
|
108
|
+
id: string | any,
|
|
109
|
+
options?: Omit<FindOptions, "meta">,
|
|
110
|
+
): Promise<any | null>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 批量通过 _id 查询多个文档(便利方法)
|
|
114
|
+
* @param ids - _id 数组
|
|
115
|
+
* @param options - 查询选项
|
|
116
|
+
*/
|
|
117
|
+
findByIds(
|
|
118
|
+
ids: Array<string | any>,
|
|
119
|
+
options?: {
|
|
120
|
+
projection?: Record<string, any>;
|
|
121
|
+
sort?: Record<string, 1 | -1>;
|
|
122
|
+
cache?: number;
|
|
123
|
+
maxTimeMS?: number;
|
|
124
|
+
comment?: string;
|
|
125
|
+
preserveOrder?: boolean;
|
|
126
|
+
},
|
|
127
|
+
): Promise<any[]>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 查询多个文档
|
|
131
|
+
* 支持 meta 参数和链式调用
|
|
132
|
+
*/
|
|
133
|
+
find<T = TSchema>(query?: any): FindChain<T>;
|
|
134
|
+
find<T = TSchema>(
|
|
135
|
+
query: any,
|
|
136
|
+
options: FindOptions & { meta: true | MetaOptions },
|
|
137
|
+
): Promise<ResultWithMeta<T[]>>;
|
|
138
|
+
find<T = TSchema>(
|
|
139
|
+
query?: any,
|
|
140
|
+
options?: FindOptions,
|
|
141
|
+
): Promise<T[]> | FindChain<T> | ResultWithMeta<T[]>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 计数
|
|
145
|
+
* 支持 meta 参数
|
|
146
|
+
*/
|
|
147
|
+
count(query?: any, options?: Omit<CountOptions, "meta">): Promise<number>;
|
|
148
|
+
count(
|
|
149
|
+
query: any,
|
|
150
|
+
options: CountOptions & { meta: true | MetaOptions },
|
|
151
|
+
): Promise<ResultWithMeta<number>>;
|
|
152
|
+
count(
|
|
153
|
+
query?: any,
|
|
154
|
+
options?: CountOptions,
|
|
155
|
+
): Promise<number | ResultWithMeta<number>>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 聚合查询
|
|
159
|
+
* 支持 meta 参数和链式调用
|
|
160
|
+
*/
|
|
161
|
+
aggregate<T = TSchema>(pipeline?: any[]): AggregateChain<T>;
|
|
162
|
+
aggregate<T = TSchema>(
|
|
163
|
+
pipeline: any[],
|
|
164
|
+
options: AggregateOptions & { meta: true | MetaOptions },
|
|
165
|
+
): Promise<ResultWithMeta<T[]>>;
|
|
166
|
+
aggregate<T = TSchema>(
|
|
167
|
+
pipeline?: any[],
|
|
168
|
+
options?: AggregateOptions,
|
|
169
|
+
): Promise<T[]> | AggregateChain<T> | ResultWithMeta<T[]>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 去重查询
|
|
173
|
+
* 支持 meta 参数
|
|
174
|
+
*/
|
|
175
|
+
distinct<T = any>(
|
|
176
|
+
field: string,
|
|
177
|
+
query?: any,
|
|
178
|
+
options?: Omit<DistinctOptions, "meta">,
|
|
179
|
+
): Promise<T[]>;
|
|
180
|
+
distinct<T = any>(
|
|
181
|
+
field: string,
|
|
182
|
+
query: any,
|
|
183
|
+
options: DistinctOptions & { meta: true | MetaOptions },
|
|
184
|
+
): Promise<ResultWithMeta<T[]>>;
|
|
185
|
+
distinct<T = any>(
|
|
186
|
+
field: string,
|
|
187
|
+
query?: any,
|
|
188
|
+
options?: DistinctOptions,
|
|
189
|
+
): Promise<T[] | ResultWithMeta<T[]>>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 流式查询
|
|
193
|
+
* 返回 Node.js 可读流
|
|
194
|
+
*/
|
|
195
|
+
stream(query?: any, options?: StreamOptions): NodeJS.ReadableStream;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 查询执行计划诊断
|
|
199
|
+
*/
|
|
200
|
+
explain(query?: any, options?: ExplainOptions): Promise<any>;
|
|
201
|
+
|
|
202
|
+
// ============================================================================
|
|
203
|
+
// 分页相关
|
|
204
|
+
// ============================================================================
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 深度分页
|
|
208
|
+
*/
|
|
209
|
+
findPage<T = TSchema>(options: FindPageOptions): Promise<PageResult<T>>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Bookmark 维护 APIs
|
|
213
|
+
*/
|
|
214
|
+
prewarmBookmarks(
|
|
215
|
+
keyDims: BookmarkKeyDims,
|
|
216
|
+
pages: number[],
|
|
217
|
+
): Promise<PrewarmBookmarksResult>;
|
|
218
|
+
listBookmarks(keyDims?: BookmarkKeyDims): Promise<ListBookmarksResult>;
|
|
219
|
+
clearBookmarks(keyDims?: BookmarkKeyDims): Promise<ClearBookmarksResult>;
|
|
220
|
+
|
|
221
|
+
// ============================================================================
|
|
222
|
+
// 写入操作
|
|
223
|
+
// ============================================================================
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 插入单个文档
|
|
227
|
+
* 支持简化调用和完整配置
|
|
228
|
+
*/
|
|
229
|
+
insertOne<T = TSchema>(
|
|
230
|
+
document: T,
|
|
231
|
+
options?: InsertOneSimplifiedOptions,
|
|
232
|
+
): Promise<InsertOneResult>;
|
|
233
|
+
insertOne(options: InsertOneOptions): Promise<InsertOneResult>;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 插入多个文档
|
|
237
|
+
* 支持简化调用和完整配置
|
|
238
|
+
*/
|
|
239
|
+
insertMany<T = TSchema>(
|
|
240
|
+
documents: T[],
|
|
241
|
+
options?: InsertManySimplifiedOptions,
|
|
242
|
+
): Promise<InsertManyResult>;
|
|
243
|
+
insertMany(options: InsertManyOptions): Promise<InsertManyResult>;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Upsert 单个文档(存在则更新,不存在则插入)
|
|
247
|
+
* @param filter - 查询条件
|
|
248
|
+
* @param update - 更新内容
|
|
249
|
+
* @param options - 操作选项
|
|
250
|
+
*/
|
|
251
|
+
upsertOne(
|
|
252
|
+
filter: Record<string, any>,
|
|
253
|
+
update: Record<string, any>,
|
|
254
|
+
options?: {
|
|
255
|
+
maxTimeMS?: number;
|
|
256
|
+
comment?: string;
|
|
257
|
+
},
|
|
258
|
+
): Promise<{
|
|
259
|
+
acknowledged: boolean;
|
|
260
|
+
matchedCount: number;
|
|
261
|
+
modifiedCount: number;
|
|
262
|
+
upsertedId?: any;
|
|
263
|
+
upsertedCount: number;
|
|
264
|
+
}>;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 原子递增/递减字段值(便利方法)
|
|
268
|
+
* @param filter - 查询条件
|
|
269
|
+
* @param field - 字段名或字段-增量对象
|
|
270
|
+
* @param increment - 增量(默认 1,负数为递减)
|
|
271
|
+
* @param options - 操作选项
|
|
272
|
+
*/
|
|
273
|
+
incrementOne(
|
|
274
|
+
filter: Record<string, any>,
|
|
275
|
+
field: string | Record<string, number>,
|
|
276
|
+
increment?: number,
|
|
277
|
+
options?: {
|
|
278
|
+
returnDocument?: "before" | "after";
|
|
279
|
+
projection?: Record<string, any>;
|
|
280
|
+
maxTimeMS?: number;
|
|
281
|
+
comment?: string;
|
|
282
|
+
},
|
|
283
|
+
): Promise<{
|
|
284
|
+
acknowledged: boolean;
|
|
285
|
+
matchedCount: number;
|
|
286
|
+
modifiedCount: number;
|
|
287
|
+
value: any | null;
|
|
288
|
+
}>;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* 使缓存失效
|
|
292
|
+
*/
|
|
293
|
+
invalidate(
|
|
294
|
+
op?: "find" | "findOne" | "count" | "findPage" | "aggregate" | "distinct",
|
|
295
|
+
): Promise<number>;
|
|
296
|
+
|
|
297
|
+
// ============================================================================
|
|
298
|
+
// 批量操作
|
|
299
|
+
// ============================================================================
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* 大批量插入(自动分批+重试)
|
|
303
|
+
* @since v1.0.0
|
|
304
|
+
*/
|
|
305
|
+
insertBatch<T = TSchema>(
|
|
306
|
+
documents: T[],
|
|
307
|
+
options?: InsertBatchOptions,
|
|
308
|
+
): Promise<InsertBatchResult>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 批量更新文档(流式查询+分批更新)
|
|
312
|
+
* @since v1.0.0
|
|
313
|
+
*/
|
|
314
|
+
updateBatch(
|
|
315
|
+
filter: Record<string, any>,
|
|
316
|
+
update: Record<string, any>,
|
|
317
|
+
options?: UpdateBatchOptions,
|
|
318
|
+
): Promise<UpdateBatchResult>;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 批量删除文档(流式查询+分批删除)
|
|
322
|
+
* @since v1.0.0
|
|
323
|
+
*/
|
|
324
|
+
deleteBatch(
|
|
325
|
+
filter: Record<string, any>,
|
|
326
|
+
options?: DeleteBatchOptions,
|
|
327
|
+
): Promise<DeleteBatchResult>;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 查询文档并返回总数(便利方法)
|
|
331
|
+
* @since v1.0.0
|
|
332
|
+
*/
|
|
333
|
+
findAndCount<T = TSchema>(
|
|
334
|
+
filter?: Record<string, any>,
|
|
335
|
+
options?: FindOptions,
|
|
336
|
+
): Promise<{ documents: T[]; total: number }>;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Collection 类型别名(与 CollectionAccessor 等价)
|
|
341
|
+
* @since v1.0.4
|
|
342
|
+
*/
|
|
343
|
+
export type Collection<TSchema = any> = CollectionAccessor<TSchema>;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 数据库访问器
|
|
347
|
+
*/
|
|
348
|
+
export type DbAccessor = {
|
|
349
|
+
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
350
|
+
db(dbName: string): {
|
|
351
|
+
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
352
|
+
};
|
|
353
|
+
/** `db()` 的别名,语义更清晰:`app.db.use('logs')` */
|
|
354
|
+
use(dbName: string): {
|
|
355
|
+
collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
|
|
356
|
+
};
|
|
357
|
+
};
|