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: add url, diagram for 3 relations for inclusion #4007

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

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

is it an extra []? I am looking at the similar include filter in LB3 https://loopback.io/doc/en/lb2/Include-filter.html#rest-examples:

/customers?filter[include][reviews]=author

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, /customers?filter[include][reviews]=author wouldn't work because include.filter is not a function.

And different queries between LB3 and LB4 make me wonder if we need a querying data page for LB4 as well. Cause right now the only way to check the query syntax is to go to the LB3 site.

Copy link
Contributor

Choose a reason for hiding this comment

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

The include filter for LB2 is different from the LB4 one. In LB4 it's {include: [{relation: 'customer'}]}, whereas in LB2 (from my understanding) it's {include: {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.