Skip to content

Commit

Permalink
fix(repository): make sure foreign key property in keyTo exists in ta…
Browse files Browse the repository at this point in the history
…rget model
  • Loading branch information
nabdelgadir committed Jul 15, 2019
1 parent f0811aa commit ce75385
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function resolveBelongsToMetadata(relationMeta: BelongsToDefinition) {
const targetProperties = targetModel.definition.properties;
debug('relation metadata from %o: %o', targetName, targetProperties);

if (relationMeta.keyTo) {
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
// The explict cast is needed because of a limitation of type inference
return relationMeta as BelongsToResolvedDefinition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ function resolveHasManyMetadata(
throw new InvalidRelationError(reason, relationMeta);
}

if (relationMeta.keyTo) {
const targetModel = relationMeta.target();
const targetModelProperties =
targetModel.definition && targetModel.definition.properties;

// Make sure that if it already keys to the foreign key property,
// the key exists in the target model
if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
// The explict cast is needed because of a limitation of type inference
return relationMeta as HasManyResolvedDefinition;
}
Expand All @@ -83,17 +89,13 @@ function resolveHasManyMetadata(
throw new InvalidRelationError(reason, relationMeta);
}

const targetModel = relationMeta.target();
debug(
'Resolved model %s from given metadata: %o',
targetModel.modelName,
targetModel,
);
const defaultFkName = camelCase(sourceModel.modelName + '_id');
const hasDefaultFkProperty =
targetModel.definition &&
targetModel.definition.properties &&
targetModel.definition.properties[defaultFkName];
const hasDefaultFkProperty = targetModelProperties[defaultFkName];

if (!hasDefaultFkProperty) {
const reason = `target model ${targetModel.name} is missing definition of foreign key ${defaultFkName}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ function resolveHasOneMetadata(
throw new InvalidRelationError(reason, relationMeta);
}

if (relationMeta.keyTo) {
const targetModel = relationMeta.target();
const targetModelProperties =
targetModel.definition && targetModel.definition.properties;

// Make sure that if it already keys to the foreign key property,
// the key exists in the target model
if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
// The explict cast is needed because of a limitation of type inference
return relationMeta as HasOneResolvedDefinition;
}
Expand All @@ -80,17 +86,13 @@ function resolveHasOneMetadata(
throw new InvalidRelationError(reason, relationMeta);
}

const targetModel = relationMeta.target();
debug(
'Resolved model %s from given metadata: %o',
targetModel.modelName,
targetModel,
);
const defaultFkName = camelCase(sourceModel.modelName + '_id');
const hasDefaultFkProperty =
targetModel.definition &&
targetModel.definition.properties &&
targetModel.definition.properties[defaultFkName];
const hasDefaultFkProperty = targetModelProperties[defaultFkName];

if (!hasDefaultFkProperty) {
const reason = `target model ${targetModel.name} is missing definition of foreign key ${defaultFkName}`;
Expand Down

0 comments on commit ce75385

Please sign in to comment.