@nest-omni/core 4.1.3-3 → 4.1.3-4
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
|
@@ -93,13 +93,20 @@ let IsUniqueValidator = class IsUniqueValidator {
|
|
|
93
93
|
const defCon = findCondition(args);
|
|
94
94
|
const pkCols = repository.metadata
|
|
95
95
|
.primaryColumns.map((column) => column.propertyName);
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
// 修复:检查是否为新建记录
|
|
97
|
+
// 如果所有主键都没有值(null/undefined),则为新建
|
|
98
|
+
const isNew = !pkCols.some((pk) => {
|
|
99
|
+
const pkValue = args.object[pk];
|
|
100
|
+
return pkValue !== null && pkValue !== undefined && pkValue !== '';
|
|
101
|
+
});
|
|
102
|
+
if (isNew) {
|
|
103
|
+
// 新建场景:简单检查是否存在相同值的记录
|
|
98
104
|
exists =
|
|
99
105
|
(yield repository
|
|
100
106
|
.count({ where: defCon })) < 1;
|
|
101
107
|
}
|
|
102
108
|
else {
|
|
109
|
+
// 更新场景:需要排除当前记录(通过主键对比)
|
|
103
110
|
const entities = yield repository
|
|
104
111
|
.createQueryBuilder()
|
|
105
112
|
.where(defCon)
|
|
@@ -108,16 +115,19 @@ let IsUniqueValidator = class IsUniqueValidator {
|
|
|
108
115
|
.execute();
|
|
109
116
|
const entityCount = entities.length;
|
|
110
117
|
if (entityCount === 1) {
|
|
118
|
+
// 只有一条记录,检查是否是当前记录本身
|
|
111
119
|
const entity = entities[0];
|
|
112
120
|
const oldPk = {};
|
|
113
121
|
const newPk = {};
|
|
114
|
-
pkCols.
|
|
122
|
+
pkCols.forEach((pk) => {
|
|
115
123
|
oldPk[pk] = entity[pk];
|
|
116
124
|
newPk[pk] = args.object[pk];
|
|
117
125
|
});
|
|
126
|
+
// 如果主键相同,说明是自己,允许(唯一)
|
|
118
127
|
exists = JSON.stringify(oldPk) === JSON.stringify(newPk);
|
|
119
128
|
}
|
|
120
129
|
else {
|
|
130
|
+
// 0条或多条记录
|
|
121
131
|
exists = entityCount < 1;
|
|
122
132
|
}
|
|
123
133
|
}
|