@nest-omni/core 4.1.3-6 → 4.1.3-7
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/package.json
CHANGED
|
@@ -92,29 +92,25 @@ let IsUniqueValidator = class IsUniqueValidator {
|
|
|
92
92
|
let exists;
|
|
93
93
|
const defCon = findCondition(args);
|
|
94
94
|
// 获取用于判断是否为新建的字段
|
|
95
|
-
//
|
|
95
|
+
// 优先级:手动配置的 uniqueKeys > 实体主键 > 查询条件的 keys
|
|
96
96
|
let pkCols;
|
|
97
97
|
const hasUniqueKeys = (options === null || options === void 0 ? void 0 : options.uniqueKeys) && options.uniqueKeys.length > 0;
|
|
98
98
|
if (hasUniqueKeys) {
|
|
99
|
-
// 使用手动指定的字段
|
|
99
|
+
// 1. 使用手动指定的字段
|
|
100
100
|
pkCols = options.uniqueKeys;
|
|
101
101
|
}
|
|
102
102
|
else {
|
|
103
|
-
// 使用实体的主键
|
|
103
|
+
// 2. 使用实体的主键
|
|
104
104
|
pkCols = repository.metadata
|
|
105
105
|
.primaryColumns.map((column) => column.propertyName);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const count = yield repository.count({ where: defCon });
|
|
111
|
-
// 0条(不存在)或1条(认为是自己)都允许,2条及以上不允许
|
|
112
|
-
exists = count <= 1;
|
|
113
|
-
return exists;
|
|
106
|
+
// 3. 如果没有主键,从查询条件的 keys 中提取
|
|
107
|
+
if (pkCols.length === 0 && defCon && typeof defCon === 'object') {
|
|
108
|
+
pkCols = Object.keys(defCon);
|
|
109
|
+
}
|
|
114
110
|
}
|
|
115
111
|
// 修复:检查是否为新建记录
|
|
116
112
|
// 如果所有 uniqueKeys/主键都没有值(null/undefined),则为新建
|
|
117
|
-
const isNew = !pkCols.some((pk) => {
|
|
113
|
+
const isNew = pkCols.length === 0 || !pkCols.some((pk) => {
|
|
118
114
|
const pkValue = args.object[pk];
|
|
119
115
|
return pkValue !== null && pkValue !== undefined && pkValue !== '';
|
|
120
116
|
});
|