monsqlize 1.2.0 → 1.2.1
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 +1 -0
- package/lib/index.js +29 -5
- package/lib/model/index.js +13 -32
- package/package.json +1 -1
- package/types/monsqlize.ts +25 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
| 版本 | 日期 | 变更摘要 | 详细 |
|
|
11
11
|
|------|------|---------|------|
|
|
12
|
+
| [v1.2.1](./changelogs/v1.2.1.md) | 2026-04-13 | 🐛 **Bug 修复**:`msq.model()` 实例缓存 + 索引去重 + 死代码清理 + `types/monsqlize.ts` 补全 `model()` / `collection()` 类型 | [查看](./changelogs/v1.2.1.md) |
|
|
12
13
|
| [v1.2.0](./changelogs/v1.2.0.md) | 2026-04-13 | 🐛 **Bug 修复 + 新功能**:`findPage` 正式支持 `projection` 投影参数(修复静默忽略问题)+ 有效投影策略自动保护游标排序字段 + 8 个测试用例 | [查看](./changelogs/v1.2.0.md) |
|
|
13
14
|
| [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) |
|
|
14
15
|
| [v1.1.8](./changelogs/v1.1.8.md) | 2026-03-16 | 🆕 **新功能**:Model 热重载支持(`undefine()` + `redefine()` + `_loadModels` reload 模式)+ 22个测试 (100%通过) | [查看](./changelogs/v1.1.8.md) |
|
package/lib/index.js
CHANGED
|
@@ -678,7 +678,10 @@ module.exports = class {
|
|
|
678
678
|
}
|
|
679
679
|
|
|
680
680
|
/**
|
|
681
|
-
* 获取 Model
|
|
681
|
+
* 获取 Model 实例(缓存复用)
|
|
682
|
+
*
|
|
683
|
+
* 同一 collectionName 多次调用返回同一实例。
|
|
684
|
+
* Model.redefine() / Model.undefine() 后自动失效,close() 后全部清空。
|
|
682
685
|
*
|
|
683
686
|
* @param {string} collectionName - 集合名称
|
|
684
687
|
* @returns {ModelInstance} Model 实例
|
|
@@ -710,8 +713,19 @@ module.exports = class {
|
|
|
710
713
|
throw err;
|
|
711
714
|
}
|
|
712
715
|
|
|
713
|
-
// 检查 Model 是否已定义
|
|
714
716
|
const Model = require("./model");
|
|
717
|
+
|
|
718
|
+
// 缓存命中 + redefine 失效检查
|
|
719
|
+
if (this._modelInstances && this._modelInstances.has(collectionName)) {
|
|
720
|
+
if (!Model._redefinedNames.has(collectionName)) {
|
|
721
|
+
return this._modelInstances.get(collectionName);
|
|
722
|
+
}
|
|
723
|
+
// redefine 后需要重建实例
|
|
724
|
+
this._modelInstances.delete(collectionName);
|
|
725
|
+
Model._redefinedNames.delete(collectionName);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// 检查 Model 是否已定义
|
|
715
729
|
if (!Model.has(collectionName)) {
|
|
716
730
|
const err = new Error(
|
|
717
731
|
`Model '${collectionName}' is not defined. Call Model.define() first.`,
|
|
@@ -726,9 +740,13 @@ module.exports = class {
|
|
|
726
740
|
// 获取 collection 实例
|
|
727
741
|
const collection = this.dbInstance.collection(collectionName);
|
|
728
742
|
|
|
729
|
-
// 创建 ModelInstance
|
|
730
|
-
const
|
|
731
|
-
|
|
743
|
+
// 创建 ModelInstance 并缓存
|
|
744
|
+
const instance = new Model.ModelInstance(collection, modelDef.definition, this);
|
|
745
|
+
|
|
746
|
+
if (!this._modelInstances) this._modelInstances = new Map();
|
|
747
|
+
this._modelInstances.set(collectionName, instance);
|
|
748
|
+
|
|
749
|
+
return instance;
|
|
732
750
|
}
|
|
733
751
|
|
|
734
752
|
/**
|
|
@@ -1007,6 +1025,12 @@ module.exports = class {
|
|
|
1007
1025
|
this.dbInstance = null;
|
|
1008
1026
|
this._connecting = null;
|
|
1009
1027
|
|
|
1028
|
+
// 清理 ModelInstance 缓存
|
|
1029
|
+
if (this._modelInstances) {
|
|
1030
|
+
this._modelInstances.clear();
|
|
1031
|
+
this._modelInstances = null;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1010
1034
|
return null;
|
|
1011
1035
|
}
|
|
1012
1036
|
};
|
package/lib/model/index.js
CHANGED
|
@@ -58,6 +58,14 @@ class Model {
|
|
|
58
58
|
*/
|
|
59
59
|
static _registry = new Map();
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 已被 redefine 的 collectionName 集合(用于缓存失效通知)
|
|
63
|
+
* @private
|
|
64
|
+
* @type {Set<string>}
|
|
65
|
+
* @since 1.2.1
|
|
66
|
+
*/
|
|
67
|
+
static _redefinedNames = new Set();
|
|
68
|
+
|
|
61
69
|
/**
|
|
62
70
|
* 定义并注册 Model
|
|
63
71
|
*
|
|
@@ -396,6 +404,8 @@ class Model {
|
|
|
396
404
|
* @since 1.1.7
|
|
397
405
|
*/
|
|
398
406
|
static undefine(collectionName) {
|
|
407
|
+
// 标记需要缓存失效(MonSQLize.model() 检查此标记)
|
|
408
|
+
this._redefinedNames.add(collectionName);
|
|
399
409
|
return this._registry.delete(collectionName);
|
|
400
410
|
}
|
|
401
411
|
|
|
@@ -434,6 +444,8 @@ class Model {
|
|
|
434
444
|
static redefine(collectionName, definition) {
|
|
435
445
|
this.undefine(collectionName);
|
|
436
446
|
this.define(collectionName, definition);
|
|
447
|
+
// 标记需要缓存失效(MonSQLize.model() 检查此标记)
|
|
448
|
+
this._redefinedNames.add(collectionName);
|
|
437
449
|
}
|
|
438
450
|
|
|
439
451
|
/**
|
|
@@ -443,6 +455,7 @@ class Model {
|
|
|
443
455
|
*/
|
|
444
456
|
static _clear() {
|
|
445
457
|
this._registry.clear();
|
|
458
|
+
this._redefinedNames.clear();
|
|
446
459
|
}
|
|
447
460
|
}
|
|
448
461
|
|
|
@@ -662,38 +675,6 @@ class ModelInstance {
|
|
|
662
675
|
}
|
|
663
676
|
}
|
|
664
677
|
|
|
665
|
-
/**
|
|
666
|
-
* 创建索引
|
|
667
|
-
*
|
|
668
|
-
* @private
|
|
669
|
-
* @returns {Promise<void>}
|
|
670
|
-
*/
|
|
671
|
-
async _createIndexes() {
|
|
672
|
-
if (!Array.isArray(this.indexes) || this.indexes.length === 0) {
|
|
673
|
-
return;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
try {
|
|
677
|
-
// 使用 createIndexes 批量创建索引
|
|
678
|
-
await this.collection.createIndexes(this.indexes);
|
|
679
|
-
|
|
680
|
-
if (this.msq && this.msq.logger) {
|
|
681
|
-
this.msq.logger.info(
|
|
682
|
-
`[Model] Created ${this.indexes.length} index(es) for ${this.collection.collectionName}`,
|
|
683
|
-
);
|
|
684
|
-
}
|
|
685
|
-
} catch (err) {
|
|
686
|
-
// 索引创建失败仅记录警告
|
|
687
|
-
if (this.msq && this.msq.logger) {
|
|
688
|
-
this.msq.logger.warn(
|
|
689
|
-
`[Model] Failed to create indexes for ${this.collection.collectionName}:`,
|
|
690
|
-
err.message,
|
|
691
|
-
);
|
|
692
|
-
}
|
|
693
|
-
throw err;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
|
|
697
678
|
/**
|
|
698
679
|
* 将实例方法注入到文档对象
|
|
699
680
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monsqlize",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
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",
|
package/types/monsqlize.ts
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { TransactionOptions } from './options';
|
|
7
|
-
import type { DbAccessor, HealthView } from './collection';
|
|
7
|
+
import type { DbAccessor, HealthView, Collection } from './collection';
|
|
8
8
|
import type { CacheLike } from './cache';
|
|
9
9
|
import type { Transaction } from './transaction';
|
|
10
10
|
import type { Lock, LockOptions } from './lock';
|
|
11
11
|
import type { ExpressionFunction } from './base';
|
|
12
12
|
import type { MetaInfo } from './pagination';
|
|
13
|
+
import type { ModelInstance } from './model';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* MonSQLize 主类
|
|
@@ -46,6 +47,29 @@ export interface MonSQLize {
|
|
|
46
47
|
*/
|
|
47
48
|
health(): Promise<HealthView>;
|
|
48
49
|
|
|
50
|
+
/**
|
|
51
|
+
* 获取 Model 实例(缓存复用)
|
|
52
|
+
*
|
|
53
|
+
* 同一 collectionName 多次调用返回同一实例。
|
|
54
|
+
* Model.redefine() / Model.undefine() 后自动失效,close() 后全部清空。
|
|
55
|
+
*
|
|
56
|
+
* @param collectionName - 已注册的集合名称
|
|
57
|
+
* @returns ModelInstance 实例
|
|
58
|
+
* @throws 数据库未连接(NOT_CONNECTED)
|
|
59
|
+
* @throws Model 未定义(MODEL_NOT_DEFINED)
|
|
60
|
+
* @since 1.0.3
|
|
61
|
+
*/
|
|
62
|
+
model(collectionName: string): ModelInstance;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 获取原始集合实例
|
|
66
|
+
*
|
|
67
|
+
* @param collectionName - 集合名称
|
|
68
|
+
* @returns Collection 实例
|
|
69
|
+
* @throws 数据库未连接(NOT_CONNECTED)
|
|
70
|
+
*/
|
|
71
|
+
collection(collectionName: string): Collection;
|
|
72
|
+
|
|
49
73
|
|
|
50
74
|
// ============================================================================
|
|
51
75
|
// 事件系统
|