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.
- package/CHANGELOG.md +56 -60
- package/LICENSE +201 -21
- package/README.md +537 -1828
- package/changelogs/README.md +160 -0
- package/changelogs/v2.0.0.md +222 -0
- package/dist/cjs/index.cjs +10600 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10650 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +526 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +67 -69
- package/index.d.ts +0 -206
- package/index.mjs +0 -52
- package/lib/cache-invalidation.js +0 -279
- package/lib/cache.js +0 -530
- package/lib/common/cursor.js +0 -59
- package/lib/common/docs-urls.js +0 -73
- package/lib/common/index-options.js +0 -223
- package/lib/common/log.js +0 -61
- package/lib/common/namespace.js +0 -22
- package/lib/common/normalize.js +0 -34
- package/lib/common/page-result.js +0 -43
- package/lib/common/runner.js +0 -57
- package/lib/common/server-features.js +0 -232
- package/lib/common/shape-builders.js +0 -27
- package/lib/common/validation.js +0 -113
- package/lib/connect.js +0 -99
- package/lib/constants.js +0 -55
- package/lib/count-queue.js +0 -188
- package/lib/distributed-cache-invalidator.js +0 -260
- package/lib/errors.js +0 -168
- package/lib/expression/cache/ExpressionCache.js +0 -114
- package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
- package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
- package/lib/expression/detector.js +0 -84
- package/lib/expression/factory.js +0 -29
- package/lib/expression/index.js +0 -19
- package/lib/function-cache.js +0 -533
- package/lib/index.js +0 -1251
- package/lib/infrastructure/ConnectionPoolManager.js +0 -464
- package/lib/infrastructure/HealthChecker.js +0 -281
- package/lib/infrastructure/PoolConfig.js +0 -199
- package/lib/infrastructure/PoolSelector.js +0 -225
- package/lib/infrastructure/PoolStats.js +0 -244
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
- package/lib/infrastructure/ssh-tunnel.js +0 -41
- package/lib/infrastructure/uri-parser.js +0 -36
- package/lib/lock/Lock.js +0 -67
- package/lib/lock/errors.js +0 -28
- package/lib/lock/index.js +0 -13
- package/lib/logger.js +0 -225
- package/lib/model/examples/test.js +0 -311
- package/lib/model/features/defaults.js +0 -161
- package/lib/model/features/populate.js +0 -568
- package/lib/model/features/relations.js +0 -120
- package/lib/model/features/soft-delete.js +0 -349
- package/lib/model/features/version.js +0 -157
- package/lib/model/features/virtuals.js +0 -219
- package/lib/model/index.js +0 -1265
- package/lib/mongodb/common/accessor-helpers.js +0 -59
- package/lib/mongodb/common/agg-pipeline.js +0 -36
- package/lib/mongodb/common/aggregation-validator.js +0 -127
- package/lib/mongodb/common/iid.js +0 -28
- package/lib/mongodb/common/lexicographic-expr.js +0 -53
- package/lib/mongodb/common/shape.js +0 -32
- package/lib/mongodb/common/sort.js +0 -39
- package/lib/mongodb/common/transaction-aware.js +0 -25
- package/lib/mongodb/connect.js +0 -234
- package/lib/mongodb/index.js +0 -639
- package/lib/mongodb/management/admin-ops.js +0 -200
- package/lib/mongodb/management/bookmark-ops.js +0 -167
- package/lib/mongodb/management/cache-ops.js +0 -50
- package/lib/mongodb/management/collection-ops.js +0 -387
- package/lib/mongodb/management/database-ops.js +0 -202
- package/lib/mongodb/management/index-ops.js +0 -475
- package/lib/mongodb/management/index.js +0 -17
- package/lib/mongodb/management/namespace.js +0 -31
- package/lib/mongodb/management/validation-ops.js +0 -268
- package/lib/mongodb/queries/aggregate.js +0 -172
- package/lib/mongodb/queries/chain.js +0 -631
- package/lib/mongodb/queries/count.js +0 -99
- package/lib/mongodb/queries/distinct.js +0 -78
- package/lib/mongodb/queries/find-and-count.js +0 -193
- package/lib/mongodb/queries/find-by-ids.js +0 -236
- package/lib/mongodb/queries/find-one-by-id.js +0 -171
- package/lib/mongodb/queries/find-one.js +0 -71
- package/lib/mongodb/queries/find-page.js +0 -618
- package/lib/mongodb/queries/find.js +0 -171
- package/lib/mongodb/queries/index.js +0 -51
- package/lib/mongodb/queries/watch.js +0 -538
- package/lib/mongodb/writes/common/batch-retry.js +0 -65
- package/lib/mongodb/writes/delete-batch.js +0 -323
- package/lib/mongodb/writes/delete-many.js +0 -181
- package/lib/mongodb/writes/delete-one.js +0 -173
- package/lib/mongodb/writes/find-one-and-delete.js +0 -203
- package/lib/mongodb/writes/find-one-and-replace.js +0 -239
- package/lib/mongodb/writes/find-one-and-update.js +0 -240
- package/lib/mongodb/writes/increment-one.js +0 -259
- package/lib/mongodb/writes/index.js +0 -46
- package/lib/mongodb/writes/insert-batch.js +0 -508
- package/lib/mongodb/writes/insert-many.js +0 -223
- package/lib/mongodb/writes/insert-one.js +0 -169
- package/lib/mongodb/writes/replace-one.js +0 -226
- package/lib/mongodb/writes/result-handler.js +0 -237
- package/lib/mongodb/writes/update-batch.js +0 -416
- package/lib/mongodb/writes/update-many.js +0 -275
- package/lib/mongodb/writes/update-one.js +0 -273
- package/lib/mongodb/writes/upsert-one.js +0 -203
- package/lib/multi-level-cache.js +0 -244
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -267
- package/lib/saga/SagaContext.js +0 -67
- package/lib/saga/SagaDefinition.js +0 -32
- package/lib/saga/SagaExecutor.js +0 -201
- package/lib/saga/SagaOrchestrator.js +0 -186
- package/lib/saga/index.js +0 -11
- package/lib/slow-query-log/base-storage.js +0 -70
- package/lib/slow-query-log/batch-queue.js +0 -97
- package/lib/slow-query-log/config-manager.js +0 -196
- package/lib/slow-query-log/index.js +0 -238
- package/lib/slow-query-log/mongodb-storage.js +0 -324
- package/lib/slow-query-log/query-hash.js +0 -39
- package/lib/sync/ChangeStreamSyncManager.js +0 -405
- package/lib/sync/ResumeTokenStore.js +0 -192
- package/lib/sync/SyncConfig.js +0 -127
- package/lib/sync/SyncTarget.js +0 -240
- package/lib/sync/index.js +0 -20
- package/lib/transaction/CacheLockManager.js +0 -162
- package/lib/transaction/DistributedCacheLockManager.js +0 -475
- package/lib/transaction/Transaction.js +0 -315
- package/lib/transaction/TransactionManager.js +0 -267
- package/lib/transaction/index.js +0 -11
- package/lib/utils/objectid-converter.js +0 -632
- package/types/README.md +0 -122
- package/types/base.ts +0 -94
- package/types/batch.ts +0 -187
- package/types/cache.ts +0 -71
- package/types/chain.ts +0 -254
- package/types/collection.ts +0 -357
- package/types/expression.ts +0 -109
- package/types/function-cache.d.ts +0 -135
- package/types/lock.ts +0 -95
- package/types/model/definition.ts +0 -152
- package/types/model/index.ts +0 -10
- package/types/model/instance.ts +0 -121
- package/types/model/relations.ts +0 -121
- package/types/model/virtuals.ts +0 -32
- package/types/monsqlize.ts +0 -245
- package/types/options.ts +0 -192
- package/types/pagination.ts +0 -154
- package/types/pool.ts +0 -125
- package/types/query.ts +0 -71
- package/types/saga.ts +0 -125
- package/types/stream.ts +0 -64
- package/types/sync.ts +0 -79
- package/types/transaction.ts +0 -79
- package/types/write.ts +0 -77
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* find 查询模块
|
|
3
|
-
* @description 提供多条记录查询功能,支持投影、排序、分页、缓存和流式返回
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { normalizeProjection, normalizeSort } = require('../../common/normalize');
|
|
7
|
-
const { FindChain } = require('./chain');
|
|
8
|
-
const { convertObjectIdStrings } = require('../../utils/objectid-converter');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 创建 find 查询操作
|
|
12
|
-
* @param {Object} context - 上下文对象
|
|
13
|
-
* @param {Object} context.collection - MongoDB 集合实例
|
|
14
|
-
* @param {Object} context.defaults - 默认配置
|
|
15
|
-
* @param {Function} context.run - 缓存执行器
|
|
16
|
-
* @param {string} context.instanceId - 实例ID
|
|
17
|
-
* @param {string} context.effectiveDbName - 数据库名
|
|
18
|
-
* @param {Object} context.logger - 日志器
|
|
19
|
-
* @param {Function} context.emit - 事件发射器
|
|
20
|
-
* @param {Object} context.mongoSlowLogShaper - 慢查询日志格式化器
|
|
21
|
-
* @returns {Object} 包含 find 和 stream 方法的对象
|
|
22
|
-
*/
|
|
23
|
-
function createFindOps(context) {
|
|
24
|
-
const {
|
|
25
|
-
collection,
|
|
26
|
-
defaults,
|
|
27
|
-
run,
|
|
28
|
-
instanceId,
|
|
29
|
-
effectiveDbName,
|
|
30
|
-
logger,
|
|
31
|
-
emit,
|
|
32
|
-
mongoSlowLogShaper
|
|
33
|
-
} = context;
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
/**
|
|
37
|
-
* 查询多条记录
|
|
38
|
-
* @param {Object} [query={}] - 查询条件,使用 MongoDB 查询语法
|
|
39
|
-
* @param {Object} [options={}] - 查询选项 { projection, sort, limit, skip, cache, maxTimeMS, stream, explain }
|
|
40
|
-
* @param {Object|Array} [options.projection] - 字段投影配置
|
|
41
|
-
* @param {Object} [options.sort] - 排序配置
|
|
42
|
-
* @param {number} [options.limit] - 限制返回数量
|
|
43
|
-
* @param {number} [options.skip] - 跳过记录数
|
|
44
|
-
* @param {number} [options.cache] - 缓存时间(毫秒)
|
|
45
|
-
* @param {number} [options.maxTimeMS] - 查询超时时间(毫秒)
|
|
46
|
-
* @param {boolean} [options.stream] - 是否使用流式返回
|
|
47
|
-
* @param {number} [options.batchSize] - 批处理大小
|
|
48
|
-
* @param {boolean|string} [options.explain] - 是否返回查询执行计划,可选值:true/'queryPlanner'/'executionStats'/'allPlansExecution'
|
|
49
|
-
* @param {string} [options.hint] - 索引提示
|
|
50
|
-
* @param {Object} [options.collation] - 排序规则
|
|
51
|
-
* @param {string} [options.comment] - 查询注释
|
|
52
|
-
* @returns {Promise<Array>|ReadableStream|FindChain} 记录数组或可读流(当 stream: true 时);当 explain=true 时返回执行计划;默认返回 FindChain 实例支持链式调用
|
|
53
|
-
*/
|
|
54
|
-
find: (query = {}, options = {}) => {
|
|
55
|
-
// ✅ v1.3.0: 自动转换 ObjectId 字符串
|
|
56
|
-
const convertedQuery = convertObjectIdStrings(query, 'query', 0, new WeakSet(), {
|
|
57
|
-
logger: context.logger,
|
|
58
|
-
excludeFields: context.autoConvertConfig?.excludeFields,
|
|
59
|
-
customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
|
|
60
|
-
maxDepth: context.autoConvertConfig?.maxDepth
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// 如果没有提供 options 或 options 为空对象,返回 FindChain 以支持完整的链式调用
|
|
64
|
-
const hasOptions = options && Object.keys(options).length > 0;
|
|
65
|
-
|
|
66
|
-
if (!hasOptions) {
|
|
67
|
-
// 返回 FindChain 实例,支持 .limit().skip().sort() 等链式调用
|
|
68
|
-
return new FindChain(context, convertedQuery, {});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 如果提供了 options,执行原有逻辑(向后兼容)
|
|
72
|
-
options.projection = normalizeProjection(options.projection);
|
|
73
|
-
const {
|
|
74
|
-
projection,
|
|
75
|
-
limit = defaults.findLimit,
|
|
76
|
-
skip,
|
|
77
|
-
maxTimeMS = defaults.maxTimeMS,
|
|
78
|
-
stream = false,
|
|
79
|
-
batchSize,
|
|
80
|
-
explain,
|
|
81
|
-
comment
|
|
82
|
-
} = options;
|
|
83
|
-
const sort = normalizeSort(options.sort);
|
|
84
|
-
|
|
85
|
-
const driverOpts = { projection, sort, skip, maxTimeMS, ...(options.hint ? { hint: options.hint } : {}), ...(options.collation ? { collation: options.collation } : {}) };
|
|
86
|
-
if (limit !== undefined) driverOpts.limit = limit;
|
|
87
|
-
if (batchSize !== undefined) driverOpts.batchSize = batchSize;
|
|
88
|
-
if (comment) driverOpts.comment = comment;
|
|
89
|
-
|
|
90
|
-
// 如果启用 explain,直接返回执行计划(不缓存)
|
|
91
|
-
if (explain) {
|
|
92
|
-
const verbosity = typeof explain === 'string' ? explain : 'queryPlanner';
|
|
93
|
-
const cursor = collection.find(convertedQuery, driverOpts);
|
|
94
|
-
return cursor.explain(verbosity);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 如果启用流式返回,直接返回 MongoDB 游标流
|
|
98
|
-
if (stream) {
|
|
99
|
-
const cursor = collection.find(convertedQuery, driverOpts);
|
|
100
|
-
const readableStream = cursor.stream();
|
|
101
|
-
|
|
102
|
-
// 添加慢查询日志支持
|
|
103
|
-
const startTime = Date.now();
|
|
104
|
-
let docCount = 0;
|
|
105
|
-
|
|
106
|
-
readableStream.on('data', () => {
|
|
107
|
-
docCount++;
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
readableStream.on('end', () => {
|
|
111
|
-
const durationMs = Date.now() - startTime;
|
|
112
|
-
const slowQueryMs = defaults?.slowQueryMs || 500;
|
|
113
|
-
|
|
114
|
-
if (durationMs >= slowQueryMs) {
|
|
115
|
-
try {
|
|
116
|
-
const meta = {
|
|
117
|
-
op: 'stream',
|
|
118
|
-
durationMs,
|
|
119
|
-
docCount,
|
|
120
|
-
iid: instanceId,
|
|
121
|
-
type: context.type,
|
|
122
|
-
db: effectiveDbName,
|
|
123
|
-
collection: collection.collectionName,
|
|
124
|
-
query: mongoSlowLogShaper?.sanitize ? mongoSlowLogShaper.sanitize(query) : query,
|
|
125
|
-
limit,
|
|
126
|
-
};
|
|
127
|
-
logger?.warn?.('🐌 Slow stream query', meta);
|
|
128
|
-
emit?.('slow-query', meta);
|
|
129
|
-
} catch (_) { }
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
return readableStream;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 执行查询的 Promise
|
|
137
|
-
const resultPromise = run(
|
|
138
|
-
'find',
|
|
139
|
-
{ query: convertedQuery, ...options },
|
|
140
|
-
async () => collection.find(convertedQuery, driverOpts).toArray()
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
// 添加 explain 方法支持链式调用(与原生 MongoDB 一致)
|
|
144
|
-
resultPromise.explain = async (verbosity = 'queryPlanner') => {
|
|
145
|
-
const cursor = collection.find(convertedQuery, driverOpts);
|
|
146
|
-
return cursor.explain(verbosity);
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
return resultPromise;
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 流式查询多条记录(语法糖方法)
|
|
154
|
-
* @description 这是 find(query, {...options, stream: true}) 的便捷方法
|
|
155
|
-
* @param {Object} [query={}] - 查询条件
|
|
156
|
-
* @param {Object} [options={}] - { projection, sort, limit, skip, maxTimeMS, batchSize }
|
|
157
|
-
* @returns {ReadableStream} MongoDB 游标流
|
|
158
|
-
*/
|
|
159
|
-
stream: (query = {}, options = {}) => {
|
|
160
|
-
// 注意:这里需要通过回调获取完整的 collection 方法对象
|
|
161
|
-
// 在主文件中会重写这个方法
|
|
162
|
-
return context.getCollectionMethods().find(query, {
|
|
163
|
-
...options,
|
|
164
|
-
stream: true
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
module.exports = createFindOps;
|
|
171
|
-
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 查询模块统一导出
|
|
3
|
-
* @module queries
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { createFindPage, bookmarkKey, buildKeyDimsAuto } = require('./find-page');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 创建 findPage 操作(工厂函数包装)
|
|
10
|
-
* @param {Object} context - 上下文对象
|
|
11
|
-
* @returns {Object} 包含 findPage 方法
|
|
12
|
-
*/
|
|
13
|
-
function createFindPageOps(context) {
|
|
14
|
-
const { collection, getCache, instanceId, type, effectiveDbName, defaults, logger, run } = context;
|
|
15
|
-
|
|
16
|
-
// 预构建 ns 字符串,确保书签键稳定
|
|
17
|
-
const nsStr = `${instanceId}:${type}:${effectiveDbName}:${collection.collectionName}`;
|
|
18
|
-
|
|
19
|
-
const findPageImpl = createFindPage({
|
|
20
|
-
collection,
|
|
21
|
-
getCache, // 直接传递 getCache 回调
|
|
22
|
-
getNamespace: () => ({ ns: nsStr, db: effectiveDbName, coll: collection.collectionName }),
|
|
23
|
-
defaults,
|
|
24
|
-
logger,
|
|
25
|
-
databaseName: effectiveDbName,
|
|
26
|
-
collectionName: collection.collectionName,
|
|
27
|
-
run
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
findPage: async (options = {}) => findPageImpl(options)
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
module.exports = {
|
|
36
|
-
createFindOps: require('./find'),
|
|
37
|
-
createFindOneOps: require('./find-one'),
|
|
38
|
-
createFindOneByIdOps: require('./find-one-by-id').createFindOneByIdOps, // findOneById 便利方法
|
|
39
|
-
createFindByIdsOps: require('./find-by-ids').createFindByIdsOps, // findByIds 便利方法
|
|
40
|
-
createFindAndCountOps: require('./find-and-count').createFindAndCountOps, // 新增:findAndCount 便利方法
|
|
41
|
-
createCountOps: require('./count'),
|
|
42
|
-
createAggregateOps: require('./aggregate'),
|
|
43
|
-
createDistinctOps: require('./distinct'),
|
|
44
|
-
createFindPageOps, // 新增工厂函数
|
|
45
|
-
createWatchOps: require('./watch').createWatchOps, // 🆕 watch 方法
|
|
46
|
-
// 导出原始函数和辅助函数供 bookmark 模块使用
|
|
47
|
-
createFindPage,
|
|
48
|
-
bookmarkKey,
|
|
49
|
-
buildKeyDimsAuto
|
|
50
|
-
};
|
|
51
|
-
|