monsqlize 1.1.6 → 1.1.7

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/lib/connect.js CHANGED
@@ -1,83 +1,99 @@
1
- /**
2
- * 数据库连接管理器
3
- * 统一管理各种数据库的连接创建和实例化逻辑
4
- */
5
- const Mongo = require('./mongodb');
6
-
7
- module.exports = class ConnectionManager {
8
-
9
- /**
10
- * 支持的数据库类型映射
11
- */
12
- static get SUPPORTED_DATABASES() {
13
- return {
14
- 'mongodb': Mongo,
15
- };
16
- }
17
-
18
- /**
19
- * 创建数据库实例
20
- * @param {string} type - 数据库类型
21
- * @param {string} databaseName - 数据库名称
22
- * @param {Object} cache - 缓存实例(内存缓存)
23
- * @param {Object} logger - 日志记录器
24
- * @param {Object} [defaults] - 统一默认配置(如 maxTimeMS、namespace)
25
- * @returns {Object} 数据库实例
26
- * @throws {Error} 当数据库类型不支持或未实现时抛出错误
27
- */
28
- static createInstance(type, databaseName, cache, logger, defaults) {
29
- const SUPPORTED_DATABASES = this.SUPPORTED_DATABASES;
30
- // 验证数据库类型是否支持
31
- if (!(type in SUPPORTED_DATABASES)) {
32
- const supportedTypes = Object.keys(SUPPORTED_DATABASES).join(', ');
33
- throw new Error(`Invalid database type: ${type}. Supported types are: ${supportedTypes}`);
34
- }
35
-
36
- // 检查是否已实现
37
- if (SUPPORTED_DATABASES[type] === null) {
38
- throw new Error(`${type} support not implemented yet`);
39
- }
40
-
41
- // 获取对应的数据库类
42
- const DatabaseClass = SUPPORTED_DATABASES[type];
43
-
44
- // 创建并返回实例
45
- return new DatabaseClass(type, databaseName, cache, logger, defaults);
46
- }
47
-
48
- /**
49
- * 连接数据库
50
- * @param {string} type - 数据库类型
51
- * @param {string} databaseName - 数据库名称
52
- * @param {Object} config - 数据库连接配置
53
- * @param {Object} cache - 缓存实例
54
- * @param {Object} logger - 日志记录器
55
- * @param {Object} [defaults] - 统一默认配置(如 maxTimeMS、namespace)
56
- * @param {Object} [poolManager] - 多连接池管理器(v1.0.8+)
57
- * @returns {{accessor: Function, instance: Object}} 访问器与底层适配器实例
58
- * @throws {Error} 连接失败时抛出错误
59
- */
60
- static async connect(type, databaseName, config, cache, logger, defaults, poolManager) {
61
- // 创建数据库实例
62
- const instance = this.createInstance(type, databaseName, cache, logger, defaults);
63
-
64
- // 🆕 v1.0.8: 传递 poolManager 给实例
65
- if (poolManager) {
66
- instance.poolManager = poolManager;
67
- }
68
-
69
- // 建立连接
70
- await instance.connect(config);
71
-
72
- // ---------- 构建访问器 ----------
73
- const collection = (collectionName) => instance.collection(databaseName, collectionName);
74
- const db = (databaseName)=>{
75
- return {
76
- collection:(collectionName)=>instance.collection(databaseName, collectionName)
77
- };
78
- };
79
-
80
- return { collection, db, instance };
81
- }
82
- };
83
-
1
+ /**
2
+ * 数据库连接管理器
3
+ * 统一管理各种数据库的连接创建和实例化逻辑
4
+ */
5
+ const Mongo = require("./mongodb");
6
+
7
+ module.exports = class ConnectionManager {
8
+ /**
9
+ * 支持的数据库类型映射
10
+ */
11
+ static get SUPPORTED_DATABASES() {
12
+ return {
13
+ mongodb: Mongo,
14
+ };
15
+ }
16
+
17
+ /**
18
+ * 创建数据库实例
19
+ * @param {string} type - 数据库类型
20
+ * @param {string} databaseName - 数据库名称
21
+ * @param {Object} cache - 缓存实例(内存缓存)
22
+ * @param {Object} logger - 日志记录器
23
+ * @param {Object} [defaults] - 统一默认配置(如 maxTimeMS、namespace)
24
+ * @returns {Object} 数据库实例
25
+ * @throws {Error} 当数据库类型不支持或未实现时抛出错误
26
+ */
27
+ static createInstance(type, databaseName, cache, logger, defaults) {
28
+ const SUPPORTED_DATABASES = this.SUPPORTED_DATABASES;
29
+ // 验证数据库类型是否支持
30
+ if (!(type in SUPPORTED_DATABASES)) {
31
+ const supportedTypes = Object.keys(SUPPORTED_DATABASES).join(", ");
32
+ throw new Error(
33
+ `Invalid database type: ${type}. Supported types are: ${supportedTypes}`,
34
+ );
35
+ }
36
+
37
+ // 检查是否已实现
38
+ if (SUPPORTED_DATABASES[type] === null) {
39
+ throw new Error(`${type} support not implemented yet`);
40
+ }
41
+
42
+ // 获取对应的数据库类
43
+ const DatabaseClass = SUPPORTED_DATABASES[type];
44
+
45
+ // 创建并返回实例
46
+ return new DatabaseClass(type, databaseName, cache, logger, defaults);
47
+ }
48
+
49
+ /**
50
+ * 连接数据库
51
+ * @param {string} type - 数据库类型
52
+ * @param {string} databaseName - 数据库名称
53
+ * @param {Object} config - 数据库连接配置
54
+ * @param {Object} cache - 缓存实例
55
+ * @param {Object} logger - 日志记录器
56
+ * @param {Object} [defaults] - 统一默认配置(如 maxTimeMS、namespace)
57
+ * @param {Object} [poolManager] - 多连接池管理器(v1.0.8+)
58
+ * @returns {{accessor: Function, instance: Object}} 访问器与底层适配器实例
59
+ * @throws {Error} 连接失败时抛出错误
60
+ */
61
+ static async connect(
62
+ type,
63
+ databaseName,
64
+ config,
65
+ cache,
66
+ logger,
67
+ defaults,
68
+ poolManager,
69
+ ) {
70
+ // 创建数据库实例
71
+ const instance = this.createInstance(
72
+ type,
73
+ databaseName,
74
+ cache,
75
+ logger,
76
+ defaults,
77
+ );
78
+
79
+ // 🆕 v1.0.8: 传递 poolManager 给实例
80
+ if (poolManager) {
81
+ instance.poolManager = poolManager;
82
+ }
83
+
84
+ // 建立连接
85
+ await instance.connect(config);
86
+
87
+ // ---------- 构建访问器 ----------
88
+ const collection = (collectionName) =>
89
+ instance.collection(databaseName, collectionName);
90
+ const db = (databaseName) => {
91
+ return {
92
+ collection: (collectionName) =>
93
+ instance.collection(databaseName, collectionName),
94
+ };
95
+ };
96
+
97
+ return { collection, db, use: db, instance };
98
+ }
99
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monsqlize",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "A lightweight MongoDB ORM with multi-level caching, transaction support, distributed features, Saga distributed transactions, unified expression system with 122 operators, and universal function caching (100% MongoDB support)",
5
5
  "main": "lib/index.js",
6
6
  "module": "index.mjs",
@@ -115,7 +115,7 @@
115
115
  "dependencies": {
116
116
  "async-lock": "^1.4.1",
117
117
  "mongodb": "^6.17.0",
118
- "schema-dsl": "^1.1.5",
118
+ "schema-dsl": "^1.2.4",
119
119
  "ssh2": "^1.17.0"
120
120
  }
121
121
  }
