Skip to content

Commit

Permalink
fix: enhance skipRelation check in validate entity join columns fn
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 6, 2024
1 parent ab87659 commit 3120433
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/helpers/entity/join-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function validateEntityJoinColumns<T extends ObjectLiteral>(
for (let i = 0; i < entityMetadata.relations.length; i++) {
const relation = entityMetadata.relations[i];

let isOptional : boolean = false;
let skipRelation : boolean = false;

const where : FindOptionsWhere<ObjectLiteral> = {};
const columns : string[] = [];
Expand All @@ -38,16 +38,17 @@ export async function validateEntityJoinColumns<T extends ObjectLiteral>(
continue;
}

if (
joinColumn.isNullable &&
entity[joinColumn.propertyName] === null
) {
skipRelation = true;
break;
}

if (joinColumn.referencedColumn) {
where[joinColumn.referencedColumn.propertyName] = entity[joinColumn.propertyName];

if (
joinColumn.isNullable &&
(entity[joinColumn.propertyName] === null || entity[joinColumn.propertyName] === undefined)
) {
isOptional = true;
}

columns.push(joinColumn.propertyName);
} else {
throw EntityRelationLookupError.notReferenced(
Expand All @@ -57,7 +58,7 @@ export async function validateEntityJoinColumns<T extends ObjectLiteral>(
}
}

if (columns.length === 0) {
if (skipRelation || columns.length === 0) {
continue;
}

Expand All @@ -67,10 +68,6 @@ export async function validateEntityJoinColumns<T extends ObjectLiteral>(
});

if (!item) {
if (isOptional) {
continue;
}

throw EntityRelationLookupError.notFound(relation.propertyName, columns);
}

Expand Down

0 comments on commit 3120433

Please sign in to comment.