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
package/index.mjs DELETED
@@ -1,52 +0,0 @@
1
- /**
2
- * monSQLize ES Module Entry Point
3
- *
4
- * 使用方式:
5
- * ```javascript
6
- * import MonSQLize from 'monsqlize';
7
- * // 或
8
- * import { MonSQLize } from 'monsqlize';
9
- *
10
- * const db = new MonSQLize({
11
- * type: 'mongodb',
12
- * config: { uri: 'mongodb://localhost:27017/mydb' }
13
- * });
14
- *
15
- * await db.connect();
16
- * ```
17
- */
18
-
19
- import { createRequire } from 'module';
20
- const require = createRequire(import.meta.url);
21
-
22
- // 使用 require 导入 CommonJS 模块
23
- const MonSQLizeClass = require('./lib/index.js');
24
- const Logger = require('./lib/logger.js');
25
- const MemoryCache = require('./lib/cache.js');
26
- const { createRedisCacheAdapter } = require('./lib/redis-cache-adapter.js');
27
- const TransactionManager = require('./lib/transaction/TransactionManager.js');
28
- const CacheLockManager = require('./lib/transaction/CacheLockManager.js');
29
- const DistributedCacheInvalidator = require('./lib/distributed-cache-invalidator.js');
30
- const { withCache, FunctionCache } = require('./lib/function-cache.js');
31
-
32
- // 默认导出
33
- export default MonSQLizeClass;
34
-
35
- // 🆕 v1.0.9: 导出表达式函数
36
- const { expr, createExpression } = MonSQLizeClass;
37
-
38
- // 命名导出
39
- export {
40
- MonSQLizeClass as MonSQLize,
41
- Logger,
42
- MemoryCache,
43
- createRedisCacheAdapter,
44
- TransactionManager,
45
- CacheLockManager,
46
- DistributedCacheInvalidator,
47
- expr, // 🆕 v1.0.9: 统一表达式函数
48
- createExpression, // 🆕 v1.0.9: 表达式函数别名
49
- withCache, // 🆕 v1.1.4: 函数缓存装饰器
50
- FunctionCache // 🆕 v1.1.4: 函数缓存类
51
- };
52
-
@@ -1,279 +0,0 @@
1
- /**
2
- * 缓存精准失效引擎
3
- * @description 提供精准缓存失效功能,只清除受影响的查询缓存
4
- * @module lib/cache-invalidation
5
- */
6
-
7
- const CacheFactory = require('./cache');
8
-
9
- /**
10
- * 缓存精准失效引擎
11
- * @class
12
- */
13
- class CacheInvalidationEngine {
14
- /**
15
- * 判断文档字段值是否匹配查询条件
16
- * @param {any} docValue - 文档字段值
17
- * @param {any} queryValue - 查询条件值
18
- * @returns {boolean} true=匹配, false=不匹配
19
- *
20
- * @example
21
- * matchesField('active', 'active') // → true
22
- * matchesField(25, { $gt: 20 }) // → true
23
- * matchesField('test', { $in: ['test', 'demo'] }) // → true
24
- */
25
- static matchesField(docValue, queryValue) {
26
- // 1. 简单等值匹配
27
- if (typeof queryValue !== 'object' || queryValue === null) {
28
- return docValue === queryValue;
29
- }
30
-
31
- // 2. $in 操作符
32
- if (queryValue.$in && Array.isArray(queryValue.$in)) {
33
- return queryValue.$in.includes(docValue);
34
- }
35
-
36
- // 3. $gt/$gte/$lt/$lte 操作符
37
- if (queryValue.$gt !== undefined) {
38
- return docValue > queryValue.$gt;
39
- }
40
- if (queryValue.$gte !== undefined) {
41
- return docValue >= queryValue.$gte;
42
- }
43
- if (queryValue.$lt !== undefined) {
44
- return docValue < queryValue.$lt;
45
- }
46
- if (queryValue.$lte !== undefined) {
47
- return docValue <= queryValue.$lte;
48
- }
49
-
50
- // 4. $ne 操作符
51
- if (queryValue.$ne !== undefined) {
52
- return docValue !== queryValue.$ne;
53
- }
54
-
55
- // 5. $exists 操作符
56
- if (queryValue.$exists !== undefined) {
57
- const exists = docValue !== undefined;
58
- return queryValue.$exists ? exists : !exists;
59
- }
60
-
61
- // 6. $eq 操作符(显式等值)
62
- if (queryValue.$eq !== undefined) {
63
- return docValue === queryValue.$eq;
64
- }
65
-
66
- // 7. 不支持的操作符 → 返回 false(跳过匹配)
67
- return false;
68
- }
69
-
70
- /**
71
- * 判断文档是否匹配查询条件
72
- * @param {Object} doc - 写入的文档
73
- * @param {Object} query - 查询条件
74
- * @returns {boolean} true=匹配(需要失效), false=不匹配
75
- *
76
- * @example
77
- * matchesQuery({ status: 'active' }, { status: 'active' }) // → true
78
- * matchesQuery({ status: 'inactive' }, { status: 'active' }) // → false
79
- * matchesQuery({ age: 25 }, { age: { $gt: 20 } }) // → true
80
- */
81
- static matchesQuery(doc, query) {
82
- // 1. 空查询或 null/undefined → 匹配所有
83
- if (!query || Object.keys(query).length === 0) {
84
- return true;
85
- }
86
-
87
- // 2. 逐字段检查(AND 逻辑)
88
- for (const [field, value] of Object.entries(query)) {
89
- if (!this.matchesField(doc[field], value)) {
90
- return false; // 任何字段不匹配 → 不失效
91
- }
92
- }
93
-
94
- // 3. 所有字段都匹配 → 失效
95
- return true;
96
- }
97
-
98
- /**
99
- * 检测查询是否包含复杂操作符
100
- * @param {Object} query - 查询条件
101
- * @returns {boolean} true=包含复杂操作符
102
- *
103
- * @example
104
- * hasComplexOperators({ $or: [...] }) // → true
105
- * hasComplexOperators({ status: 'active' }) // → false
106
- */
107
- static hasComplexOperators(query) {
108
- if (!query || typeof query !== 'object') {
109
- return false;
110
- }
111
-
112
- // 复杂操作符列表(不支持精准失效)
113
- const complexOps = [
114
- '$or', // 逻辑或
115
- '$nor', // 逻辑非或
116
- '$and', // 逻辑与(保守跳过,可选支持)
117
- '$expr', // 表达式求值
118
- '$where', // JavaScript 函数
119
- '$text', // 全文检索
120
- '$regex', // 正则匹配
121
- '$jsonSchema' // Schema 验证
122
- ];
123
-
124
- return complexOps.some(op => query[op] !== undefined);
125
- }
126
-
127
- /**
128
- * 从缓存键中提取查询条件
129
- * @param {string} cacheKey - 缓存键(stableStringify 序列化后的)
130
- * @returns {Object|null} 查询条件对象,无法解析返回 null
131
- *
132
- * @example
133
- * const key = '{"ns":{...},"op":"find","query":{"status":"active"},...}';
134
- * extractQueryFromKey(key) // → { status: 'active' }
135
- */
136
- static extractQueryFromKey(cacheKey) {
137
- try {
138
- // 缓存键格式: stableStringify({ns, op, query, projection, ...})
139
- // 注意:stableStringify 可能输出 undefined(不是有效 JSON),需要先替换
140
- const sanitizedKey = cacheKey.replace(/:undefined/g, ':null');
141
- const keyObj = JSON.parse(sanitizedKey);
142
-
143
- // 提取 query 字段
144
- if (keyObj && typeof keyObj === 'object' && keyObj.query !== undefined && keyObj.query !== null) {
145
- return keyObj.query;
146
- }
147
-
148
- return null;
149
- } catch (err) {
150
- // 解析失败,返回 null
151
- return null;
152
- }
153
- }
154
-
155
- /**
156
- * 合并 filter 和 update 构造文档(用于 upsert)
157
- * @param {Object} filter - 查询条件
158
- * @param {Object} update - 更新操作
159
- * @returns {Object} 合并后的文档
160
- *
161
- * @example
162
- * mergeFilterAndUpdate(
163
- * { userId: 'user123' },
164
- * { $set: { name: 'Alice', age: 25 } }
165
- * ) // → { userId: 'user123', name: 'Alice', age: 25 }
166
- */
167
- static mergeFilterAndUpdate(filter, update) {
168
- const doc = { ...filter };
169
-
170
- if (!update || typeof update !== 'object') {
171
- return doc;
172
- }
173
-
174
- // 提取 $set 字段
175
- if (update.$set && typeof update.$set === 'object') {
176
- Object.assign(doc, update.$set);
177
- }
178
-
179
- // 提取 $inc 字段(简化处理:假设增量字段也可能匹配)
180
- if (update.$inc && typeof update.$inc === 'object') {
181
- Object.assign(doc, update.$inc);
182
- }
183
-
184
- // 提取 $setOnInsert 字段(仅插入时)
185
- if (update.$setOnInsert && typeof update.$setOnInsert === 'object') {
186
- Object.assign(doc, update.$setOnInsert);
187
- }
188
-
189
- // 提取 $unset 字段(设为 undefined)
190
- if (update.$unset && typeof update.$unset === 'object') {
191
- for (const key of Object.keys(update.$unset)) {
192
- doc[key] = undefined;
193
- }
194
- }
195
-
196
- return doc;
197
- }
198
-
199
- /**
200
- * 精准失效缓存
201
- * @param {Object} cache - 缓存实例
202
- * @param {Object} context - 失效上下文
203
- * @param {string} context.instanceId - 实例ID
204
- * @param {string} context.type - 数据库类型
205
- * @param {string} context.db - 数据库名
206
- * @param {string} context.collection - 集合名
207
- * @param {Object} context.document - 受影响的文档
208
- * @param {string} context.operation - 操作类型
209
- * @returns {Promise<number>} 删除的缓存键数量
210
- *
211
- * @example
212
- * await invalidatePrecise(cache, {
213
- * instanceId: 'test',
214
- * type: 'mongodb',
215
- * db: 'shop',
216
- * collection: 'users',
217
- * document: { status: 'active', name: 'Alice' },
218
- * operation: 'insertOne'
219
- * })
220
- */
221
- static async invalidatePrecise(cache, context) {
222
- const { instanceId, type, db, collection, document, operation } = context;
223
-
224
- // 🆕 v1.1.6: 规范化 document 中的 ObjectId 为字符串
225
- // 确保与缓存键中的 query 格式一致(缓存键生成时已规范化)
226
- const CacheFactory = require('./cache');
227
- const normalizedDocument = CacheFactory._normalizeObjectIds(document);
228
-
229
- // Step 1: 构建 namespace 模式,获取集合的所有缓存键
230
- const pattern = CacheFactory.buildNamespacePattern({
231
- iid: instanceId,
232
- type,
233
- db,
234
- collection
235
- });
236
- const allKeys = cache.keys(pattern);
237
-
238
- // 没有缓存,直接返回
239
- if (!allKeys || allKeys.length === 0) {
240
- return 0;
241
- }
242
-
243
- // Step 2: 遍历每个缓存键,判断是否需要失效
244
- const keysToDelete = [];
245
-
246
- for (const key of allKeys) {
247
- // Step 2.1: 解析查询条件
248
- const query = this.extractQueryFromKey(key);
249
- if (query === null) {
250
- // 解析失败(返回 null),跳过该键
251
- continue;
252
- }
253
-
254
- // Step 2.2: 检测是否包含复杂操作符
255
- if (this.hasComplexOperators(query)) {
256
- // 复杂查询,跳过失效(按 TTL 自然过期)
257
- continue;
258
- }
259
-
260
- // Step 2.3: 判断文档是否匹配查询条件
261
- if (this.matchesQuery(normalizedDocument, query)) {
262
- // 匹配成功,标记为需要失效
263
- keysToDelete.push(key);
264
- }
265
- }
266
-
267
- // Step 3: 批量删除匹配的缓存键
268
- if (keysToDelete.length > 0) {
269
- const deleted = await cache.delMany(keysToDelete);
270
- return deleted;
271
- }
272
-
273
- return 0;
274
- }
275
- }
276
-
277
- module.exports = CacheInvalidationEngine;
278
-
279
-