Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update belongsTo docs with decorator syntax #3808

Merged
merged 1 commit into from
Oct 8, 2019
Merged

Conversation

deepakrkris
Copy link
Contributor

@deepakrkris deepakrkris commented Sep 25, 2019

Fixes #2639

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@deepakrkris deepakrkris changed the title docs: update belongsTo docs to include keyFrom and keyTo docs: update belongsTo docs with decorator syntax Sep 25, 2019
@deepakrkris deepakrkris self-assigned this Sep 25, 2019
@deepakrkris
Copy link
Contributor Author

deepakrkris commented Sep 26, 2019

@agnes512 agree on your comment to add example for modifying foreign key property in source model on the previous PR #3772. I have added in this PR.

@deepakrkris
Copy link
Contributor Author

closed previous PR #3772 because of travis job errrors.

docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
@belongsTo(
() => Customer,
{keyFrom: 'customerId', keyTo: 'id', name: 'customer'},
{name: 'customer_id'},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think {keyFrom: '.., name: ..} is enough for customizing the name ( or even just to have the relation name {name: ..})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example is to provide all the parameters for the decorator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deepakrkris When looking at the decorator's implementation, I feel the 3rd parameter(property definition) is more for configuring the property, like:

name should be inferred from the property cust_Id itself.

Copy link
Contributor Author

@deepakrkris deepakrkris Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jannyHou that is very strange. PropertyDefinition does not have a name key defined, but typescript is accepting the name attribute ! Also I am explicitly using the name key to customize db column names and it works perfectly fine for the todo-list example

Copy link
Contributor Author

@deepakrkris deepakrkris Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jannyHou , also one more thing, cust_Id being a customized name (per standard naming convention it should be customerId), the name attribute to indicate target model cannot be inferred from the customized name, it has to be explicitly stated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deepakrkris sorry for replying late (refreshed the PR several times last week but somehow git didn't show your message, weird...)

PropertyDefinition does not have a name key defined, but typescript is accepting the name attribute

It's because type PropertyDefinition allows arbitrary properties, see code

Also I am explicitly using the name key to customize db column names and it works perfectly fine for the todo-list example

Thank you got it, so it's used to configure the db column name.

docs/site/BelongsTo-relation.md Show resolved Hide resolved
@deepakrkris deepakrkris force-pushed the fix2639 branch 4 times, most recently from 724f6ed to bf96ded Compare September 26, 2019 16:59
Copy link
Member

@dhmlau dhmlau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor comment, otherwise LGTM.

docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
@emonddr
Copy link
Contributor

emonddr commented Sep 26, 2019

Check one or both of these, depending on your changes..

image

@deepakrkris deepakrkris force-pushed the fix2639 branch 3 times, most recently from 1f96124 to 7e84bfe Compare September 26, 2019 19:01
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deepakrkris Good stuff! Looks good in general, I left a few comments.

@belongsTo(
() => Customer,
{keyFrom: 'customerId', keyTo: 'id', name: 'customer'},
{name: 'customer_id'},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deepakrkris When looking at the decorator's implementation, I feel the 3rd parameter(property definition) is more for configuring the property, like:

name should be inferred from the property cust_Id itself.

docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
@agnes512 agnes512 self-requested a review September 30, 2019 19:44
@deepakrkris deepakrkris force-pushed the fix2639 branch 2 times, most recently from 2db04ae to 40b8dc6 Compare September 30, 2019 23:04
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
@deepakrkris deepakrkris force-pushed the fix2639 branch 10 times, most recently from 6e491fb to 8bd0c19 Compare October 4, 2019 22:04
@deepakrkris deepakrkris force-pushed the fix2639 branch 4 times, most recently from 1260549 to 57983c4 Compare October 7, 2019 16:16
Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
My only (irrelevant) concern is that we might want to improve the code base to deal with default values.

constructor and designates the relation type. It also calls `property()` to
ensure that the decorated property is correctly defined.
The standard naming convention for the foreign key property in the source model
is `relation name` + `Id` (for example, Order.customerId).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation here looks weird. It seems like that we define the relation name first, then we decide the default value of our foreign key name. This is because keyFrom defaults to the decorated property name, not targetModel + Id.

I wanted to point out in this PR that, in our code, the way we get the default relation name is by modifying the decorated property name:

customerId -> (get rid of `id`) -> customer

I found this code is really confusing. For example, if we decorate the foreign key name as

@belongsTo(..)
anyRandomNameId

the default relation name will be anyRandomName, which doesn't make sense at all. I guess it was designed to use the default foreign key customerId. But the foreign key doesn't default to targetModel + Id. It defaults to the decorated name. That's the reason makes it hard to document. In short, the code doesn't fully deal with default values as we expected.

We might want to improve the way to get the relation name in belongTo in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes the code is not very self explanatory in terms of defining relations as of now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the FK usually refers to the target model, such as customerId. anyRandomNameId does not name the FK meaningfully. In such cases, the options.name should be used.

docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
docs/site/BelongsTo-relation.md Outdated Show resolved Hide resolved
@deepakrkris deepakrkris merged commit 7f06b10 into master Oct 8, 2019
@deepakrkris deepakrkris deleted the fix2639 branch October 8, 2019 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Docs] belongsTo() not working in loopback4 - Cannot read property 'target' of undefined
8 participants