monsqlize 1.1.9 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # 变更日志 (CHANGELOG)
2
2
 
3
3
  > **说明**: 版本概览摘要,详细变更见 [changelogs/](./changelogs/) 目录
4
- > **最后更新**: 2026-04-02
4
+ > **最后更新**: 2026-04-13
5
5
 
6
6
  ---
7
7
 
@@ -9,6 +9,7 @@
9
9
 
10
10
  | 版本 | 日期 | 变更摘要 | 详细 |
11
11
  |------|------|---------|------|
12
+ | [v1.2.0](./changelogs/v1.2.0.md) | 2026-04-13 | 🐛 **Bug 修复 + 新功能**:`findPage` 正式支持 `projection` 投影参数(修复静默忽略问题)+ 有效投影策略自动保护游标排序字段 + 8 个测试用例 | [查看](./changelogs/v1.2.0.md) |
12
13
  | [v1.1.9](./changelogs/v1.1.9.md) | 2026-04-02 | 🚨 **P1 Bug 修复**:MultiLevelCache L2→L1 回填 TTL 缺失(null 永久驻留 L1)+ 新增 `backfillLocalTTL` 配置 + Redis `getWithTTL` 方法 + 14 个回归测试 | [查看](./changelogs/v1.1.9.md) |
13
14
  | [v1.1.8](./changelogs/v1.1.8.md) | 2026-03-16 | 🆕 **新功能**:Model 热重载支持(`undefine()` + `redefine()` + `_loadModels` reload 模式)+ 22个测试 (100%通过) | [查看](./changelogs/v1.1.8.md) |
14
15
  | [v1.1.6](./changelogs/v1.1.6.md) | 2026-02-11 | 🎉 **重大功能**:精准缓存失效机制 + 🚨 upsert 缓存失效 Bug 修复 + 36个测试 (100%通过) | [查看](./changelogs/v1.1.6.md) |
@@ -16,15 +16,18 @@ const { reverseSort } = require('./sort');
16
16
  * @param {{a:object,s:object}|null} [params.cursor] - 解析后的游标对象
17
17
  * @param {'after'|'before'|null} [params.direction]
18
18
  * @param {object[]} [params.lookupPipeline] - 页内联表等追加管道
19
+ * @param {Record<string,any>} [params.projection] - 已计算的有效投影(调用方负责保护排序字段)
19
20
  * @returns {object[]} 聚合管道数组
20
21
  */
