Skip to content

Commit

Permalink
fix: fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Sep 12, 2019
2 parents a5cbac2 + 90ecdb9 commit e1c19f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
78 changes: 40 additions & 38 deletions docs/site/HasMany-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,15 @@ issue](https://github.com/strongloop/loopback-next/issues/1179) to follow the di

## Querying related models

Loopback4 has the concept of `inclusion resolver` in relations, which helps to
query data through an include filter. An inclusion resolver is a function that
can fetch target models for the given list of source model instances. LB4
creates a different inclusion resolver for each relation type.
LoopBack 4 has the concept of an `inclusion resolver` in relations, which helps
to query data through an `include` filter. An inclusion resolver is a function
that can fetch target models for the given list of source model instances.
LoopBack 4 creates a different inclusion resolver for each relation type.

The following is an example for a has-many inclusion resolver:
The following is an example for a HasMany inclusion resolver:

- Model `Customer`
- Model `Order`
- `Customer` has many `Order`
- Two models: `Customer` and `Order`
- A `Customer` has many `Order`s

```ts
// import statements
Expand All @@ -349,8 +348,9 @@ class Order extends Entity {
}
```

The inclusion resolver allows users to retrieve all customers along with all
related orders by the following code:
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:

```ts
customerRepo.find({include: [{relation: 'orders'}]});
Expand All @@ -360,9 +360,9 @@ customerRepo.find({include: [{relation: 'orders'}]});

- Base repository classes have a public property `inclusionResolvers`, which
maintains a map containing inclusion resolvers for each relation.
- The inclusion resolver `inclusionResolver` of a certain relation is built when
the source repository class calls the `createHasManyRepositoryFactoryFor`
function in the constructor with the relation name.
- The `inclusionResolver` of a certain relation is built when the source
repository class calls the `createHasManyRepositoryFactoryFor` function in the
constructor with the relation name.
- Call `registerInclusionResolver` to add the resolver of that relation to the
`inclusionResolvers` map. (As we realized in LB3, not all relations are
allowed to be traversed. Users can decide to which resolvers can be added.)
Expand All @@ -380,7 +380,7 @@ export class CustomerRepository extends DefaultCrudRepository {
) {
super(Customer, dataSource);

// we already have this line to create HasManyRepository factory
// we already have this line to create a HasManyRepository factory
this.orders = this.createHasManyRepositoryFactoryFor(
'orders',
orderRepositoryGetter,
Expand All @@ -392,30 +392,32 @@ export class CustomerRepository extends DefaultCrudRepository {
}
```

- We can simply include the relation in queries via find(), findOne(),
findById() methods. Example:

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

which returns:

```ts
[
{
id: 1,
name: 'Thor',
orders:[{name: 'Mjolnir', customerId: 1}, {name: 'Rocket Raccoon', customerId: 1}]
},
{
id: 2,
name: 'Captain',
orders:[{name: 'Shield', customerId: 2}]
},
...
]
```
- We can simply include the relation in queries via `find()`, `findOne()`, and
`findById()` methods. Example:

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

which returns:

```ts
[
{
id: 1,
name: 'Thor',
orders: [
{name: 'Mjolnir', customerId: 1},
{name: 'Rocket Raccoon', customerId: 1},
],
},
{
id: 2,
name: 'Captain',
orders: [{name: 'Shield', customerId: 2}],
},
];
```

- You can delete a relation from `inclusionResolvers` to disable the inclusion
for a certain relation. e.g
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import {expect} from '@loopback/testlab';
import {
buildLookupMap,
reduceAsArray,
findByForeignKeys,
reduceAsArray,
reduceAsSingleItem,
} from '../../../..';
import {
Expand Down Expand Up @@ -93,9 +93,9 @@ describe('buildLoopupMap', () => {
);
const expected = new Map<Number, Category>();
expected.set(1, cat);
//expected.set(2, [pencils]);
expect(result).to.eql(expected);
});

it('returns multiple instances when multiple target instances are passed in', async () => {
const cat1 = await categoryRepo.create({id: 1, name: 'Angus'});
const cat2 = await categoryRepo.create({id: 2, name: 'Nola'});
Expand Down

0 comments on commit e1c19f9

Please sign in to comment.