@@ -1,287 +1,357 @@
1
- /**
2
- * Collection API 相关类型定义
3
- * @module types/collection
4
- */
5
-
6
- import type { FindOptions, CountOptions, AggregateOptions, DistinctOptions } from './query';
7
- import type {
8
- InsertOneSimplifiedOptions,
9
- InsertOneOptions,
10
- InsertOneResult,
11
- InsertManySimplifiedOptions,
12
- InsertManyOptions,
13
- InsertManyResult
14
- } from './write';
15
- import type {
16
- InsertBatchOptions,
17
- InsertBatchResult,
18
- UpdateBatchOptions,
19
- UpdateBatchResult,
20
- DeleteBatchOptions,
21
- DeleteBatchResult
22
- } from './batch';
23
- import type {
24
- FindPageOptions,
25
- PageResult,
26
- ResultWithMeta,
27
- MetaOptions,
28
- BookmarkKeyDims,
29
- PrewarmBookmarksResult,
30
- ListBookmarksResult,
31
- ClearBookmarksResult
32
- } from './pagination';
33
- import type { StreamOptions, ExplainOptions } from './stream';
34
- import type { FindChain, AggregateChain } from './chain';
35
-
36
- /**
37
- * 健康视图
38
- */
39
- export interface HealthView {
40
- status: 'up' | 'down';
41
- connected: boolean;
42
- defaults?: any;
43
- cache?: any;
44
- driver?: { connected: boolean };
45
- }
46
-
47
- /**
48
- * Collection 访问器接口
49
- * 提供所有 Collection 操作方法
50
- */
51
- export interface CollectionAccessor<TSchema = any> {
52
- /**
53
- * 获取命名空间信息
54
- */
55
- getNamespace(): { iid: string; type: string; db: string; collection: string };
56
-
57
- /**
58
- * 删除集合
59
- */
60
- dropCollection(): Promise<boolean>;
61
-
62
- /**
63
- * 创建集合
64
- */
65
- createCollection(name?: string | null, options?: any): Promise<boolean>;
66
-
67
- /**
68
- * 创建视图
69
- */
70
- createView(viewName: string, source: string, pipeline?: any[]): Promise<boolean>;
71
-
72
- // ============================================================================
73
- // 查询方法
74
- // ============================================================================
75
-
76
- /**
77
- * 查询单个文档
78
- * 支持 meta 参数和泛型
79
- */
80
- findOne<T = TSchema>(query?: any, options?: Omit<FindOptions, 'meta'>): Promise<T | null>;
81
- findOne<T = TSchema>(query: any, options: FindOptions & { meta: true | MetaOptions }): Promise<ResultWithMeta<T | null>>;
82
- findOne<T = TSchema>(query?: any, options?: FindOptions): Promise<T | null | ResultWithMeta<T | null>>;
83
-
84
- /**
85
- * 通过 _id 查询单个文档(便利方法)
86
- * @param id - 文档的 _id(字符串会自动转换为 ObjectId)
87
- * @param options - 查询选项
88
- */
89
- findOneById(id: string | any, options?: Omit<FindOptions, 'meta'>): Promise<any | null>;
90
-
91
- /**
92
- * 批量通过 _id 查询多个文档(便利方法)
93
- * @param ids - _id 数组
94
- * @param options - 查询选项
95
- */
96
- findByIds(
97
- ids: Array<string | any>,
98
- options?: {
99
- projection?: Record<string, any>;
100
- sort?: Record<string, 1 | -1>;
101
- cache?: number;
102
- maxTimeMS?: number;
103
- comment?: string;
104
- preserveOrder?: boolean;
105
- }
106
- ): Promise<any[]>;
107
-
108
- /**
109
- * 查询多个文档
110
- * 支持 meta 参数和链式调用
111
- */
112
- find<T = TSchema>(query?: any): FindChain<T>;
113
- find<T = TSchema>(query: any, options: FindOptions & { meta: true | MetaOptions }): Promise<ResultWithMeta<T[]>>;
114
- find<T = TSchema>(query?: any, options?: FindOptions): Promise<T[]> | FindChain<T> | ResultWithMeta<T[]>;
115
-
116
- /**
117
- * 计数
118
- * 支持 meta 参数
119
- */
120
- count(query?: any, options?: Omit<CountOptions, 'meta'>): Promise<number>;
121
- count(query: any, options: CountOptions & { meta: true | MetaOptions }): Promise<ResultWithMeta<number>>;
122
- count(query?: any, options?: CountOptions): Promise<number | ResultWithMeta<number>>;
123
-
124
- /**
125
- * 聚合查询
126
- * 支持 meta 参数和链式调用
127
- */
128
- aggregate<T = TSchema>(pipeline?: any[]): AggregateChain<T>;
129
- aggregate<T = TSchema>(pipeline: any[], options: AggregateOptions & { meta: true | MetaOptions }): Promise<ResultWithMeta<T[]>>;
130
- aggregate<T = TSchema>(pipeline?: any[], options?: AggregateOptions): Promise<T[]> | AggregateChain<T> | ResultWithMeta<T[]>;
131
-
132
- /**
133
- * 去重查询
134
- * 支持 meta 参数
135
- */
136
- distinct<T = any>(field: string, query?: any, options?: Omit<DistinctOptions, 'meta'>): Promise<T[]>;
137
- distinct<T = any>(field: string, query: any, options: DistinctOptions & { meta: true | MetaOptions }): Promise<ResultWithMeta<T[]>>;
138
- distinct<T = any>(field: string, query?: any, options?: DistinctOptions): Promise<T[] | ResultWithMeta<T[]>>;
139
-
140
- /**
141
- * 流式查询
142
- * 返回 Node.js 可读流
143
- */
144
- stream(query?: any, options?: StreamOptions): NodeJS.ReadableStream;
145
-
146
- /**
147
- * 查询执行计划诊断
148
- */
149
- explain(query?: any, options?: ExplainOptions): Promise<any>;
150
-
151
- // ============================================================================
152
- // 分页相关
153
- // ============================================================================
154
-
155
- /**
156
- * 深度分页
157
- */
158
- findPage<T = TSchema>(options: FindPageOptions): Promise<PageResult<T>>;
159
-
160
- /**
161
- * Bookmark 维护 APIs
162
- */
163
- prewarmBookmarks(keyDims: BookmarkKeyDims, pages: number[]): Promise<PrewarmBookmarksResult>;
164
- listBookmarks(keyDims?: BookmarkKeyDims): Promise<ListBookmarksResult>;
165
- clearBookmarks(keyDims?: BookmarkKeyDims): Promise<ClearBookmarksResult>;
166
-
167
- // ============================================================================
168
- // 写入操作
169
- // ============================================================================
170
-
171
- /**
172
- * 插入单个文档
173
- * 支持简化调用和完整配置
174
- */
175
- insertOne<T = TSchema>(document: T, options?: InsertOneSimplifiedOptions): Promise<InsertOneResult>;
176
- insertOne(options: InsertOneOptions): Promise<InsertOneResult>;
177
-
178
- /**
179
- * 插入多个文档
180
- * 支持简化调用和完整配置
181
- */
182
- insertMany<T = TSchema>(documents: T[], options?: InsertManySimplifiedOptions): Promise<InsertManyResult>;
183
- insertMany(options: InsertManyOptions): Promise<InsertManyResult>;
184
-
185
- /**
186
- * Upsert 单个文档(存在则更新,不存在则插入)
187
- * @param filter - 查询条件
188
- * @param update - 更新内容
189
- * @param options - 操作选项
190
- */
191
- upsertOne(
192
- filter: Record<string, any>,
193
- update: Record<string, any>,
194
- options?: {
195
- maxTimeMS?: number;
196
- comment?: string;
197
- }
198
- ): Promise<{
199
- acknowledged: boolean;
200
- matchedCount: number;
201
- modifiedCount: number;
202
- upsertedId?: any;
203
- upsertedCount: number;
204
- }>;
205
-
206
- /**
207
- * 原子递增/递减字段值(便利方法)
208
- * @param filter - 查询条件
209
- * @param field - 字段名或字段-增量对象
210
- * @param increment - 增量(默认 1,负数为递减)
211
- * @param options - 操作选项
212
- */
213
- incrementOne(
214
- filter: Record<string, any>,
215
- field: string | Record<string, number>,
216
- increment?: number,
217
- options?: {
218
- returnDocument?: 'before' | 'after';
219
- projection?: Record<string, any>;
220
- maxTimeMS?: number;
221
- comment?: string;
222
- }
223
- ): Promise<{
224
- acknowledged: boolean;
225
- matchedCount: number;
226
- modifiedCount: number;
227
- value: any | null;
228
- }>;
229
-
230
- /**
231
- * 使缓存失效
232
- */
233
- invalidate(op?: 'find' | 'findOne' | 'count' | 'findPage' | 'aggregate' | 'distinct'): Promise<number>;
234
-
235
- // ============================================================================
236
- // 批量操作
237
- // ============================================================================
238
-
239
- /**
240
- * 大批量插入(自动分批+重试)
241
- * @since v1.0.0
242
- */
243
- insertBatch<T = TSchema>(documents: T[], options?: InsertBatchOptions): Promise<InsertBatchResult>;
244
-
245
- /**
246
- * 批量更新文档(流式查询+分批更新)
247
- * @since v1.0.0
248
- */
249
- updateBatch(
250
- filter: Record<string, any>,
251
- update: Record<string, any>,
252
- options?: UpdateBatchOptions
253
- ): Promise<UpdateBatchResult>;
254
-
255
- /**
256
- * 批量删除文档(流式查询+分批删除)
257
- * @since v1.0.0
258
- */
259
- deleteBatch(
260
- filter: Record<string, any>,
261
- options?: DeleteBatchOptions
262
- ): Promise<DeleteBatchResult>;
263
-
264
- /**
265
- * 查询文档并返回总数(便利方法)
266
- * @since v1.0.0
267
- */
268
- findAndCount<T = TSchema>(
269
- filter?: Record<string, any>,
270
- options?: FindOptions
271
- ): Promise<{ documents: T[]; total: number }>;
272
- }
273
-
274
- /**
275
- * Collection 类型别名(与 CollectionAccessor 等价)
276
- * @since v1.0.4
277
- */
278
- export type Collection<TSchema = any> = CollectionAccessor<TSchema>;
279
-
280
- /**
281
- * 数据库访问器
282
- */
283
- export type DbAccessor = {
284
- collection<TSchema = any>(name: string): CollectionAccessor<TSchema>;
285
- db(dbName: string): { collection<TSchema = any>(name: string): CollectionAccessor<TSchema> };
286
- };
287
-
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
+ };