monsqlize 1.3.0 → 2.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.
Files changed (172) hide show
  1. package/CHANGELOG.md +56 -60
  2. package/LICENSE +201 -21
  3. package/README.md +537 -1828
  4. package/changelogs/README.md +160 -0
  5. package/changelogs/v2.0.0.md +222 -0
  6. package/dist/cjs/index.cjs +10600 -0
  7. package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
  8. package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
  9. package/dist/cjs/transaction/Transaction.cjs +158 -0
  10. package/dist/cjs/transaction/TransactionManager.cjs +298 -0
  11. package/dist/esm/index.mjs +10650 -0
  12. package/dist/types/base.d.ts +81 -0
  13. package/dist/types/collection.d.ts +1031 -0
  14. package/dist/types/expression.d.ts +115 -0
  15. package/dist/types/index.d.ts +23 -0
  16. package/dist/types/lock.d.ts +74 -0
  17. package/dist/types/model.d.ts +526 -0
  18. package/dist/types/mongodb.d.ts +49 -0
  19. package/dist/types/monsqlize.d.ts +491 -0
  20. package/dist/types/pool.d.ts +84 -0
  21. package/dist/types/runtime.d.ts +362 -0
  22. package/dist/types/saga.d.ts +143 -0
  23. package/dist/types/slow-query-log.d.ts +126 -0
  24. package/dist/types/sync.d.ts +103 -0
  25. package/dist/types/transaction.d.ts +132 -0
  26. package/package.json +67 -69
  27. package/index.d.ts +0 -206
  28. package/index.mjs +0 -52
  29. package/lib/cache-invalidation.js +0 -279
  30. package/lib/cache.js +0 -530
  31. package/lib/common/cursor.js +0 -59
  32. package/lib/common/docs-urls.js +0 -73
  33. package/lib/common/index-options.js +0 -223
  34. package/lib/common/log.js +0 -61
  35. package/lib/common/namespace.js +0 -22
  36. package/lib/common/normalize.js +0 -34
  37. package/lib/common/page-result.js +0 -43
  38. package/lib/common/runner.js +0 -57
  39. package/lib/common/server-features.js +0 -232
  40. package/lib/common/shape-builders.js +0 -27
  41. package/lib/common/validation.js +0 -113
  42. package/lib/connect.js +0 -99
  43. package/lib/constants.js +0 -55
  44. package/lib/count-queue.js +0 -188
  45. package/lib/distributed-cache-invalidator.js +0 -260
  46. package/lib/errors.js +0 -168
  47. package/lib/expression/cache/ExpressionCache.js +0 -114
  48. package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
  49. package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
  50. package/lib/expression/detector.js +0 -84
  51. package/lib/expression/factory.js +0 -29
  52. package/lib/expression/index.js +0 -19
  53. package/lib/function-cache.js +0 -533
  54. package/lib/index.js +0 -1251
  55. package/lib/infrastructure/ConnectionPoolManager.js +0 -464
  56. package/lib/infrastructure/HealthChecker.js +0 -281
  57. package/lib/infrastructure/PoolConfig.js +0 -199
  58. package/lib/infrastructure/PoolSelector.js +0 -225
  59. package/lib/infrastructure/PoolStats.js +0 -244
  60. package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
  61. package/lib/infrastructure/ssh-tunnel.js +0 -41
  62. package/lib/infrastructure/uri-parser.js +0 -36
  63. package/lib/lock/Lock.js +0 -67
  64. package/lib/lock/errors.js +0 -28
  65. package/lib/lock/index.js +0 -13
  66. package/lib/logger.js +0 -225
  67. package/lib/model/examples/test.js +0 -311
  68. package/lib/model/features/defaults.js +0 -161
  69. package/lib/model/features/populate.js +0 -568
  70. package/lib/model/features/relations.js +0 -120
  71. package/lib/model/features/soft-delete.js +0 -349
  72. package/lib/model/features/version.js +0 -157
  73. package/lib/model/features/virtuals.js +0 -219
  74. package/lib/model/index.js +0 -1265
  75. package/lib/mongodb/common/accessor-helpers.js +0 -59
  76. package/lib/mongodb/common/agg-pipeline.js +0 -36
  77. package/lib/mongodb/common/aggregation-validator.js +0 -127
  78. package/lib/mongodb/common/iid.js +0 -28
  79. package/lib/mongodb/common/lexicographic-expr.js +0 -53
  80. package/lib/mongodb/common/shape.js +0 -32
  81. package/lib/mongodb/common/sort.js +0 -39
  82. package/lib/mongodb/common/transaction-aware.js +0 -25
  83. package/lib/mongodb/connect.js +0 -234
  84. package/lib/mongodb/index.js +0 -639
  85. package/lib/mongodb/management/admin-ops.js +0 -200
  86. package/lib/mongodb/management/bookmark-ops.js +0 -167
  87. package/lib/mongodb/management/cache-ops.js +0 -50
  88. package/lib/mongodb/management/collection-ops.js +0 -387
  89. package/lib/mongodb/management/database-ops.js +0 -202
  90. package/lib/mongodb/management/index-ops.js +0 -475
  91. package/lib/mongodb/management/index.js +0 -17
  92. package/lib/mongodb/management/namespace.js +0 -31
  93. package/lib/mongodb/management/validation-ops.js +0 -268
  94. package/lib/mongodb/queries/aggregate.js +0 -172
  95. package/lib/mongodb/queries/chain.js +0 -631
  96. package/lib/mongodb/queries/count.js +0 -99
  97. package/lib/mongodb/queries/distinct.js +0 -78
  98. package/lib/mongodb/queries/find-and-count.js +0 -193
  99. package/lib/mongodb/queries/find-by-ids.js +0 -236
  100. package/lib/mongodb/queries/find-one-by-id.js +0 -171
  101. package/lib/mongodb/queries/find-one.js +0 -71
  102. package/lib/mongodb/queries/find-page.js +0 -618
  103. package/lib/mongodb/queries/find.js +0 -171
  104. package/lib/mongodb/queries/index.js +0 -51
  105. package/lib/mongodb/queries/watch.js +0 -538
  106. package/lib/mongodb/writes/common/batch-retry.js +0 -65
  107. package/lib/mongodb/writes/delete-batch.js +0 -323
  108. package/lib/mongodb/writes/delete-many.js +0 -181
  109. package/lib/mongodb/writes/delete-one.js +0 -173
  110. package/lib/mongodb/writes/find-one-and-delete.js +0 -203
  111. package/lib/mongodb/writes/find-one-and-replace.js +0 -239
  112. package/lib/mongodb/writes/find-one-and-update.js +0 -240
  113. package/lib/mongodb/writes/increment-one.js +0 -259
  114. package/lib/mongodb/writes/index.js +0 -46
  115. package/lib/mongodb/writes/insert-batch.js +0 -508
  116. package/lib/mongodb/writes/insert-many.js +0 -223
  117. package/lib/mongodb/writes/insert-one.js +0 -169
  118. package/lib/mongodb/writes/replace-one.js +0 -226
  119. package/lib/mongodb/writes/result-handler.js +0 -237
  120. package/lib/mongodb/writes/update-batch.js +0 -416
  121. package/lib/mongodb/writes/update-many.js +0 -275
  122. package/lib/mongodb/writes/update-one.js +0 -273
  123. package/lib/mongodb/writes/upsert-one.js +0 -203
  124. package/lib/multi-level-cache.js +0 -244
  125. package/lib/operators.js +0 -330
  126. package/lib/redis-cache-adapter.js +0 -267
  127. package/lib/saga/SagaContext.js +0 -67
  128. package/lib/saga/SagaDefinition.js +0 -32
  129. package/lib/saga/SagaExecutor.js +0 -201
  130. package/lib/saga/SagaOrchestrator.js +0 -186
  131. package/lib/saga/index.js +0 -11
  132. package/lib/slow-query-log/base-storage.js +0 -70
  133. package/lib/slow-query-log/batch-queue.js +0 -97
  134. package/lib/slow-query-log/config-manager.js +0 -196
  135. package/lib/slow-query-log/index.js +0 -238
  136. package/lib/slow-query-log/mongodb-storage.js +0 -324
  137. package/lib/slow-query-log/query-hash.js +0 -39
  138. package/lib/sync/ChangeStreamSyncManager.js +0 -405
  139. package/lib/sync/ResumeTokenStore.js +0 -192
  140. package/lib/sync/SyncConfig.js +0 -127
  141. package/lib/sync/SyncTarget.js +0 -240
  142. package/lib/sync/index.js +0 -20
  143. package/lib/transaction/CacheLockManager.js +0 -162
  144. package/lib/transaction/DistributedCacheLockManager.js +0 -475
  145. package/lib/transaction/Transaction.js +0 -315
  146. package/lib/transaction/TransactionManager.js +0 -267
  147. package/lib/transaction/index.js +0 -11
  148. package/lib/utils/objectid-converter.js +0 -632
  149. package/types/README.md +0 -122
  150. package/types/base.ts +0 -94
  151. package/types/batch.ts +0 -187
  152. package/types/cache.ts +0 -71
  153. package/types/chain.ts +0 -254
  154. package/types/collection.ts +0 -357
  155. package/types/expression.ts +0 -109
  156. package/types/function-cache.d.ts +0 -135
  157. package/types/lock.ts +0 -95
  158. package/types/model/definition.ts +0 -152
  159. package/types/model/index.ts +0 -10
  160. package/types/model/instance.ts +0 -121
  161. package/types/model/relations.ts +0 -121
  162. package/types/model/virtuals.ts +0 -32
  163. package/types/monsqlize.ts +0 -245
  164. package/types/options.ts +0 -192
  165. package/types/pagination.ts +0 -154
  166. package/types/pool.ts +0 -125
  167. package/types/query.ts +0 -71
  168. package/types/saga.ts +0 -125
  169. package/types/stream.ts +0 -64
  170. package/types/sync.ts +0 -79
  171. package/types/transaction.ts +0 -79
  172. package/types/write.ts +0 -77
