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,618 +0,0 @@
1
- // find-page.js
2
- // 统一的 findPage 实现:支持 after/before 游标、page 跳页(书签+少量 hops)、
3
- // 可选 offset 兜底与 totals(none|async|sync|approx 占位)。
4
-
5
- const crypto = require('crypto');
6
- const { ErrorCodes, createError } = require('../../errors');
7
- const { CACHE, PAGINATION } = require('../../constants');
8
- const { ensureStableSort, reverseSort, pickAnchor } = require('../common/sort');
9
- const { buildPagePipelineA } = require('../common/agg-pipeline');
10
- const { decodeCursor } = require('../../common/cursor');
11
- const { validateLimitAfterBefore, assertCursorSortCompatible } = require('../../common/validation');
12
- const { makePageResult } = require('../../common/page-result');
13
- const { normalizeSort, normalizeProjection } = require('../../common/normalize');
14
- const { convertObjectIdStrings } = require('../../utils/objectid-converter');
15
-
16
- // —— 有效投影计算 ——
17
- /**
18
- * 计算有效投影:自动保护排序字段,确保游标锚点提取(pickAnchor)不受投影影响。
19
- * - 包含型投影(所有非 _id 值均为 1/true):强制追加排序字段(值为 1)
20
- * - 排除型投影(含任意非 _id 字段值为 0/false):从排除列表中移除排序字段
21
- * @param {Record<string,any>|undefined} projection - normalizeProjection 归一化后的投影
22
- * @param {Record<string,1|-1>} sort - 稳定排序(含 _id)
23
- * @returns {Record<string,any>|undefined}
24
- */
25
- function buildEffectiveProjection(projection, sort) {
26
- if (!projection) return undefined;
27
- const sortFields = Object.keys(sort || {});
28
- // 判断排除型:任意非 _id 字段值为 0 或 false
29
- const isExclusion = Object.entries(projection).some(([k, v]) => k !== '_id' && (v === 0 || v === false));
30
- const effective = { ...projection };
31
- if (isExclusion) {
32
- // 排除型:取消对排序字段的排除,确保游标可用
33
- for (const k of sortFields) {
34
- if (effective[k] === 0 || effective[k] === false) {
35
- delete effective[k];
36
- }
37
- }
38
- } else {
39
- // 包含型:强制包含排序字段
40
- for (const k of sortFields) {
41
- if (!effective[k]) effective[k] = 1;
42
- }
43
- }
44
- return effective;
45
- }
46
-
47
- // —— Count 队列支持(高并发控制)——
48
- let countQueue = null;
49
-
50
- function getCountQueue(ctx) {
51
- if (!countQueue && ctx.countQueue?.enabled) {
52
- const CountQueue = require('../../count-queue');
53
- countQueue = new CountQueue(ctx.countQueue);
54
- }
55
- return countQueue;
56
- }
57
-
58
- // —— 工具:稳定序列化与哈希 ——
59
- function deepStableStringify(x) {
60
- if (x === null || x === undefined) return 'null';
61
- if (Array.isArray(x)) {
62
- return `[${x.map(deepStableStringify).join(',')}]`;
63
- }
64
- if (typeof x === 'object') {
65
- const keys = Object.keys(x).sort();
66
- const entries = keys.map(k => `${JSON.stringify(k)}:${deepStableStringify(x[k])}`);
67
- return `{${entries.join(',')}}`;
68
- }
69
- // primitives
70
- return JSON.stringify(x);
71
- }
72
- const stableStringify = (o) => deepStableStringify(o);
73
- const hash = (x) => crypto.createHash('sha256').update(String(x)).digest('hex');
74
-
75
- // —— 去敏形状 ——
76
- function shapeOfQuery(q) {
77
- // 操作符/层级感知的去敏形状(不含具体值)
78
- function walk(o, path = [], acc = []) {
79
- if (!o || typeof o !== 'object') return acc;
80
- const keys = Object.keys(o).sort();
81
- for (const k of keys) {
82
- const isOp = k.startsWith('$');
83
- const key = isOp ? `$${k.slice(1)}` : k;
84
- acc.push([...path, key].join('.'));
85
- const v = o[k];
86
- if (v && typeof v === 'object') walk(v, [...path, key], acc);
87
- }
88
- return acc;
89
- }
90
- return hash(JSON.stringify(walk(q)));
91
- }
92
- function shapeOfPipeline(p) {
93
- // 记录阶段算子序列 + 顶层字段名集合(仍去敏)
94
- const seq = (p || []).map(stage => {
95
- const op = Object.keys(stage)[0];
96
- const body = stage[op];
97
- const keys = body && typeof body === 'object' ? Object.keys(body).sort() : [];
98
- return { op, keys };
99
- });
100
- return hash(JSON.stringify(seq));
101
- }
102
- function buildKeyDimsAuto({ db, coll, sort, limit, query, pipeline }) {
103
- return { db, coll, sort, limit, queryShape: shapeOfQuery(query), pipelineShape: shapeOfPipeline(pipeline) };
104
- }
105
-
106
- // —— 键构造 ——
107
- function bookmarkKey(ns, keyDims) {
108
- const payload = { db: keyDims.db, coll: keyDims.coll, sort: keyDims.sort, limit: keyDims.limit, q: keyDims.queryShape, p: keyDims.pipelineShape };
109
- return `${ns}:bm:${hash(stableStringify(payload))}`;
110
- }
111
- function totalsKey(ns, keyDims) {
112
- const payload = { db: keyDims.db, coll: keyDims.coll, sort: keyDims.sort, limit: keyDims.limit, q: keyDims.queryShape, p: keyDims.pipelineShape };
113
- return `${ns}:tot:${hash(stableStringify(payload))}`;
114
- }
115
-
116
- // —— totals inflight 去重(窗口时间见 CACHE.TOTALS_INFLIGHT_WINDOW_MS) ——
117
- const __totalsInflight = new Map(); // key -> { p: Promise, t0: number }
118
- function withTotalsInflight(key, fn) {
119
- const now = Date.now();
120
- const hit = __totalsInflight.get(key);
121
- if (hit && now - hit.t0 < CACHE.TOTALS_INFLIGHT_WINDOW_MS) return hit.p;
122
- const p = (async () => {
123
- try { return await fn(); }
124
- finally { __totalsInflight.delete(key); }
125
- })();
126
- __totalsInflight.set(key, { p, t0: now });
127
- return p;
128
- }
129
-
130
- async function bmGet(cache, ns, keyDims, page) {
131
- const key = `${bookmarkKey(ns, keyDims)}:${page}`;
132
- const cursor = cache?.get ? (await cache.get(key)) : null;
133
- // 验证游标是否有效
134
- if (cursor && typeof cursor === 'string' && cursor.length > 0) {
135
- return cursor;
136
- }
137
- return null;
138
- }
139
- async function bmSet(cache, ns, keyDims, page, cursor, ttlMs) {
140
- const key = `${bookmarkKey(ns, keyDims)}:${page}`;
141
- if (cache?.set) await cache.set(key, cursor, ttlMs);
142
- }
143
-
144
- // totals 计算 + 缓存:失败也写入 total:null(语义统一),并附加 error 字段
145
- async function computeAndCacheTotal({ collection, cache, ns, keyDims, query, limit, hint, collation, maxTimeMS, ttlMs, logger, countQueue }) {
146
- const key = totalsKey(ns, keyDims);
147
- try {
148
- let total;
149
-
150
- // 使用队列控制并发(避免高并发下压垮数据库)
151
- if (countQueue) {
152
- total = await countQueue.execute(() =>
153
- collection.countDocuments(query || {}, {
154
- maxTimeMS,
155
- ...(hint ? { hint } : {}),
156
- ...(collation ? { collation } : {})
157
- })
158
- );
159
- } else {
160
- // 无队列,直接执行(不推荐用于生产环境)
161
- total = await collection.countDocuments(query || {}, {
162
- maxTimeMS,
163
- ...(hint ? { hint } : {}),
164
- ...(collation ? { collation } : {})
165
- });
166
- }
167
-
168
- const totalPages = Math.ceil(total / Math.max(1, limit || 1));
169
- const payload = { total, totalPages, ts: Date.now() };
170
- if (cache?.set) await cache.set(key, payload, ttlMs);
171
- return payload;
172
- } catch (e) {
173
- // 失败:保持 async 语义一致,写入 total:null,并记录一次 warn(去敏)
174
- try { logger && logger.warn && logger.warn('totals.count.failed', { ns, reason: String(e && (e.message || e)) }); } catch (_) { }
175
- const payload = { total: null, totalPages: null, ts: Date.now(), error: 'count_failed' };
176
- if (cache?.set) await cache.set(key, payload, ttlMs);
177
- return payload;
178
- }
179
- }
180
-
181
- function makeJumpTooFarError({ remaining, maxHops }) {
182
- return createError(
183
- ErrorCodes.JUMP_TOO_FAR,
184
- '跳页跨度过大,maxHops 限制触发',
185
- [{ path: ['page'], message: `remaining=${remaining}, maxHops=${maxHops}` }]
186
- );
187
- }
188
-
189
- function createFindPage(ctx) {
190
- const { collection, getCache, getNamespace, defaults, logger, run } = ctx;
191
-
192
- async function doFindPageOne({ options, stableSort, direction, parsedCursor }) {
193
- const sortForQuery = direction === 'before' ? reverseSort(stableSort) : stableSort;
194
- // 🆕 v1.2.0: 计算有效投影(自动保护排序字段,确保游标锚点可提取)
195
- const normalizedProjection = normalizeProjection(options.projection);
196
- const effectiveProjection = buildEffectiveProjection(normalizedProjection, stableSort);
197
- const pipeline = buildPagePipelineA({
198
- query: options.query || {},
199
- sort: sortForQuery,
200
- limit: options.limit,
201
- cursor: parsedCursor,
202
- direction,
203
- lookupPipeline: options.pipeline || [],
204
- projection: effectiveProjection
205
- });
206
- const driverOpts = {
207
- maxTimeMS: options.maxTimeMS ?? defaults.maxTimeMS,
208
- allowDiskUse: options.allowDiskUse,
209
- ...(options.hint ? { hint: options.hint } : {}),
210
- ...(options.collation ? { collation: options.collation } : {}),
211
- };
212
-
213
- // 如果启用 explain,直接返回执行计划(不缓存,不进行分页处理)
214
- if (options.explain) {
215
- const verbosity = typeof options.explain === 'string' ? options.explain : 'queryPlanner';
216
- const aggCursor = collection.aggregate(pipeline, driverOpts);
217
- return await aggCursor.explain(verbosity);
218
- }
219
-
220
- // 如果启用流式返回
221
- if (options.stream) {
222
- if (options.batchSize !== undefined) driverOpts.batchSize = options.batchSize;
223
- // 流式查询:不应该用 limit+1,直接使用 limit
224
- const streamPipeline = buildPagePipelineA({
225
- query: options.query || {},
226
- sort: sortForQuery,
227
- limit: options.limit, // 注意:这里不加1
228
- cursor: parsedCursor,
229
- direction,
230
- lookupPipeline: options.pipeline || [],
231
- projection: normalizedProjection // 🆕 v1.2.0: 流式无需游标保护,使用原始投影
232
- });
233
- // 手动修改最后的 $limit 阶段,不使用 limit+1
234
- const limitStageIndex = streamPipeline.findIndex(stage => stage.$limit !== undefined);
235
- if (limitStageIndex >= 0) {
236
- streamPipeline[limitStageIndex] = { $limit: options.limit };
237
- }
238
- return collection.aggregate(streamPipeline, driverOpts).stream();
239
- }
240
-
241
- const rows = await (run ? run('findPage', options, () => collection.aggregate(pipeline, driverOpts).toArray()) : collection.aggregate(pipeline, driverOpts).toArray());
242
- return makePageResult(rows, {
243
- limit: options.limit,
244
- stableSort,
245
- direction,
246
- hasCursor: !!parsedCursor,
247
- pickAnchor,
248
- });
249
- }
250
-
251
- return async function findPage(options = {}) {
252
- const startTime = Date.now(); // 记录开始时间
253
-
254
- // ✅ v1.3.0: 自动转换 ObjectId 字符串
255
- if (options.query) {
256
- options.query = convertObjectIdStrings(options.query, 'filter', 0, new WeakSet(), {
257
- logger: ctx.logger,
258
- excludeFields: ctx.autoConvertConfig?.excludeFields,
259
- customFieldPatterns: ctx.autoConvertConfig?.customFieldPatterns,
260
- maxDepth: ctx.autoConvertConfig?.maxDepth
261
- });
262
- }
263
-
264
- const MAX_LIMIT = defaults?.findPageMaxLimit ?? 500;
265
- // 基础校验:limit + after/before 互斥
266
- validateLimitAfterBefore(options, { maxLimit: MAX_LIMIT });
267
-
268
- // 如果启用流式模式,只支持简单的游标分页(after/before)或首页查询
269
- // 不支持跳页和 totals 等复杂功能
270
- if (options.stream) {
271
- const { after, before, page } = options;
272
-
273
- // 流式模式不支持跳页
274
- if (Number.isInteger(page) && page > 1) {
275
- throw createError(
276
- ErrorCodes.STREAM_NO_JUMP,
277
- '流式模式不支持跳页功能,请使用 after/before 游标分页'
278
- );
279
- }
280
-
281
- // 流式模式不支持 totals
282
- if (options.totals?.mode && options.totals.mode !== 'none') {
283
- throw createError(
284
- ErrorCodes.STREAM_NO_TOTALS,
285
- '流式模式不支持 totals 统计'
286
- );
287
- }
288
-
289
- // 流式模式不支持 explain
290
- if (options.explain) {
291
- throw createError(
292
- ErrorCodes.STREAM_NO_EXPLAIN,
293
- '流式模式不支持 explain 分析,请使用普通分页模式'
294
- );
295
- }
296
-
297
- const { sort } = options;
298
- const stableSort = ensureStableSort(normalizeSort(sort) || { _id: 1 });
299
-
300
- if (after || before) {
301
- const parsedCursor = decodeCursor(after || before);
302
- assertCursorSortCompatible(stableSort, parsedCursor && parsedCursor.s);
303
- return doFindPageOne({
304
- options,
305
- stableSort,
306
- direction: after ? 'after' : 'before',
307
- parsedCursor
308
- });
309
- }
310
-
311
- // 首页流式查询
312
- return doFindPageOne({
313
- options,
314
- stableSort,
315
- direction: null,
316
- parsedCursor: null
317
- });
318
- }
319
-
320
- // 如果启用 explain,直接返回执行计划(简化流程)
321
- if (options.explain) {
322
- const { sort } = options;
323
- const stableSort = ensureStableSort(normalizeSort(sort) || { _id: 1 });
324
- const parsedCursor = options.after ? decodeCursor(options.after) : options.before ? decodeCursor(options.before) : null;
325
- const direction = options.after ? 'after' : options.before ? 'before' : null;
326
- return await doFindPageOne({ options, stableSort, direction, parsedCursor });
327
- }
328
-
329
- // 解析上下文
330
- const cache = getCache && getCache();
331
- const nsObj = getNamespace && getNamespace();
332
- const ns = nsObj?.ns || `${defaults?.iid || ''}:${defaults?.type || ''}:${ctx.databaseName || ''}:${ctx.collectionName || ''}`;
333
-
334
- const {
335
- query = {},
336
- pipeline: lookupPipeline = [],
337
- sort,
338
- limit,
339
- after,
340
- before,
341
- page,
342
- } = options;
343
-
344
- // 互斥校验:page 与 after/before 不能同时出现
345
- if ((after || before) && Number.isInteger(page)) {
346
- const err = new Error('参数校验失败');
347
- err.code = 'VALIDATION_ERROR';
348
- err.details = [{ path: ['page'], type: 'any.conflict', message: 'page 与 after/before 互斥' }];
349
- throw err;
350
- }
351
-
352
- const stableSort = ensureStableSort(normalizeSort(sort) || { _id: 1 });
353
-
354
- // 辅助函数:添加 meta 信息
355
- const attachMeta = (result) => {
356
- if (options.meta) {
357
- result.meta = {
358
- op: 'findPage',
359
- durationMs: Date.now() - startTime,
360
- cacheHit: false
361
- };
362
- }
363
- return result;
364
- };
365
-
366
- // 1) after/before:现有路径
367
- if (after || before) {
368
- const parsedCursor = decodeCursor(after || before);
369
- assertCursorSortCompatible(stableSort, parsedCursor && parsedCursor.s);
370
- const result = await doFindPageOne({ options, stableSort, direction: after ? 'after' : 'before', parsedCursor });
371
-
372
- // 为游标分页添加 totals 支持
373
- if (options.totals && options.totals.mode && options.totals.mode !== 'none') {
374
- const keyDims = buildKeyDimsAuto({
375
- db: ctx.databaseName,
376
- coll: ctx.collectionName,
377
- sort: stableSort,
378
- limit,
379
- query,
380
- pipeline: lookupPipeline,
381
- });
382
- const out = await attachTotals({ collection, cache, ns, keyDims, limit, query, totals: options.totals, ctx });
383
- result.totals = out;
384
- }
385
-
386
- return attachMeta(result);
387
- }
388
-
389
- // 2) 无 page:首页
390
- if (!Number.isInteger(page) || page < 1) {
391
- const result = await doFindPageOne({ options, stableSort, direction: null, parsedCursor: null });
392
-
393
- // 为首页添加 totals 支持
394
- if (options.totals && options.totals.mode && options.totals.mode !== 'none') {
395
- const keyDims = buildKeyDimsAuto({
396
- db: ctx.databaseName,
397
- coll: ctx.collectionName,
398
- sort: stableSort,
399
- limit,
400
- query,
401
- pipeline: lookupPipeline,
402
- });
403
- const out = await attachTotals({ collection, cache, ns, keyDims, limit, query, totals: options.totals, ctx });
404
- result.totals = out;
405
- }
406
-
407
- return attachMeta(result);
408
- }
409
-
410
- // —— 跳页模式 ——
411
- const step = Number.isInteger(options.jump?.step) ? options.jump.step : (defaults?.bookmarks?.step ?? 10);
412
- const maxHops = Number.isInteger(options.jump?.maxHops) ? options.jump.maxHops : (defaults?.bookmarks?.maxHops ?? 20);
413
- const ttlMs = defaults?.bookmarks?.ttlMs ?? 6 * 3600_000;
414
- const maxBookmarkPages = defaults?.bookmarks?.maxPages ?? 10000;
415
-
416
- // 自动 keyDims
417
- const keyDims = options.jump?.keyDims || buildKeyDimsAuto({
418
- db: ctx.databaseName,
419
- coll: ctx.collectionName,
420
- sort: stableSort,
421
- limit,
422
- query,
423
- pipeline: lookupPipeline,
424
- });
425
-
426
- // 2.1 可选:offset 兜底
427
- if (options.offsetJump?.enable) {
428
- const skip = (page - 1) * limit;
429
- const maxSkip = options.offsetJump.maxSkip ?? 50_000;
430
- if (skip <= maxSkip) {
431
- const p = [];
432
- if (query && Object.keys(query).length) p.push({ $match: query });
433
- p.push({ $sort: stableSort }, { $skip: skip }, { $limit: limit + 1 });
434
- if (lookupPipeline?.length) p.push(...lookupPipeline);
435
- // 🆕 v1.2.0: 支持 projection(同样保护排序字段)
436
- const offsetNormalizedProj = normalizeProjection(options.projection);
437
- const offsetEffectiveProj = buildEffectiveProjection(offsetNormalizedProj, stableSort);
438
- if (offsetEffectiveProj) p.push({ $project: offsetEffectiveProj });
439
- const driverOpts = {
440
- maxTimeMS: options.maxTimeMS ?? defaults.maxTimeMS,
441
- allowDiskUse: options.allowDiskUse,
442
- ...(options.hint ? { hint: options.hint } : {}),
443
- ...(options.collation ? { collation: options.collation } : {}),
444
- };
445
- const rows = await (run ? run('findPage', options, () => collection.aggregate(p, driverOpts).toArray()) : collection.aggregate(p, driverOpts).toArray());
446
- const res = makePageResult(rows, { limit, stableSort, direction: null, hasCursor: false, pickAnchor });
447
- if (res.pageInfo?.endCursor && page <= maxBookmarkPages) await bmSet(cache, ns, keyDims, page, res.pageInfo.endCursor, ttlMs);
448
- if (res.pageInfo) res.pageInfo.currentPage = page;
449
- // totals(可选)
450
- if (options.totals && options.totals.mode && options.totals.mode !== 'none') {
451
- const out = await attachTotals({ collection, cache, ns, keyDims, limit, query, totals: options.totals, ctx });
452
- res.totals = out;
453
- }
454
- return attachMeta(res);
455
- }
456
- }
457
-
458
- // 2.2 书签跳转
459
- const pageIndex = page - 1;
460
- const anchorPage = Math.floor(pageIndex / step) * step; // 0, step, 2*step, ...
461
- const remaining = pageIndex - anchorPage; // 0..(step-1)
462
-
463
- let current = null;
464
- if (anchorPage > 0) {
465
- const anchorCursor = await bmGet(cache, ns, keyDims, anchorPage);
466
- if (anchorCursor) {
467
- // 修复:从缓存获取书签后,需要构造完整的结果对象
468
- // 直接使用游标进行查询以获取该页的数据
469
- try {
470
- const parsedAnchor = decodeCursor(anchorCursor);
471
- current = await doFindPageOne({
472
- options: { ...options, after: anchorCursor },
473
- stableSort,
474
- direction: 'after',
475
- parsedCursor: parsedAnchor
476
- });
477
- // 保存当前页的游标,用于后续跳转
478
- if (current.pageInfo?.endCursor && (anchorPage + 1) <= maxBookmarkPages) {
479
- await bmSet(cache, ns, keyDims, anchorPage + 1, current.pageInfo.endCursor, ttlMs);
480
- }
481
- } catch (e) {
482
- // 如果书签游标失效,重置 current,从头开始
483
- current = null;
484
- }
485
- }
486
- }
487
-
488
- // 累计 hops 计数:限制"整次跳页"的连续 after 次数总和不超过 maxHops
489
- let hops = 0;
490
-
491
- if (!current) {
492
- current = await doFindPageOne({ options, stableSort, direction: null, parsedCursor: null });
493
- if (current.pageInfo?.endCursor && 1 <= maxBookmarkPages) await bmSet(cache, ns, keyDims, 1, current.pageInfo.endCursor, ttlMs);
494
- for (let i = 1; i < anchorPage; i++) {
495
- hops += 1; if (hops > maxHops) throw makeJumpTooFarError({ remaining: anchorPage - i, maxHops });
496
- if (!current.pageInfo?.endCursor) break; // 没有更多数据
497
- current = await doFindPageOne({
498
- options: { ...options, after: current.pageInfo.endCursor },
499
- stableSort,
500
- direction: 'after',
501
- parsedCursor: decodeCursor(current.pageInfo.endCursor)
502
- });
503
- const pNo = i + 1;
504
- if (pNo % step === 0 && pNo <= maxBookmarkPages && current.pageInfo?.endCursor) await bmSet(cache, ns, keyDims, pNo, current.pageInfo.endCursor, ttlMs);
505
- }
506
- }
507
-
508
- for (let i = 0; i < remaining; i++) {
509
- hops += 1; if (hops > maxHops) throw makeJumpTooFarError({ remaining: remaining - i, maxHops });
510
- if (!current?.pageInfo?.endCursor) break; // 没有更多数据
511
- current = await doFindPageOne({
512
- options: { ...options, after: current.pageInfo.endCursor },
513
- stableSort,
514
- direction: 'after',
515
- parsedCursor: decodeCursor(current.pageInfo.endCursor)
516
- });
517
- const pNo = anchorPage + 1 + i + 1;
518
- if (pNo % step === 0 && pNo <= maxBookmarkPages && current.pageInfo?.endCursor) await bmSet(cache, ns, keyDims, pNo, current.pageInfo.endCursor, ttlMs);
519
- }
520
-
521
- if (current?.pageInfo) current.pageInfo.currentPage = page;
522
-
523
- // totals(可选)
524
- if (options.totals && options.totals.mode && options.totals.mode !== 'none') {
525
- const out = await attachTotals({ collection, cache, ns, keyDims, limit, query, totals: options.totals, ctx });
526
- current.totals = out;
527
- }
528
-
529
- return attachMeta(current);
530
- };
531
-
532
- // totals 附加逻辑:支持 sync/async/approx;async 返回短 token(keyHash),避免暴露命名空间
533
- async function attachTotals({ collection, cache, ns, keyDims, limit, query, totals, ctx }) {
534
- const mode = totals.mode || 'none';
535
- const ttlMs = totals.ttlMs ?? 10 * 60_000;
536
- const key = totalsKey(ns, keyDims);
537
- const keyHash = key.split(':').pop(); // 仅最后一段哈希作为对外 token
538
-
539
- // 获取 count 队列
540
- const queue = getCountQueue(ctx);
541
-
542
- if (mode === 'sync') {
543
- const result = await computeAndCacheTotal({
544
- collection, cache, ns, keyDims, query, limit,
545
- hint: totals.hint,
546
- collation: totals.collation,
547
- maxTimeMS: totals.maxTimeMS ?? 2000,
548
- ttlMs,
549
- logger,
550
- countQueue: queue // ← 传递队列
551
- });
552
- return { mode: 'sync', ...result };
553
- } else if (mode === 'async') {
554
- const cached = cache?.get ? await cache.get(key) : null;
555
- if (cached) return { mode: 'async', ...cached, token: keyHash };
556
- // 触发异步计算(去重 + 微任务队列)
557
- const doAsync = () => withTotalsInflight(key, () => computeAndCacheTotal({
558
- collection, cache, ns, keyDims, query, limit,
559
- hint: totals.hint,
560
- collation: totals.collation,
561
- maxTimeMS: totals.maxTimeMS ?? 2000,
562
- ttlMs,
563
- logger,
564
- countQueue: queue // ← 传递队列
565
- })).catch(() => { });
566
- if (typeof queueMicrotask === 'function') queueMicrotask(doAsync); else setTimeout(doAsync, 0);
567
- return { mode: 'async', total: null, totalPages: null, token: keyHash };
568
- } else if (mode === 'approx') {
569
- // 近似:使用 estimatedDocumentCount(基于元数据,速度快但不考虑查询条件)
570
- const cached = cache?.get ? await cache.get(key) : null;
571
- if (cached) return { mode: 'approx', ...cached, approx: true };
572
-
573
- try {
574
- // 如果有查询条件,使用 countDocuments(精确但可能慢)
575
- // 如果是空查询,使用 estimatedDocumentCount(快速但近似)
576
- let total;
577
- if (query && Object.keys(query).length > 0) {
578
- // 有查询条件,使用精确统计(需要队列控制)
579
- if (queue) {
580
- total = await queue.execute(() =>
581
- collection.countDocuments(query, {
582
- maxTimeMS: totals.maxTimeMS ?? 2000,
583
- ...(totals.hint ? { hint: totals.hint } : {}),
584
- ...(totals.collation ? { collation: totals.collation } : {})
585
- })
586
- );
587
- } else {
588
- total = await collection.countDocuments(query, {
589
- maxTimeMS: totals.maxTimeMS ?? 2000,
590
- ...(totals.hint ? { hint: totals.hint } : {}),
591
- ...(totals.collation ? { collation } : {})
592
- });
593
- }
594
- } else {
595
- // 空查询,使用快速近似统计(不需要队列,速度很快)
596
- total = await collection.estimatedDocumentCount({
597
- maxTimeMS: totals.maxTimeMS ?? 1000
598
- });
599
- }
600
-
601
- const totalPages = Math.ceil(total / Math.max(1, limit || 1));
602
- const payload = { total, totalPages, ts: Date.now(), approx: true };
603
- if (cache?.set) await cache.set(key, payload, ttlMs);
604
- return { mode: 'approx', ...payload };
605
- } catch (e) {
606
- // 失败降级
607
- try { logger && logger.warn && logger.warn('totals.approx.failed', { ns, reason: String(e && (e.message || e)) }); } catch (_) { }
608
- const payload = { total: null, totalPages: null, ts: Date.now(), error: 'approx_failed', approx: true };
609
- if (cache?.set) await cache.set(key, payload, ttlMs);
610
- return { mode: 'approx', ...payload };
611
- }
612
- }
613
- return undefined;
614
- }
615
- }
616
-
617
- module.exports = { createFindPage, bookmarkKey, buildKeyDimsAuto, shapeOfQuery, shapeOfPipeline };
618
-