21
- function buildPagePipelineA({ query = {}, sort, limit, cursor, direction, lookupPipeline = [] }) {
22
+ function buildPagePipelineA({ query = {}, sort, limit, cursor, direction, lookupPipeline = [], projection }) {
22
23
  const pipeline = [];
23
24
  if (query && Object.keys(query).length) pipeline.push({ $match: query });
24
25
  if (cursor) pipeline.push({ $match: { $expr: buildLexiExpr(sort, cursor.a) } });
25
26
  pipeline.push({ $sort: sort });
26
27
  pipeline.push({ $limit: limit + 1 });
27
28
  if (lookupPipeline && lookupPipeline.length) pipeline.push(...lookupPipeline);
29
+ // 🆕 v1.2.0: 在 lookup 之后、方向恢复之前注入 $project(调用方已确保排序字段存在)
30
+ if (projection) pipeline.push({ $project: projection });
28
31
  if (direction === 'before') pipeline.push({ $sort: reverseSort(sort) });
29
32
  return pipeline;
30
33
  }
@@ -10,9 +10,40 @@ const { buildPagePipelineA } = require('../common/agg-pipeline');
10
10
  const { decodeCursor } = require('../../common/cursor');
11
11
  const { validateLimitAfterBefore, assertCursorSortCompatible } = require('../../common/validation');
12
12
  const { makePageResult } = require('../../common/page-result');
13
- const { normalizeSort } = require('../../common/normalize');
13
+ const { normalizeSort, normalizeProjection } = require('../../common/normalize');
14
14
  const { convertObjectIdStrings } = require('../../utils/objectid-converter');
15
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
+
16
47
  // —— Count 队列支持(高并发控制)——
17
48
  let countQueue = null;
18
49
 
@@ -160,13 +191,17 @@ function createFindPage(ctx) {
160
191
 
161
192
  async function doFindPageOne({ options, stableSort, direction, parsedCursor }) {
162
193
  const sortForQuery = direction === 'before' ? reverseSort(stableSort) : stableSort;
194
+ // 🆕 v1.2.0: 计算有效投影(自动保护排序字段,确保游标锚点可提取)
195
+ const normalizedProjection = normalizeProjection(options.projection);
196
+ const effectiveProjection = buildEffectiveProjection(normalizedProjection, stableSort);
163
197
  const pipeline = buildPagePipelineA({
164
198
  query: options.query || {},
165
199
  sort: sortForQuery,
166
200
  limit: options.limit,
167
201
  cursor: parsedCursor,
168
202
  direction,
169
- lookupPipeline: options.pipeline || []
203
+ lookupPipeline: options.pipeline || [],
204
+ projection: effectiveProjection
170
205
  });
171
206
  const driverOpts = {
172
207
  maxTimeMS: options.maxTimeMS ?? defaults.maxTimeMS,
@@ -185,14 +220,15 @@ function createFindPage(ctx) {
185
220
  // 如果启用流式返回
186
221
  if (options.stream) {
187
222
  if (options.batchSize !== undefined) driverOpts.batchSize = options.batchSize;
188
- // 流式查询:��应该用 limit+1,直接使用 limit
223
+ // 流式查询:不应该用 limit+1,直接使用 limit
189
224
  const streamPipeline = buildPagePipelineA({
190
225
  query: options.query || {},
191
226
  sort: sortForQuery,
192
227
  limit: options.limit, // 注意:这里不加1
193
228
  cursor: parsedCursor,
194
229
  direction,
195
- lookupPipeline: options.pipeline || []
230
+ lookupPipeline: options.pipeline || [],
231
+ projection: normalizedProjection // 🆕 v1.2.0: 流式无需游标保护,使用原始投影
196
232
  });
197
233
  // 手动修改最后的 $limit 阶段,不使用 limit+1
198
234
  const limitStageIndex = streamPipeline.findIndex(stage => stage.$limit !== undefined);
@@ -396,6 +432,10 @@ function createFindPage(ctx) {
396
432
  if (query && Object.keys(query).length) p.push({ $match: query });
397
433
  p.push({ $sort: stableSort }, { $skip: skip }, { $limit: limit + 1 });
398
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 });
399
439
  const driverOpts = {
400
440
  maxTimeMS: options.maxTimeMS ?? defaults.maxTimeMS,
401
441
  allowDiskUse: options.allowDiskUse,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monsqlize",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "description": "A lightweight MongoDB ORM with multi-level caching, transaction support, distributed features, Saga distributed transactions, unified expression system with 122 operators, and universal function caching (100% MongoDB support)",
5
5
  "main": "lib/index.js",
6
6
  "module": "index.mjs",
@@ -61,6 +61,9 @@ export interface TotalsOptions {
61
61
  * 深度分页(统一版)选项
62
62
  */
63
63
  export interface FindPageOptions extends FindOptions {
64
+ /**
65
+ * 附加聚合管道(在 projection 之前执行,仅对当页数据生效)
66
+ */
64
67
  pipeline?: object[];
65
68
  after?: string;
66
69
  before?: string;
@@ -77,6 +80,8 @@ export interface FindPageOptions extends FindOptions {
77
80
  offsetJump?: OffsetJumpOptions; // 小范围 offset 兜底
78
81
  totals?: TotalsOptions; // 总数/总页数配置
79
82
  meta?: boolean | MetaOptions; // 返回耗时元信息
83
+ // 注:projection 继承自 FindOptions,findPage 自 v1.2.0 起正式支持。
84
+ // 排序字段会被自动保留以确保游标正确生成。
80
85
  }
81
86
 
82
87
  /**