Skip to content

Commit

Permalink
docs: add url, diagram for 3 relations for inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Oct 28, 2019
1 parent 2116d53 commit 49f38db
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
31 changes: 29 additions & 2 deletions docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ on constructor to avoid "Circular dependency" error (see

## Querying related models

Different from LB3, LB4 creates a different inclusion resolver for each relation
type to query related models. Each **relation** has its own inclusion resolver
`inclusionResolver`. And each **repository** has a built-in property
`inclusionResolvers` as a registry for its inclusionResolvers. Here is a diagram
to show the idea:

![inclusion](./imgs/relation-inclusion.png)

A `belongsTo` relation has an `inclusionResolver` function as a property. It
fetches target models for the given list of source model instances.

Expand All @@ -336,12 +344,18 @@ belongs to a `Customer`.

After setting up the relation in the repository class, the inclusion resolver
allows users to retrieve all orders along with their related customers through
the following code:
the following code at the repository level:

```ts
orderRepo.find({include: [{relation: 'customer'}]});
```

or use APIs with controllers:

```
GET http://localhost:3000/orders?filter[include][][relation]=customer
```

### Enable/disable the inclusion resolvers:

- Base repository classes have a public property `inclusionResolvers`, which
Expand Down Expand Up @@ -380,12 +394,21 @@ export class OrderRepository extends DefaultCrudRepository {
```

- We can simply include the relation in queries via `find()`, `findOne()`, and
`findById()` methods. Example:
`findById()` methods. For example, these queries return all orders with their
`Customer`:

if you process data at the repository level:

```ts
orderRepository.find({include: [{relation: 'customer'}]});
```

this is the same as the url:

```
GET http://localhost:3000/orders?filter[include][][relation]=customer
```

which returns:

```ts
Expand Down Expand Up @@ -419,6 +442,10 @@ export class OrderRepository extends DefaultCrudRepository {
];
```

Here is a diagram to make this more intuitive:

![Graph](./imgs/belongsTo-relation-graph.png)

- You can delete a relation from `inclusionResolvers` to disable the inclusion
for a certain relation. e.g
`orderRepository.inclusionResolvers.delete('customer')`
Expand Down
37 changes: 32 additions & 5 deletions docs/site/HasMany-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ values for these three fields:
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Description</th>
<th>Default Value</th>
<th width="95">Field Name</th>
<th width="260">Description</th>
<th width="260">Default Value</th>
<th>Example</th>
</tr>
</thead>
Expand Down Expand Up @@ -421,6 +421,14 @@ issue](https://github.com/strongloop/loopback-next/issues/1179) to follow the di

## Querying related models

Different from LB3, LB4 creates a different inclusion resolver for each relation
type to query related models. Each **relation** has its own inclusion resolver
`inclusionResolver`. And each **repository** has a built-in property
`inclusionResolvers` as a registry for its inclusionResolvers. Here is a diagram
to show the idea:

![inclusion](./imgs/relation-inclusion.png)

A `hasMany` relation has an `inclusionResolver` function as a property. It
fetches target models for the given list of source model instances.

Expand All @@ -429,12 +437,18 @@ many `Order`s.

After setting up the relation in the repository class, the inclusion resolver
allows users to retrieve all customers along with their related orders through
the following code:
the following code at the repository level:

```ts
customerRepo.find({include: [{relation: 'orders'}]});
```

or use APIs with controllers:

```
GET http://localhost:3000/customers?filter[include][][relation]=orders
```

### Enable/disable the inclusion resolvers:

- Base repository classes have a public property `inclusionResolvers`, which
Expand Down Expand Up @@ -472,12 +486,21 @@ export class CustomerRepository extends DefaultCrudRepository {
```

- We can simply include the relation in queries via `find()`, `findOne()`, and
`findById()` methods. Example:
`findById()` methods. For example, these queries return all customers with
their `Order`s:

if you process data at the repository level:

```ts
customerRepository.find({include: [{relation: 'orders'}]});
```

this is the same as the url:

```
GET http://localhost:3000/customers?filter[include][][relation]=orders
```

which returns:

```ts
Expand All @@ -498,6 +521,10 @@ export class CustomerRepository extends DefaultCrudRepository {
];
```

Here is a diagram to make this more intuitive:

![Graph](./imgs/hasMany-relation-graph.png)

- You can delete a relation from `inclusionResolvers` to disable the inclusion
for a certain relation. e.g
`customerRepository.inclusionResolvers.delete('orders')`
Expand Down
37 changes: 32 additions & 5 deletions docs/site/hasOne-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ values for these three fields:
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Description</th>
<th>Default Value</th>
<th width="95">Field Name</th>
<th width="260">Description</th>
<th width="260">Default Value</th>
<th>Example</th>
</tr>
</thead>
Expand Down Expand Up @@ -371,6 +371,14 @@ issue](https://github.com/strongloop/loopback-next/issues/1179) to follow the di

## Querying related models

Different from LB3, LB4 creates a different inclusion resolver for each relation
type to query related models. Each **relation** has its own inclusion resolver
`inclusionResolver`. And each **repository** has a built-in property
`inclusionResolvers` as a registry for its inclusionResolvers. Here is a diagram
to show the idea:

![inclusion](./imgs/relation-inclusion.png)

A `hasOne` relation has an `inclusionResolver` function as a property. It
fetches target models for the given list of source model instances.

Expand All @@ -379,12 +387,18 @@ Using the relation between `Supplier` and `Account` we have shown above, a

After setting up the relation in the repository class, the inclusion resolver
allows users to retrieve all suppliers along with their related account
instances through the following code:
instances through the following code at the repository level:

```ts
supplierRepository.find({include: [{relation: 'account'}]});
```

or use APIs with controllers:

```
GET http://localhost:3000/suppliers?filter[include][][relation]=account
```

### Enable/disable the inclusion resolvers:

- Base repository classes have a public property `inclusionResolvers`, which
Expand Down Expand Up @@ -419,12 +433,21 @@ export class SupplierRepository extends DefaultCrudRepository {
```

- We can simply include the relation in queries via `find()`, `findOne()`, and
`findById()` methods. Example:
`findById()` methods. For example, these queries return all suppliers with
their `Account`:

if you process data at the repository level:

```ts
supplierRepository.find({include: [{relation: 'account'}]});
```

this is the same as the url:

```
GET http://localhost:3000/suppliers?filter[include][][relation]=account
```

which returns:

```ts
Expand All @@ -442,6 +465,10 @@ export class SupplierRepository extends DefaultCrudRepository {
];
```

Here is a diagram to make this more intuitive:

![Graph](./imgs/hasOne-relation-graph.png)

- You can delete a relation from `inclusionResolvers` to disable the inclusion
for a certain relation. e.g
`supplierRepository.inclusionResolvers.delete('account')`
Expand Down
Binary file added docs/site/imgs/belongsTo-relation-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/site/imgs/hasMany-relation-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/site/imgs/hasOne-relation-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/site/imgs/relation-inclusion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49f38db

Please sign in to comment.