Skip to content

Commit 56e4142

Browse files
committed
release: v1.2.3 — fix model() using registry key as MongoDB collection name
1 parent 4964672 commit 56e4142

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
| 版本 | 日期 | 变更摘要 | 详细 |
1111
|------|------|---------|------|
12+
| [v1.2.3](./changelogs/v1.2.3.md) | 2026-04-26 | 🐛 **Bug Fix**:`model()` 方法修复注册 key 直接作为 MongoDB 集合名的问题,优先读 `definition.collection > definition.name`,注册 key 仅作 fallback,向后兼容 | [查看](./changelogs/v1.2.3.md) |
1213
| [v1.2.2](./changelogs/v1.2.2.md) | 2026-04-24 | 🆕 **新功能**:Model 数据源绑定 — `connection: { pool?, database? }` 支持 Model 绑定指定连接池和/或数据库,四种组合路由,向后兼容;🐛 **Bug Fix**:修复 `Model._clear()` 导致 `msq.model()` 实例缓存失效失效,populate 关系丢失问题 | [查看](./changelogs/v1.2.2.md) |
1314
| [v1.2.1](./changelogs/v1.2.1.md) | 2026-04-13 | 🐛 **Bug 修复**:`msq.model()` 实例缓存 + 索引去重 + 死代码清理 + `types/monsqlize.ts` 补全 `model()` / `collection()` 类型 | [查看](./changelogs/v1.2.1.md) |
1415
| [v1.2.0](./changelogs/v1.2.0.md) | 2026-04-13 | 🐛 **Bug 修复 + 新功能**:`findPage` 正式支持 `projection` 投影参数(修复静默忽略问题)+ 有效投影策略自动保护游标排序字段 + 8 个测试用例 | [查看](./changelogs/v1.2.0.md) |

‎changelogs/v1.2.3.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# v1.2.3 — 2026-04-26
2+
3+
> **类型**: 🐛 Bug Fix
4+
> **影响范围**: `lib/index.js`(`model()` 方法)
5+
> **向后兼容**: ✅ 完全兼容 — 对未使用目录路由注册 key 的场景行为不变
6+
7+
---
8+
9+
## Bug Fix:`model()` 方法使用注册 key 作为 MongoDB 集合名
10+
11+
### 问题
12+
13+
`model(collectionName)` 获取 collection 实例时,直接将传入的注册 key(`collectionName`)作为 MongoDB 集合名传给 `_resolveModelCollection` 和 `dbInstance.collection()`。
14+
15+
当上层框架(如 vext)使用带路径前缀的注册 key(如 `AOrder`,由目录 `a/order.ts` 推断),而 `definition.name` 存储了实际 MongoDB 集合名(如 `order`)时,MongoDB 访问的集合名与预期不符。
16+
17+
### 根因
18+
19+
```js
20+
// v1.2.2 — 直接使用注册 key 作为集合名
21+
const collection = (connection && (connection.pool || connection.database))
22+
? this._resolveModelCollection(collectionName, connection)
23+
: this.dbInstance.collection(collectionName);
24+
```
25+
26+
### 修复
27+
28+
在获取 collection 前,优先从 `definition.collection` 或 `definition.name` 读取实际集合名,注册 key 仅作为 fallback:
29+
30+
```js
31+
// [v1.2.3] 实际 MongoDB 集合名可能不同于注册 key
32+
const actualCollectionName =
33+
modelDef.definition.collection ||
34+
modelDef.definition.name ||
35+
collectionName; // fallback:注册 key(向后兼容)
36+
37+
const collection = (connection && (connection.pool || connection.database))
38+
? this._resolveModelCollection(actualCollectionName, connection)
39+
: this.dbInstance.collection(actualCollectionName);
40+
```
41+
42+
### 兼容性
43+
44+
- 现有 model(注册 key 与 MongoDB 集合名相同,如直接传 `"admin_login_logs"`):
45+
`actualCollectionName = definition.collection || definition.name || "admin_login_logs"` — 行为完全不变 ✅
46+
- 新场景(框架注入 `definition.name` 实际集合名):正确使用 `definition.name` ✅

‎lib/index.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,11 +737,18 @@ module.exports = class {
737737
// 获取 Model 定义
738738
const modelDef = Model.get(collectionName);
739739

740+
// [v1.2.3 patch] 实际 MongoDB 集合名可能不同于注册 key
741+
// 优先读 definition.collection > definition.name > key(fallback,向后兼容)
742+
const actualCollectionName =
743+
modelDef.definition.collection ||
744+
modelDef.definition.name ||
745+
collectionName;
746+
740747
// 获取 collection 实例(v1.2.2+ 支持 connection 路由)
741748
const connection = modelDef.definition.connection;
742749
const collection = (connection && (connection.pool || connection.database))
743-
? this._resolveModelCollection(collectionName, connection)
744-
: this.dbInstance.collection(collectionName);
750+
? this._resolveModelCollection(actualCollectionName, connection)
751+
: this.dbInstance.collection(actualCollectionName);
745752

746753
// 创建 ModelInstance 并缓存
747754
const instance = new Model.ModelInstance(collection, modelDef.definition, this);

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monsqlize",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"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)",
55
"main": "lib/index.js",
66
"module": "index.mjs",

0 commit comments

Comments
 (0)