Hi,
Now that LoopBack supports the inclusion of related models (https://strongloop.com/strongblog/inclusion-of-related-models/) I'm trying to figure out if there is a way to do the same for POST operations. (looks that no, based on #3439)
I have actually a case where I have several endpoints, like
(1) /endpoint1
(2) /endpoint1/{id}/endpoint2
And the idea is be able to send in endpoint1 a full json (including the schema 1 and 2). At the same time, must be possible to use (2) to create a new endpoint2 or GET it.
At this time I have a (not nice) workaround, where I duplicate the models and replace the relationship decorators with properties (hasMany -> property.array) and use the second model in the controller to check the request-body.
In addition, my repository implements a logic to take apart the endpoint1 and call several other repositories and transform a single POST to several DB inserts.
Any better idea of how to implement this?
Hi @gczobel-f5
The closest example I know of is in the Loopback4-shopping-example that involves two repository calls inside the controller.
// create the new user
const savedUser = await this.userRepository.create(
_.omit(newUserRequest, 'password'),
);
// set the password
await this.userRepository
.userCredentials(savedUser.id)
.create({password});
Hope that helps.
Closing as resolved.
Most helpful comment
Hi @gczobel-f5
The closest example I know of is in the Loopback4-shopping-example that involves two repository calls inside the controller.
Hope that helps.