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,219 +0,0 @@
1
- /**
2
- * Virtual Fields Manager - 虚拟字段管理器
3
- *
4
- * 虚拟字段是计算属性,不存储在数据库中,但可以像普通字段一样访问
5
- *
6
- * 使用场景:
7
- * - 组合多个字段(如 firstName + lastName = fullName)
8
- * - 格式化显示(如日期格式化)
9
- * - 计算属性(如年龄从生日计算)
10
- *
11
- * @module lib/model/features/virtuals
12
- * @since v1.0.6
13
- */
14
-
15
- /**
16
- * VirtualManager - 虚拟字段管理器
17
- */
18
- class VirtualManager {
19
- /**
20
- * 构造函数
21
- * @param {Model} model - 所属的 Model 实例
22
- */
23
- constructor(model) {
24
- this.model = model;
25
- this.virtuals = new Map(); // 虚拟字段定义缓存
26
- }
27
-
28
- /**
29
- * 定义虚拟字段
30
- *
31
- * @param {string} name - 虚拟字段名称
32
- * @param {Object} config - 虚拟字段配置
33
- * @param {Function} config.get - getter 函数(必需)
34
- * @param {Function} [config.set] - setter 函数(可选)
35
- *
36
- * @example
37
- * virtuals.define('fullName', {
38
- * get: function() {
39
- * return `${this.firstName} ${this.lastName}`;
40
- * },
41
- * set: function(value) {
42
- * const parts = value.split(' ');
43
- * this.firstName = parts[0];
44
- * this.lastName = parts[1];
45
- * }
46
- * });
47
- */
48
- define(name, config) {
49
- // 验证参数
50
- if (!name || typeof name !== 'string') {
51
- throw new Error('虚拟字段名称必须是字符串');
52
- }
53
-
54
- if (!config || typeof config !== 'object') {
55
- throw new Error('虚拟字段配置必须是对象');
56
- }
57
-
58
- if (typeof config.get !== 'function') {
59
- throw new Error('虚拟字段配置必须包含 get 函数');
60
- }
61
-
62
- if (config.set && typeof config.set !== 'function') {
63
- throw new Error('虚拟字段的 set 必须是函数');
64
- }
65
-
66
- // 保存虚拟字段定义
67
- this.virtuals.set(name, {
68
- get: config.get,
69
- set: config.set || null
70
- });
71
- }
72
-
73
- /**
74
- * 获取虚拟字段定义
75
- * @param {string} name - 虚拟字段名称
76
- * @returns {Object|null} 虚拟字段配置,不存在返回 null
77
- */
78
- get(name) {
79
- return this.virtuals.get(name) || null;
80
- }
81
-
82
- /**
83
- * 获取所有虚拟字段
84
- * @returns {Map} 所有虚拟字段定义
85
- */
86
- getAll() {
87
- return this.virtuals;
88
- }
89
-
90
- /**
91
- * 检查虚拟字段是否存在
92
- * @param {string} name - 虚拟字段名称
93
- * @returns {boolean}
94
- */
95
- has(name) {
96
- return this.virtuals.has(name);
97
- }
98
-
99
- /**
100
- * 应用虚拟字段到文档
101
- *
102
- * @param {Object|Array} docs - 文档或文档数组
103
- * @returns {Object|Array} 应用虚拟字段后的文档
104
- */
105
- apply(docs) {
106
- if (!docs) {
107
- return docs;
108
- }
109
-
110
- // 处理数组
111
- if (Array.isArray(docs)) {
112
- return docs.map(doc => this.applyToDocument(doc));
113
- }
114
-
115
- // 处理单个文档
116
- return this.applyToDocument(docs);
117
- }
118
-
119
- /**
120
- * 应用虚拟字段到单个文档
121
- *
122
- * @private
123
- * @param {Object} doc - 文档对象
124
- * @returns {Object} 应用虚拟字段后的文档
125
- */
126
- applyToDocument(doc) {
127
- if (!doc || typeof doc !== 'object' || Buffer.isBuffer(doc)) {
128
- return doc;
129
- }
130
-
131
- // 为每个虚拟字段定义 getter/setter
132
- for (const [name, config] of this.virtuals.entries()) {
133
- // 使用 Object.defineProperty 定义虚拟字段
134
- Object.defineProperty(doc, name, {
135
- enumerable: true, // 可枚举(会出现在 JSON.stringify 中)
136
- configurable: true, // 可配置
137
- get: function() {
138
- // 绑定 this 到文档对象
139
- return config.get.call(this);
140
- },
141
- set: config.set ? function(value) {
142
- // 如果定义了 setter,绑定 this 到文档对象
143
- config.set.call(this, value);
144
- } : undefined
145
- });
146
- }
147
-
148
- return doc;
149
- }
150
-
151
- /**
152
- * 从文档中移除虚拟字段(用于保存到数据库前)
153
- *
154
- * @param {Object} doc - 文档对象
155
- * @returns {Object} 移除虚拟字段后的文档
156
- */
157
- removeFromDocument(doc) {
158
- if (!doc || typeof doc !== 'object' || Buffer.isBuffer(doc)) {
159
- return doc;
160
- }
161
-
162
- const cleaned = { ...doc };
163
-
164
- // 删除所有虚拟字段
165
- for (const name of this.virtuals.keys()) {
166
- delete cleaned[name];
167
- }
168
-
169
- return cleaned;
170
- }
171
- }
172
-
173
- /**
174
- * 设置虚拟字段功能
175
- *
176
- * @param {ModelInstance} modelInstance - Model 实例
177
- * @param {Object} virtualsConfig - 虚拟字段配置
178
- *
179
- * @example
180
- * setupVirtuals(modelInstance, {
181
- * fullName: {
182
- * get: function() {
183
- * return `${this.firstName} ${this.lastName}`;
184
- * }
185
- * }
186
- * });
187
- */
188
- function setupVirtuals(modelInstance, virtualsConfig) {
189
- // 初始化 VirtualManager
190
- modelInstance._virtuals = new VirtualManager(modelInstance);
191
-
192
- // 注册虚拟字段
193
- if (virtualsConfig && typeof virtualsConfig === 'object') {
194
- for (const [name, config] of Object.entries(virtualsConfig)) {
195
- modelInstance._virtuals.define(name, config);
196
- }
197
- }
198
-
199
- // 扩展查询方法,自动应用虚拟字段
200
- const originalInjectMethods = modelInstance._injectInstanceMethods;
201
- modelInstance._injectInstanceMethods = function(result) {
202
- // 先应用原有的实例方法注入
203
- if (originalInjectMethods) {
204
- originalInjectMethods.call(this, result);
205
- }
206
-
207
- // 再应用虚拟字段
208
- if (this._virtuals) {
209
- this._virtuals.apply(result);
210
- }
211
- };
212
- }
213
-
214
- module.exports = {
215
- VirtualManager,
216
- setupVirtuals
217
- };
218
-
219
-