Skip to content

Commit

Permalink
docs: belongsTo decorator parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakrkris committed Sep 25, 2019
1 parent fe3fc79 commit 9c92082
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,31 @@ 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
ensure that the decorated property is correctly defined.
Usage of belongsTo relation is similar to legacy datasource juggler.

The `@belongsTo` decorator takes three parameters
- the target model Class,
- the relation definition has three attributes - keyFrom, keyTo, name
- keyFrom is a property name on the "source" model (e.g. Order.customerId). keyFrom attribute has become a dummy parameter and is not relevant any more with the recent changes to use the decorated target as the actual keyFrom value.
- keyTo is a property name on the "target" model, typically the primary key of the "target" model.
- name is the relation name model name.
- property definition, this creates a property decorator implicitly. The name attribute in the definition can be used to customize datasource column name.

A usage of the decorator with a custom primary key of the target model for the
above example is as follows:

```ts
class Order extends Entity {
// constructor, properties, etc.
@belongsTo(() => Customer, {keyTo: 'pk'})
@belongsTo(
() => Customer,
{ keyFrom: 'customerId', keyTo: 'id', name: 'customer' },
{ name: 'customer_id' }
)
customerId: number;
}

Expand All @@ -103,6 +109,25 @@ export interface OrderRelations {
}
```


The above example can also be trimmed as follows,
- keyFrom defaults to `{decorated-property-name}`
- keyTo is `id` and relation name defaults to `{decorated-property-name}` after stripping the trailing `Id` suffix.


```ts
class Order extends Entity {
// constructor, properties, etc.
@belongsTo(() => Customer, { keyTo: 'id' }),
customerId: number;
}

export interface OrderRelations {
customer?: CustomerWithRelations;
}
```


## Configuring a belongsTo relation

The configuration and resolution of a `belongsTo` relation takes place at the
Expand Down

0 comments on commit 9c92082

Please sign in to comment.