Skip to content

Commit

Permalink
docs: update belongsTo docs to include keyFrom and keyTo
Browse files Browse the repository at this point in the history
Fixes #2639
  • Loading branch information
deepakrkris committed Sep 25, 2019
1 parent a934fd9 commit f82c5e8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,51 @@ export interface OrderRelations {
export type OrderWithRelations = Order & OrderRelations;
```


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.
- the `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;
}

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

The above example can also be trimmed as follows,
- keyFrom defaults to `{decorated-property-name}`
- keyTo is `id`
- the relation name defaults to `{decorated-property-name}` after stripping the trailing `Id` suffix.
- The standard naming convention for the foreign key property in the source model is `relation name` + `Id`. (e.g. Order.customerId). If the property name in the source model has to be customized then the relation name ( `name` attribute in the relation definition) has to be stated explicitly. Also, the corresponding `hasMany` relation in the `target` model has to be modified with the correct `keyTo` value.


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

Expand All @@ -95,6 +128,7 @@ export interface OrderRelations {
}
```


## Configuring a belongsTo relation

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

0 comments on commit f82c5e8

Please sign in to comment.