From fe3fc79e108f58c0239ff5c9b8dd382d11f7db43 Mon Sep 17 00:00:00 2001 From: DEEPAK RAJAMOHAN Date: Thu, 19 Sep 2019 09:15:00 -0700 Subject: [PATCH] docs: update belongsTo docs to include keyFrom and keyTo Fixes https://github.com/strongloop/loopback-next/issues/2639 --- docs/site/BelongsTo-relation.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/site/BelongsTo-relation.md b/docs/site/BelongsTo-relation.md index 7c4eaf928b0d..da1bbe9f983e 100644 --- a/docs/site/BelongsTo-relation.md +++ b/docs/site/BelongsTo-relation.md @@ -75,6 +75,14 @@ export interface OrderRelations { export type OrderWithRelations = Order & OrderRelations; ``` + +Definition of `belongsTo` relation can also include explicit `keyFrom`, `keyTo` and `name` attributes. + +- keyFrom is foreign key of the referenced model, whereas keyTo is primary key of referenced model, name is the referenced model name. Usage of keyFrom and keyTo in a belongsTo relation is similar to the use in legacy datasource juggler. +- In the above example for the model Order, keyFrom is `customerId` and keyTo is `id`. keyFrom defaults to `{decorated-belongsTo-property-name}`, keyTo defaults to `id` and the referenced model name defaults to the decorated property name after stripping the trailing `Id` suffix. +- In case in the above example, the property name `customerId` has to be customized to say `cust_id`, then the keyFrom, keyTo and name values needs to be explicitly defined as `@belongsTo(() => Customer, { keyFrom: 'customerId', keyTo: 'id'}, { name: 'customer' })`. + + The definition of the `belongsTo` relation is inferred by using the `@belongsTo` decorator. The decorator takes in a function resolving the target model class constructor and designates the relation type. It also calls `property()` to