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
@@ -1,171 +0,0 @@
1
- /**
2
- * findOneById 查询模块
3
- * @description 通过 _id 查询单个文档的便利方法,自动处理 ObjectId 转换
4
- */
5
-
6
- const { ObjectId } = require('mongodb');
7
- const { createError, ErrorCodes } = require('../../errors');
8
-
9
- /**
10
- * 创建 findOneById 查询操作
11
- * @param {Object} context - 上下文对象
12
- * @param {Object} context.collection - MongoDB 集合实例
13
- * @param {Object} context.defaults - 默认配置
14
- * @param {Function} context.run - 缓存执行器
15
- * @param {string} context.instanceId - 实例ID
16
- * @param {string} context.effectiveDbName - 数据库名
17
- * @param {Object} context.logger - 日志器
18
- * @param {Function} context.emit - 事件发射器
19
- * @param {Object} context.mongoSlowLogShaper - 慢查询日志格式化器
20
- * @returns {Function} findOneById 方法
21
- */
22
- function createFindOneByIdOps(context) {
23
- const {
24
- collection,
25
- defaults,
26
- run,
27
- instanceId,
28
- effectiveDbName,
29
- logger,
30
- emit,
31
- mongoSlowLogShaper
32
- } = context;
33
-
34
- /**
35
- * 通过 _id 查询单个文档
36
- * @param {string|ObjectId} id - 文档 _id(字符串会自动转换为 ObjectId)
37
- * @param {Object} [options={}] - 查询选项
38
- * @param {Object|Array} [options.projection] - 字段投影配置
39
- * @param {number} [options.cache] - 缓存时间(毫秒)
40
- * @param {number} [options.maxTimeMS] - 查询超时时间(毫秒)
41
- * @param {string} [options.comment] - 查询注释
42
- * @returns {Promise<Object|null>} 查询到的文档,如果不存在则返回 null
43
- * @throws {Error} 当参数无效时
44
- *
45
- * @example
46
- * // 字符串 ID(自动转换为 ObjectId)
47
- * const user = await collection('users').findOneById('507f1f77bcf86cd799439011');
48
- *
49
- * @example
50
- * // ObjectId(直接使用)
51
- * const { ObjectId } = require('mongodb');
52
- * const user = await collection('users').findOneById(new ObjectId('507f1f77bcf86cd799439011'));
53
- *
54
- * @example
55
- * // 带选项
56
- * const user = await collection('users').findOneById(userId, {
57
- * projection: { name: 1, email: 1 },
58
- * cache: 5000,
59
- * maxTimeMS: 3000
60
- * });
61
- */
62
- const findOneById = async function findOneById(id, options = {}) {
63
- const startTime = Date.now();
64
-
65
- // 1. 参数验证
66
- if (!id) {
67
- throw createError(
68
- ErrorCodes.INVALID_ARGUMENT,
69
- 'id 参数是必需的',
70
- [{ field: 'id', type: 'required', message: 'id 不能为空' }]
71
- );
72
- }
73
-
74
- // 2. 转换 ID 为 ObjectId
75
- let objectId;
76
- try {
77
- if (typeof id === 'string') {
78
- // 验证字符串是否是有效的 ObjectId 格式(24 个十六进制字符)
79
- if (!/^[0-9a-fA-F]{24}$/.test(id)) {
80
- throw createError(
81
- ErrorCodes.INVALID_ARGUMENT,
82
- `无效的 ObjectId 格式: "${id}"`,
83
- [{
84
- field: 'id',
85
- type: 'format',
86
- message: 'id 必须是 24 个十六进制字符的字符串或 ObjectId 实例',
87
- received: id
88
- }]
89
- );
90
- }
91
- objectId = new ObjectId(id);
92
- } else if (id instanceof ObjectId) {
93
- // 只接受 ObjectId 实例
94
- objectId = id;
95
- } else {
96
- // 拒绝其他类型(包括数字、对象等)
97
- throw createError(
98
- ErrorCodes.INVALID_ARGUMENT,
99
- 'id 必须是字符串或 ObjectId 实例',
100
- [{
101
- field: 'id',
102
- type: 'type',
103
- message: `期望: string 或 ObjectId,实际: ${typeof id}`,
104
- received: typeof id
105
- }]
106
- );
107
- }
108
- } catch (error) {
109
- if (error.code === ErrorCodes.INVALID_ARGUMENT) {
110
- throw error;
111
- }
112
- // ObjectId 构造函数抛出的错误
113
- throw createError(
114
- ErrorCodes.INVALID_ARGUMENT,
115
- `无效的 ObjectId: ${error.message}`,
116
- [{ field: 'id', type: 'invalid', message: error.message, received: id }]
117
- );
118
- }
119
-
120
- // 3. 构建查询
121
- const query = { _id: objectId };
122
-
123
- // 4. 标准化选项
124
- const { normalizeProjection } = require('../../common/normalize');
125
- const projection = normalizeProjection(options.projection);
126
- const cache = options.cache || 0;
127
- const maxTimeMS = options.maxTimeMS !== undefined ? options.maxTimeMS : defaults.maxTimeMS;
128
- const comment = options.comment;
129
-
130
- const driverOpts = { projection, maxTimeMS };
131
- if (comment) driverOpts.comment = comment;
132
-
133
- // 5. 执行查询(通过 run 支持缓存)
134
- const result = await run(
135
- 'findOneById',
136
- { id: objectId.toString(), ...options },
137
- async () => collection.findOne(query, driverOpts)
138
- );
139
-
140
- // 6. 慢查询日志
141
- const duration = Date.now() - startTime;
142
- const slowQueryMs = defaults?.slowQueryMs || 1000;
143
-
144
- if (duration >= slowQueryMs) {
145
- try {
146
- const meta = {
147
- operation: 'findOneById',
148
- durationMs: duration,
149
- iid: instanceId,
150
- type: 'mongodb',
151
- db: effectiveDbName,
152
- collection: collection.collectionName,
153
- id: mongoSlowLogShaper?.sanitize ? mongoSlowLogShaper.sanitize(objectId.toString()) : objectId.toString(),
154
- options: mongoSlowLogShaper?.sanitize ? mongoSlowLogShaper.sanitize(options) : options
155
- };
156
- logger?.warn?.('🐌 Slow query: findOneById', meta);
157
- emit?.('slow-query', meta);
158
- } catch (_) {
159
- // 忽略日志错误
160
- }
161
- }
162
-
163
- return result;
164
- };
165
-
166
- return { findOneById };
167
- }
168
-
169
- module.exports = { createFindOneByIdOps };
170
-
171
-
@@ -1,71 +0,0 @@
1
- /**
2
- * findOne 查询模块
3
- * @description 提供单条记录查询功能,支持投影、排序和缓存
4
- */
5
-
6
- const { normalizeProjection, normalizeSort } = require('../../common/normalize');
7
- const { convertObjectIdStrings } = require('../../utils/objectid-converter');
8
-
9
- /**
10
- * 创建 findOne 查询操作
11
- * @param {Object} context - 上下文对象
12
- * @returns {Object} 包含 findOne 方法的对象
13
- */
14
- function createFindOneOps(context) {
15
- const { collection, defaults, run } = context;
16
-
17
- return {
18
- /**
19
- * 查询单条记录
20
- * @description 根据指定条件查询集合中的第一条匹配记录,支持投影、排序和缓存功能
21
- * @param {Object} [query={}] - 查询条件,使用MongoDB查询语法,如 {name: 'John', age: {$gt: 18}}
22
- * @param {Object} [options={}] - 查询选项配置对象
23
- * @param {Object|Array} [options.projection] - 字段投影配置,指定返回的字段
24
- * @param {Object} [options.sort] - 排序配置,如 {createdAt: -1, name: 1},-1降序,1升序
25
- * @param {number} [options.cache=0] - 缓存时间(毫秒),0表示不缓存,>0时结果将被缓存指定时间
26
- * @param {number} [options.maxTimeMS] - 查询超时时间(毫秒),防止长时间查询阻塞
27
- * @param {boolean|string} [options.explain] - 是否返回查询执行计划,可选值:true/'queryPlanner'/'executionStats'/'allPlansExecution'
28
- * @param {string} [options.hint] - 索引提示
29
- * @param {Object} [options.collation] - 排序规则
30
- * @param {string} [options.comment] - 查询注释
31
- * @returns {Promise<Object|null>} 返回匹配的第一条记录对象,未找到时返回null;当 explain=true 时返回执行计划
32
- */
33
- findOne: async (query = {}, options = {}) => {
34
- // ✅ v1.3.0: 自动转换 ObjectId 字符串
35
- const convertedQuery = convertObjectIdStrings(query, 'query', 0, new WeakSet(), {
36
- logger: context.logger,
37
- excludeFields: context.autoConvertConfig?.excludeFields,
38
- customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
39
- maxDepth: context.autoConvertConfig?.maxDepth
40
- });
41
-
42
- options.projection = normalizeProjection(options.projection);
43
- const {
44
- projection,
45
- maxTimeMS = defaults.maxTimeMS,
46
- explain,
47
- comment
48
- } = options;
49
- const sort = normalizeSort(options.sort);
50
-
51
- const driverOpts = { projection, sort, maxTimeMS, ...(options.hint ? { hint: options.hint } : {}), ...(options.collation ? { collation: options.collation } : {}) };
52
- if (comment) driverOpts.comment = comment;
53
-
54
- // 如果启用 explain,直接返回执行计划(不缓存)
55
- if (explain) {
56
- const verbosity = typeof explain === 'string' ? explain : 'queryPlanner';
57
- const cursor = collection.find(convertedQuery, driverOpts).limit(1);
58
- return await cursor.explain(verbosity);
59
- }
60
-
61
- return run(
62
- 'findOne',
63
- { query: convertedQuery, ...options },
64
- () => collection.findOne(convertedQuery, driverOpts)
65
- );
66
- }
67
- };
68
- }
69
-
70
- module.exports = createFindOneOps;
71
-