@nest-omni/core 4.1.3-4 → 4.1.3-6

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-omni/core",
3
- "version": "4.1.3-4",
3
+ "version": "4.1.3-6",
4
4
  "description": "A comprehensive NestJS framework for building enterprise-grade applications with best practices",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -21,9 +21,21 @@ export declare class IsUniqueValidator implements ValidatorConstraintInterface {
21
21
  validate<E>(value: string, args: IUniqueValidationArguments<E>): Promise<boolean>;
22
22
  defaultMessage(args: ValidationArguments): string;
23
23
  }
24
+ /**
25
+ * IsUnique 配置选项
26
+ */
27
+ export interface IsUniqueOptions {
28
+ /**
29
+ * 手动指定用于判断是否为新建的字段名
30
+ * 适用于视图(View)等没有主键的场景
31
+ * 例如:uniqueKeys: ['id'] 或 uniqueKeys: ['id', 'version']
32
+ */
33
+ uniqueKeys?: string[];
34
+ }
24
35
  type UniqueValidationConstraints<E> = [
25
36
  ObjectType<E> | EntitySchema<E> | string | (() => ObjectType<E> | EntitySchema<E> | string),
26
- (validationArguments: ValidationArguments) => FindOptionsWhere<E>
37
+ (validationArguments: ValidationArguments) => FindOptionsWhere<E>,
38
+ IsUniqueOptions?
27
39
  ];
28
40
  interface IUniqueValidationArguments<E> extends ValidationArguments {
29
41
  constraints: UniqueValidationConstraints<E>;
@@ -84,17 +84,36 @@ let IsUniqueValidator = class IsUniqueValidator {
84
84
  }
85
85
  validate(value, args) {
86
86
  return __awaiter(this, void 0, void 0, function* () {
87
- const [entityRef, findCondition] = args.constraints;
87
+ const [entityRef, findCondition, options] = args.constraints;
88
88
  // 解析 Entity 引用
89
89
  const entityClass = this.resolveEntity(entityRef, args.property, args.object);
90
90
  const repository = this.transactionHost.tx.getRepository(entityClass);
91
91
  args.value = value;
92
92
  let exists;
93
93
  const defCon = findCondition(args);
94
- const pkCols = repository.metadata
95
- .primaryColumns.map((column) => column.propertyName);
94
+ // 获取用于判断是否为新建的字段
95
+ // 优先使用手动配置的 uniqueKeys,否则使用主键
96
+ let pkCols;
97
+ const hasUniqueKeys = (options === null || options === void 0 ? void 0 : options.uniqueKeys) && options.uniqueKeys.length > 0;
98
+ if (hasUniqueKeys) {
99
+ // 使用手动指定的字段
100
+ pkCols = options.uniqueKeys;
101
+ }
102
+ else {
103
+ // 使用实体的主键
104
+ pkCols = repository.metadata
105
+ .primaryColumns.map((column) => column.propertyName);
106
+ }
107
+ // 如果没有主键也没有 uniqueKeys,使用简化逻辑
108
+ // 查询条件本身就能表示唯一性:0-1条允许,2条及以上不允许
109
+ if (pkCols.length === 0) {
110
+ const count = yield repository.count({ where: defCon });
111
+ // 0条(不存在)或1条(认为是自己)都允许,2条及以上不允许
112
+ exists = count <= 1;
113
+ return exists;
114
+ }
96
115
  // 修复:检查是否为新建记录
97
- // 如果所有主键都没有值(null/undefined),则为新建
116
+ // 如果所有 uniqueKeys/主键都没有值(null/undefined),则为新建
98
117
  const isNew = !pkCols.some((pk) => {
99
118
  const pkValue = args.object[pk];
100
119
  return pkValue !== null && pkValue !== undefined && pkValue !== '';
@@ -110,9 +129,8 @@ let IsUniqueValidator = class IsUniqueValidator {
110
129
  const entities = yield repository
111
130
  .createQueryBuilder()
112
131
  .where(defCon)
113
- .select(pkCols)
114
132
  .limit(2)
115
- .execute();
133
+ .getMany();
116
134
  const entityCount = entities.length;
117
135
  if (entityCount === 1) {
118
136
  // 只有一条记录,检查是否是当前记录本身