Skip to content

Commit

Permalink
fixup! add keyFrom to existing if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Sep 17, 2019
1 parent 30be3e5 commit f058920
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ describe('keyTo and keyFrom with resolveHasManyMetadata', () => {
});
});

it('resolves metadata using keyTo, but not keyFrom', () => {
it('infers keyFrom if it is not provided', () => {
const meta = resolveHasManyMetadata(Category.definition.relations[
'items'
] as HasManyDefinition);

expect(meta).to.not.have.property('keyFrom');

expect(meta).to.eql({
name: 'items',
type: 'hasMany',
targetsMany: true,
source: Category,
keyFrom: 'id',
target: () => Item,
keyTo: 'categoryId',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ describe('keyTo and keyFrom with resolveHasOneMetadata', () => {
});
});

it('resolves metadata using keyTo, but not keyFrom', () => {
it('infers keyFrom if it is not provided', () => {
const meta = resolveHasOneMetadata(Category.definition.relations[
'item'
] as HasOneDefinition);

expect(meta).to.not.have.property('keyFrom');

expect(meta).to.eql({
name: 'item',
type: 'hasOne',
targetsMany: false,
source: Category,
keyFrom: 'id',
target: () => Item,
keyTo: 'categoryId',
});
Expand Down
13 changes: 4 additions & 9 deletions packages/repository/src/relations/has-many/has-many.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ export function resolveHasManyMetadata(
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 explicit cast is needed because of a limitation of type inference
return relationMeta as HasManyResolvedDefinition;
}

const sourceModel = relationMeta.source;
if (!sourceModel || !sourceModel.modelName) {
const reason = 'source model must be defined';
Expand All @@ -54,8 +47,10 @@ export function resolveHasManyMetadata(

const keyFrom = sourceModel.getIdProperties()[0];

if (relationMeta.keyTo) {
// The explict cast is needed because of a limitation of type inference
// 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 explicit cast is needed because of a limitation of type inference
return Object.assign(relationMeta, {keyFrom}) as HasManyResolvedDefinition;
}

Expand Down
13 changes: 4 additions & 9 deletions packages/repository/src/relations/has-one/has-one.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ export function resolveHasOneMetadata(
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 explicit cast is needed because of a limitation of type inference
return relationMeta as HasOneResolvedDefinition;
}

const sourceModel = relationMeta.source;
if (!sourceModel || !sourceModel.modelName) {
const reason = 'source model must be defined';
Expand All @@ -54,8 +47,10 @@ export function resolveHasOneMetadata(

const keyFrom = sourceModel.getIdProperties()[0];

if (relationMeta.keyTo) {
// The explict cast is needed because of a limitation of type inference
// 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 explicit cast is needed because of a limitation of type inference
return Object.assign(relationMeta, {keyFrom}) as HasOneResolvedDefinition;
}

Expand Down

0 comments on commit f058920

Please sign in to comment.