sourcefuse / loopback4-starter

Loopback 4 starter application. Multi-tenant architecture supported. Authentication, Authorization, Soft deletes, environment vars, Audit logs, included.
MIT License
158 stars 59 forks source link

How to use include support? #64

Closed Code-Crash closed 3 years ago

Code-Crash commented 3 years ago

Hi @team,

I'm trying to setup this repository, and it's done perfectly.

Now, If I want to include credentials while finding user by id, how can I do that?

I tried as below:

async findById(@param.path.number('id') id: number): Promise<User> {
    return await this.userRepository.findById(id, { include: [{ relation: "credentials" }] });
}

but it did not work, got error:

Unhandled error in GET /users/1: 500 Error: Relation "credentials" is not defined for users model.

Note: I understood there is no need to include credentials on client side, but I was testing for hasOne relation include which I want to utilise in different model

akshatdubeysf commented 3 years ago

Hi @Code-Crash ,

To resolve a relation, the repository of a model must register an inclusionResolver. So to include credentials in your find calls, you would have to add this in the UserRepository -

this.registerInclusionResolver('credentials', this.credentials.inclusionResolver);;

The loopback4 docs explain this here.

Code-Crash commented 3 years ago

@akshatdubeysf Thanks, let me check and will close the issue.

Code-Crash commented 3 years ago

@akshatdubeysf Thanks a lot, it worked. closing the issue.