@@ -0,0 +1,526 @@
1
+ import type { BookmarkClearResult, BookmarkListResult, BookmarkPrewarmResult, DeleteBatchResult, DeleteResult, IncrementOneResult, IndexCreateResult, InsertBatchResult, InsertManyResult, InsertOneResult, UpdateBatchResult, UpdateResult } from './collection';
2
+
3
+ /**
4
+ * Schema DSL transformer function.
5
+ * Receives a raw DSL descriptor and returns a transformed schema object.
6
+ * @since v1.0.0
7
+ */
8
+ export type SchemaDSL = (dsl: unknown) => unknown;
9
+
10
+ /**
11
+ * Default value for a model field — either a static value or a factory function.
12
+ * @template T Field value type
13
+ * @since v1.0.0
14
+ */
15
+ export type DefaultValue<T = unknown> = T | ((context?: unknown, doc?: unknown) => T);
16
+
17
+ export interface ValidationResult {
18
+ valid: boolean;
19
+ errors?: Array<{
20
+ field: string;
21
+ message: string;
22
+ value?: unknown;
23
+ }>;
24
+ data?: unknown;
25
+ }
26
+
27
+ export interface HookContext {
28
+ operation: string;
29
+ collection: string;
30
+ data?: unknown;
31
+ filter?: unknown;
32
+ update?: unknown;
33
+ result?: unknown;
34
+ error?: Error;
35
+ [key: string]: unknown;
36
+ }
37
+
38
+ export interface ModelConnection {
39
+ pool?: string;
40
+ database?: string;
41
+ }
42
+
43
+ export interface RelationConfig {
44
+ from: string;
45
+ localField: string;
46
+ foreignField: string;
47
+ single?: boolean;
48
+ }
49
+
50
+ export interface PopulateConfig {
51
+ path: string;
52
+ select?: string | string[];
53
+ match?: Record<string, unknown>;
54
+ sort?: Record<string, 1 | -1>;
55
+ limit?: number;
56
+ skip?: number;
57
+ populate?: string | PopulateConfig | Array<string | PopulateConfig>;
58
+ }
59
+
60
+ export interface VirtualConfig {
61
+ get: (this: Record<string, unknown>) => unknown;
62
+ set?: (this: Record<string, unknown>, value: unknown) => void;
63
+ }
64
+
65
+ /** v1 hooks factory format */
66
+ export type V1HooksFactory<TDocument = Record<string, unknown>> = (
67
+ model: ModelInstance<TDocument>,
68
+ ) => {
69
+ find?: {
70
+ before?: (ctx: Record<string, unknown>, ...args: unknown[]) => unknown;
71
+ after?: (ctx: Record<string, unknown>, result: unknown) => unknown;
72
+ };
73
+ insert?: {
74
+ before?: (ctx: Record<string, unknown>, ...args: unknown[]) => unknown;
75
+ after?: (ctx: Record<string, unknown>, result: unknown) => unknown;
76
+ };
77
+ update?: {
78
+ before?: (ctx: Record<string, unknown>, ...args: unknown[]) => unknown;
79
+ after?: (ctx: Record<string, unknown>, result: unknown) => unknown;
80
+ };
81
+ delete?: {
82
+ before?: (ctx: Record<string, unknown>, ...args: unknown[]) => unknown;
83
+ after?: (ctx: Record<string, unknown>, result: unknown) => unknown;
84
+ };
85
+ };
86
+
87
+ /** v1 methods factory format */
88
+ export type V1MethodsFactory<TDocument = Record<string, unknown>> = (
89
+ model: ModelInstance<TDocument>,
90
+ ) => {
91
+ instance?: Record<string, (this: TDocument & Record<string, unknown>, ...args: unknown[]) => unknown>;
92
+ static?: Record<string, (...args: unknown[]) => unknown>;
93
+ };
94
+
95
+ export interface ModelDefinitionOptions {
96
+ timestamps?: boolean | { createdAt?: string | boolean; updatedAt?: string | boolean };
97
+ validate?: boolean;
98
+ softDelete?: boolean | {
99
+ enabled?: boolean;
100
+ field?: string;
101
+ type?: string;
102
+ ttl?: number | null;
103
+ };
104
+ version?: boolean | {
105
+ enabled?: boolean;
106
+ field?: string;
107
+ };
108
+ }
109
+
110
+ export interface ModelDefinition<TDocument = Record<string, unknown>> {
111
+ enums?: Record<string, string>;
112
+ schema?: ((dsl: unknown) => unknown) | Record<string, unknown>;
113
+ defaults?: Record<string, unknown | ((context?: unknown, doc?: TDocument) => unknown)>;
114
+ hooks?:
115
+ | {
116
+ beforeCreate?: (context: HookContext) => Promise<void> | void;
117
+ afterCreate?: (context: HookContext) => Promise<void> | void;
118
+ beforeInsert?: (context: HookContext) => Promise<void> | void;
119
+ afterInsert?: (context: HookContext) => Promise<void> | void;
120
+ beforeUpdate?: (context: HookContext) => Promise<void> | void;
121
+ afterUpdate?: (context: HookContext) => Promise<void> | void;
122
+ beforeDelete?: (context: HookContext) => Promise<void> | void;
123
+ afterDelete?: (context: HookContext) => Promise<void> | void;
124
+ beforeFind?: (context: HookContext) => Promise<void> | void;
125
+ afterFind?: (context: HookContext) => Promise<void> | void;
126
+ }
127
+ | V1HooksFactory<TDocument>;
128
+ methods?:
129
+ | Record<string, (this: TDocument & Record<string, unknown>, ...args: unknown[]) => unknown>
130
+ | V1MethodsFactory<TDocument>;
131
+ statics?: Record<string, (...args: unknown[]) => unknown>;
132
+ relations?: Record<string, RelationConfig>;
133
+ virtuals?: Record<string, VirtualConfig>;
134
+ connection?: ModelConnection;
135
+ indexes?: Array<{ key: unknown } & Record<string, unknown>>;
136
+ options?: ModelDefinitionOptions;
137
+ }
138
+
139
+ export interface RegisteredModel<TDocument = Record<string, unknown>> {
140
+ collectionName: string;
141
+ definition: ModelDefinition<TDocument>;
142
+ }
143
+
144
+ export interface ModelScopeOptions {
145
+ database?: string;
146
+ pool?: string;
147
+ }
148
+
149
+ export interface PopulateProxy<T = unknown> extends Promise<T> {
150
+ populate(path: string | PopulateConfig | Array<string | PopulateConfig>, options?: Partial<Omit<PopulateConfig, 'path'>>): PopulateProxy<T>;
151
+ exec(): Promise<T>;
152
+ }
153
+
154
+ type LegacyModelPageInfo<TDocument = any> = unknown extends TDocument ? any : {
155
+ hasNext: boolean;
156
+ hasPrev: boolean;
157
+ startCursor: string | null;
158
+ endCursor: string | null;
159
+ currentPage?: number;
160
+ };
161
+ type LegacyModelTotalsInfo<TDocument = any> = unknown extends TDocument ? any : import('./collection').TotalsInfo;
162
+ type LegacyModelSyncTotalsInfo<TDocument = any> = unknown extends TDocument ? any : (import('./collection').TotalsInfo & {
163
+ mode: 'sync';
164
+ total: number;
165
+ totalPages: number;
166
+ });
167
+ export type RestoreResult = Pick<UpdateResult, 'modifiedCount'> & Partial<UpdateResult>;
168
+
169
+ export type ModelDocument<TDocument = any> = TDocument & Record<string, unknown> & {
170
+ save(): Promise<ModelDocument<TDocument>>;
171
+ remove(): Promise<boolean>;
172
+ validate(): Promise<ValidationResult>;
173
+ populate(path: string | PopulateConfig | Array<string | PopulateConfig>): PopulateProxy<ModelDocument<TDocument> | null>;
174
+ toObject(): TDocument & Record<string, unknown>;
175
+ toJSON(): TDocument & Record<string, unknown>;
176
+ };
177
+
178
+ export interface ModelInstance<TDocument = any> {
179
+ readonly collectionName: string;
180
+ readonly dbName: string;
181
+ readonly poolName?: string;
182
+ readonly definition: ModelDefinition<TDocument>;
183
+ /** 返回当前模型的命名空间元数据,包含实例 ID、类型、数据库和集合名称。 */
184
+ getNamespace(): { iid: string; type: 'mongodb'; db: string; collection: string; };
185
+ /** 返回当前模型声明的关系配置映射。 */
186
+ getRelations(): Record<string, RelationConfig>;
187
+ /** 返回当前模型声明的枚举字段值映射。 */
188
+ getEnums(): Record<string, string>;
189
+ /** 返回底层原生 MongoDB Collection 对象,用于执行框架未封装的原始操作。 */
190
+ raw(): unknown;
191
+ /**
192
+ * 查询符合条件的文档列表。
193
+ * @param query 可选的过滤条件。
194
+ * @param options 可选的查询选项(projection、sort、limit 等)。
195
+ * @returns 文档数组,支持链式 `.populate()` 调用。
196
+ */
197
+ find(query?: unknown, options?: unknown): PopulateProxy<Array<ModelDocument<TDocument>>>;
198
+ /**
199
+ * 查询第一条符合条件的文档。
200
+ * @param query 可选的过滤条件。
201
+ * @param options 可选的查询选项。
202
+ * @returns 匹配的文档,未找到时返回 `null`。
203
+ */
204
+ findOne(query?: unknown, options?: unknown): PopulateProxy<ModelDocument<TDocument> | null>;
205
+ /**
206
+ * 按主键 ID 查询单条文档(`findOne` 的 ID 快捷方式)。
207
+ * @param id 文档主键值。
208
+ * @param options 可选的查询选项。
209
+ * @returns 匹配的文档,未找到时返回 `null`。
210
+ */
211
+ findOneById(id: unknown, options?: unknown): PopulateProxy<ModelDocument<TDocument> | null>;
212
+ /**
213
+ * 按主键 ID 查询单条文档(`findOneById` 的别名)。
214
+ * @param id 文档主键值。
215
+ * @param options 可选的查询选项。
216
+ * @returns 匹配的文档,未找到时返回 `null`。
217
+ */
218
+ findById(id: unknown, options?: unknown): PopulateProxy<ModelDocument<TDocument> | null>;
219
+ /**
220
+ * 按多个主键 ID 批量查询文档。
221
+ * @param ids 主键值数组。
222
+ * @param options 可选的查询选项。
223
+ * @returns 匹配的文档数组。
224
+ */
225
+ findByIds(ids: unknown[], options?: unknown): PopulateProxy<Array<ModelDocument<TDocument>>>;
226
+ /**
227
+ * 分页查询文档,支持基于游标或页码两种分页模式。
228
+ * @param options 分页选项,包含 `limit`、`cursor`/`page`、`filter`、`sort` 等字段。
229
+ * @returns 包含文档列表、分页信息及可选汇总数据的结果对象。
230
+ */
231
+ findPage(options: { totals: { mode: 'sync'; } & Record<string, unknown>; } & Record<string, unknown>): PopulateProxy<{
232
+ items: Array<ModelDocument<TDocument>>;
233
+ pageInfo: LegacyModelPageInfo<TDocument>;
234
+ totals: LegacyModelSyncTotalsInfo<TDocument>;
235
+ meta?: import('./collection').MetaInfo;
236
+ }>;
237
+ findPage(options?: unknown): PopulateProxy<{
238
+ items: Array<ModelDocument<TDocument>>;
239
+ pageInfo: LegacyModelPageInfo<TDocument>;
240
+ totals?: LegacyModelTotalsInfo<TDocument>;
241
+ meta?: import('./collection').MetaInfo;
242
+ }>;
243
+ /**
244
+ * 查询符合条件的文档列表,同时返回未分页的总数。
245
+ * @param query 可选的过滤条件。
246
+ * @param options 可选的查询选项。
247
+ * @returns 包含文档数组和总数的对象。
248
+ */
249
+ findAndCount(query?: unknown, options?: unknown): PopulateProxy<{
250
+ data: Array<ModelDocument<TDocument>>;
251
+ total: number;
252
+ }>;
253
+ /**
254
+ * 统计符合条件的文档数量。
255
+ * @param query 可选的过滤条件。
256
+ * @param options 可选的统计选项。
257
+ * @returns 匹配的文档数量。
258
+ */
259
+ count(query?: unknown, options?: unknown): Promise<number>;
260
+ /**
261
+ * 插入单条文档。
262
+ * @param document 要插入的文档数据。
263
+ * @param options 可选的写入选项。
264
+ * @returns 包含插入 ID 的结果对象。
265
+ */
266
+ insertOne(document?: unknown, options?: unknown): Promise<InsertOneResult>;
267
+ /**
268
+ * 批量插入多条文档(有序插入,遇错即停)。
269
+ * @param documents 要插入的文档数组。
270
+ * @param options 可选的写入选项。
271
+ */
272
+ insertMany(documents?: unknown[], options?: unknown): Promise<InsertManyResult>;
273
+ /**
274
+ * 更新第一条符合条件的文档。
275
+ * @param filter 过滤条件。
276
+ * @param update 更新操作符文档(如 `$set`、`$inc`)。
277
+ * @param options 可选的更新选项。
278
+ */
279
+ updateOne(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateResult>;
280
+ /**
281
+ * 更新所有符合条件的文档。
282
+ * @param filter 过滤条件。
283
+ * @param update 更新操作符文档。
284
+ * @param options 可选的更新选项。
285
+ */
286
+ updateMany(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateResult>;
287
+ /**
288
+ * 替换第一条符合条件的文档(整体替换,不使用更新操作符)。
289
+ * @param filter 过滤条件。
290
+ * @param replacement 替换后的完整文档。
291
+ * @param options 可选的替换选项。
292
+ */
293
+ replaceOne(filter?: unknown, replacement?: unknown, options?: unknown): Promise<UpdateResult>;
294
+ /**
295
+ * 原子地查找并更新单条文档,返回更新后的文档。
296
+ * @param filter 过滤条件。
297
+ * @param update 更新操作符文档。
298
+ * @param options 可选的选项(如 `returnDocument: 'after'`)。
299
+ * @returns 更新后的文档,未找到时返回 `null`。
300
+ */
301
+ findOneAndUpdate(filter?: unknown, update?: unknown, options?: unknown): Promise<TDocument | null>;
302
+ /**
303
+ * 原子地查找并替换单条文档,返回替换后的文档。
304
+ * @param filter 过滤条件。
305
+ * @param replacement 替换后的完整文档。
306
+ * @param options 可选的选项。
307
+ * @returns 替换后的文档,未找到时返回 `null`。
308
+ */
309
+ findOneAndReplace(filter?: unknown, replacement?: unknown, options?: unknown): Promise<TDocument | null>;
310
+ /**
311
+ * 原子地查找并删除单条文档,返回被删除的文档。
312
+ * @param filter 过滤条件。
313
+ * @param options 可选的选项。
314
+ * @returns 被删除的文档,未找到时返回 `null`。
315
+ */
316
+ findOneAndDelete(filter?: unknown, options?: unknown): Promise<TDocument | null>;
317
+ /**
318
+ * 若文档存在则更新,否则插入(upsert 语义)。
319
+ * @param filter 过滤条件。
320
+ * @param update 更新操作符文档。
321
+ * @param options 可选的更新选项。
322
+ * @returns 标准 `UpdateResult` 对象。
323
+ */
324
+ upsertOne(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateResult>;
325
+ /**
326
+ * 对符合条件的单条文档的指定字段执行原子自增操作。
327
+ * @param filter 过滤条件。
328
+ * @param field 字段名或字段-增量映射对象。
329
+ * @param increment 增量值(`field` 为字符串时使用)。
330
+ * @param options 可选的更新选项。
331
+ */
332
+ incrementOne(filter?: unknown, field?: string | Record<string, number>, increment?: number, options?: unknown): Promise<IncrementOneResult<TDocument>>;
333
+ /**
334
+ * 删除第一条符合条件的文档。
335
+ * @param filter 过滤条件。
336
+ * @param options 可选的删除选项。
337
+ * @returns 标准 `DeleteResult` 对象。
338
+ */
339
+ deleteOne(filter?: unknown, options?: unknown): Promise<DeleteResult>;
340
+ /**
341
+ * 删除所有符合条件的文档。
342
+ * @param filter 过滤条件。
343
+ * @param options 可选的删除选项。
344
+ * @returns 标准 `DeleteResult` 对象。
345
+ */
346
+ deleteMany(filter?: unknown, options?: unknown): Promise<DeleteResult>;
347
+ // soft-delete extended methods
348
+ /**
349
+ * 查询包含软删除文档在内的所有匹配文档。
350
+ * @param query 可选的过滤条件。
351
+ * @param options 可选的查询选项。
352
+ */
353
+ findWithDeleted(query?: unknown, options?: unknown): PopulateProxy<Array<ModelDocument<TDocument>>>;
354
+ /**
355
+ * 仅查询已被软删除的文档。
356
+ * @param query 可选的过滤条件。
357
+ * @param options 可选的查询选项。
358
+ */
359
+ findOnlyDeleted(query?: unknown, options?: unknown): PopulateProxy<Array<ModelDocument<TDocument>>>;
360
+ /**
361
+ * 查询第一条符合条件的文档(包含软删除文档)。
362
+ * @param query 可选的过滤条件。
363
+ * @param options 可选的查询选项。
364
+ * @returns 匹配的文档,未找到时返回 `null`。
365
+ */
366
+ findOneWithDeleted(query?: unknown, options?: unknown): PopulateProxy<ModelDocument<TDocument> | null>;
367
+ /**
368
+ * 恢复第一条符合条件的软删除文档。
369
+ * @param filter 过滤条件。
370
+ * @param options 可选的更新选项。
371
+ */
372
+ restore(filter?: unknown, options?: unknown): Promise<RestoreResult>;
373
+ /**
374
+ * 批量恢复所有符合条件的软删除文档。
375
+ * @param filter 过滤条件。
376
+ * @param options 可选的更新选项。
377
+ */
378
+ restoreMany(filter?: unknown, options?: unknown): Promise<RestoreResult>;
379
+ /**
380
+ * 物理删除第一条符合条件的文档(绕过软删除机制)。
381
+ * @param filter 过滤条件。
382
+ * @param options 可选的删除选项。
383
+ * @returns 标准 `DeleteResult` 对象。
384
+ */
385
+ forceDelete(filter?: unknown, options?: unknown): Promise<DeleteResult>;
386
+ /**
387
+ * 物理删除所有符合条件的文档(绕过软删除机制)。
388
+ * @param filter 过滤条件。
389
+ * @param options 可选的删除选项。
390
+ * @returns 标准 `DeleteResult` 对象。
391
+ */
392
+ forceDeleteMany(filter?: unknown, options?: unknown): Promise<DeleteResult>;
393
+ /**
394
+ * 查询第一条仅在软删除范围内匹配的文档。
395
+ * @param query 可选的过滤条件。
396
+ * @param options 可选的查询选项。
397
+ * @returns 匹配的已删除文档,未找到时返回 `null`。
398
+ */
399
+ findOneOnlyDeleted(query?: unknown, options?: unknown): PopulateProxy<ModelDocument<TDocument> | null>;
400
+ /**
401
+ * 统计包含软删除文档在内的匹配数量。
402
+ * @param query 可选的过滤条件。
403
+ * @param options 可选的统计选项。
404
+ */
405
+ countWithDeleted(query?: unknown, options?: unknown): Promise<number>;
406
+ /**
407
+ * 统计已被软删除的文档数量。
408
+ * @param query 可选的过滤条件。
409
+ * @param options 可选的统计选项。
410
+ */
411
+ countOnlyDeleted(query?: unknown, options?: unknown): Promise<number>;
412
+ /**
413
+ * 使用写队列批量插入大量文档,适合高吞吐写入场景。
414
+ * @param docs 要插入的文档数组。
415
+ * @param options 可选的批量写入选项。
416
+ */
417
+ insertBatch(docs: unknown[], options?: unknown): Promise<InsertBatchResult>;
418
+ /**
419
+ * 批量更新符合条件的文档(底层使用 `bulkWrite`)。
420
+ * @param filter 过滤条件。
421
+ * @param update 更新操作符文档。
422
+ * @param options 可选的批量写入选项。
423
+ */
424
+ updateBatch(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateBatchResult>;
425
+ /** 批量删除符合条件的文档。 */
426
+ deleteBatch(filter?: unknown, options?: unknown): Promise<DeleteBatchResult>;
427
+ /**
428
+ * 在集合上创建单个索引。
429
+ * @param keys 索引键规范对象。
430
+ * @param options 可选的索引选项(如 `unique`、`sparse`)。
431
+ * @returns 索引创建结果。
432
+ */
433
+ createIndex(keys: unknown, options?: unknown): Promise<IndexCreateResult>;
434
+ /**
435
+ * 批量创建多个索引。
436
+ * @param specs 索引规范数组,每项包含 `key` 及可选的索引选项。
437
+ * @returns 已创建索引的名称数组。
438
+ */
439
+ createIndexes(specs: Array<{ key: unknown; } & Record<string, unknown>>): Promise<string[]>;
440
+ /** 列出集合上所有现有索引的定义信息。 */
441
+ listIndexes(): Promise<Record<string, unknown>[]>;
442
+ /**
443
+ * 按名称删除指定索引。
444
+ * @param name 索引名称。
445
+ */
446
+ dropIndex(name: string): Promise<unknown>;
447
+ /** 删除集合上的所有非 `_id` 索引。 */
448
+ dropIndexes(): Promise<unknown>;
449
+ /** 预热游标分页书签缓存。 */
450
+ prewarmBookmarks(keyDims?: unknown, pages?: number[]): Promise<BookmarkPrewarmResult>;
451
+ /** 列出游标分页书签缓存。 */
452
+ listBookmarks(keyDims?: unknown): Promise<BookmarkListResult>;
453
+ /** 清理游标分页书签缓存。 */
454
+ clearBookmarks(keyDims?: unknown): Promise<BookmarkClearResult>;
455
+ /**
456
+ * 获取指定字段在符合条件的文档中的所有唯一值。
457
+ * @param key 目标字段名。
458
+ * @param query 可选的过滤条件。
459
+ * @param options 可选的驱动级选项。
460
+ * @returns 唯一值数组。
461
+ */
462
+ distinct(key: string, query?: unknown, options?: unknown): Promise<unknown[]>;
463
+ /**
464
+ * 执行聚合管道并返回结果数组。
465
+ * @param pipeline 聚合阶段数组。
466
+ * @param options 可选的聚合选项(如 `allowDiskUse`)。
467
+ * @returns 聚合结果文档数组。
468
+ */
469
+ aggregate(pipeline?: unknown[], options?: unknown): Promise<unknown[]>;
470
+ /** 返回匹配查询的可读流。 */
471
+ stream(query?: unknown, options?: unknown): NodeJS.ReadableStream;
472
+ /** 返回 MongoDB 查询执行计划。 */
473
+ explain(query?: unknown, options?: unknown): Promise<unknown>;
474
+ /** 手动失效当前 Model 对应集合的读缓存。 */
475
+ invalidate(op?: 'find' | 'findOne' | 'count' | 'findPage' | 'aggregate' | 'distinct'): Promise<number>;
476
+ /** 删除当前 Model 对应集合。 */
477
+ dropCollection(): Promise<boolean>;
478
+ /** 创建当前或指定名称的集合。 */
479
+ createCollection(name?: string, options?: Record<string, unknown>): Promise<boolean>;
480
+ /** 创建 MongoDB view。 */
481
+ createView(name: string, source: string, pipeline?: unknown[]): Promise<boolean>;
482
+ /** 返回索引使用统计。 */
483
+ indexStats(): Promise<unknown[]>;
484
+ /** 设置集合 JSON Schema validator。 */
485
+ setValidator(validator: unknown, options?: { validationLevel?: string; validationAction?: string }): Promise<{ ok: number; collection: string }>;
486
+ /** 设置集合 validation level。 */
487
+ setValidationLevel(level: 'off' | 'moderate' | 'strict' | string): Promise<{ ok: number; validationLevel: string }>;
488
+ /** 设置集合 validation action。 */
489
+ setValidationAction(action: 'error' | 'warn' | string): Promise<{ ok: number; validationAction: string }>;
490
+ /** 读取集合 validator 与校验设置。 */
491
+ getValidator(): Promise<{ validator: Record<string, unknown> | null; validationLevel: string; validationAction: string }>;
492
+ /** 返回集合存储与索引统计。 */
493
+ stats(options?: { scale?: number }): Promise<{ ns: string; count: number; size: number; storageSize: number; totalIndexSize: number; nindexes: number; avgObjSize?: number; scaleFactor?: number }>;
494
+ /** 重命名当前 Model 对应集合。 */
495
+ renameCollection(newName: string, options?: { dropTarget?: boolean }): Promise<{ renamed: boolean; from: string; to: string }>;
496
+ /** 执行 collMod 管理命令。 */
497
+ collMod(modifications: Record<string, unknown>): Promise<Record<string, unknown>>;
498
+ /** 将集合转换为 capped collection。 */
499
+ convertToCapped(size: number, options?: { max?: number }): Promise<{ ok: number; collection: string; capped: boolean; size: number }>;
500
+ /**
501
+ * 打开集合的 ChangeStream 以监听实时变更事件。
502
+ * @param pipeline 可选的聚合过滤管道。
503
+ * @param options 可选的 ChangeStream 选项。
504
+ * @returns MongoDB 原生 ChangeStream 对象。
505
+ */
506
+ watch(pipeline?: unknown[], options?: unknown): import('mongodb').ChangeStream;
507
+ /**
508
+ * 根据模型 schema 定义验证文档数据的合法性。
509
+ * @param document 要验证的文档对象。
510
+ * @returns 包含 `valid` 标志和错误详情的验证结果对象。
511
+ */
512
+ validate(document?: unknown): ValidationResult;
513
+ }
514
+
515
+ export declare class Model {
516
+ static define<TDocument = Record<string, unknown>>(collectionName: string, definition: ModelDefinition<TDocument>): void;
517
+ static get<TDocument = Record<string, unknown>>(collectionName: string): RegisteredModel<TDocument> | undefined;
518
+ static has(collectionName: string): boolean;
519
+ static list(): string[];
520
+ static undefine(collectionName: string): boolean;
521
+ static redefine<TDocument = Record<string, unknown>>(collectionName: string, definition: ModelDefinition<TDocument>): void;
522
+ static _clear(): void;
523
+ }
524
+
525
+
526
+
@@ -0,0 +1,49 @@
1
+ import type { Db, MongoClient, MongoClientOptions } from 'mongodb';
2
+ import type { SSHConfig } from './monsqlize';
3
+
4
+ export interface MongoConnectConfig {
5
+ uri?: string;
6
+ options?: MongoClientOptions;
7
+ /** v1 compat: read preference shortcut merged into MongoClient options. */
8
+ readPreference?: MongoClientOptions['readPreference'];
9
+ /**
10
+ * v1 compat: when true, automatically starts mongodb-memory-server without requiring a uri.
11
+ * For testing only.
12
+ */
13
+ useMemoryServer?: boolean;
14
+ /** Instance/binary configuration options for mongodb-memory-server. */
15
+ memoryServerOptions?: {
16
+ instance?: { port?: number; dbName?: string; storageEngine?: string; replSet?: string };
17
+ binary?: { version?: string };
18
+ [key: string]: unknown;
19
+ };
20
+ /**
21
+ * SSH tunnel configuration. When provided, monSQLize establishes an SSH port-forward
22
+ * through the specified bastion host before connecting to MongoDB.
23
+ * The remote host/port are auto-parsed from `uri` unless overridden by `remoteHost`/`remotePort`.
24
+ * @since v1.3.0
25
+ */
26
+ ssh?: SSHConfig;
27
+ /**
28
+ * Explicit remote MongoDB host visible from the SSH server.
29
+ * Overrides the host auto-parsed from `uri` when `ssh` is set.
30
+ * @since v1.3.0
31
+ */
32
+ remoteHost?: string;
33
+ /**
34
+ * Explicit remote MongoDB port visible from the SSH server.
35
+ * Overrides the port auto-parsed from `uri` when `ssh` is set.
36
+ * @since v1.3.0
37
+ */
38
+ remotePort?: number;
39
+ /** @alias remoteHost — v1 SSH config field */
40
+ mongoHost?: string;
41
+ /** @alias remotePort — v1 SSH config field */
42
+ mongoPort?: number;
43
+ }
44
+
45
+ export interface MongoConnectionState {
46
+ client: MongoClient;
47
+ db: Db;
48
+ }
49
+