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

Inclusion of related models for POST #4435

Closed
gczobel-f5 opened this issue Jan 15, 2020 · 2 comments
Closed

Inclusion of related models for POST #4435

gczobel-f5 opened this issue Jan 15, 2020 · 2 comments
Assignees

Comments

@gczobel-f5
Copy link
Contributor

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?

@dougal83
Copy link
Contributor

dougal83 commented Jan 15, 2020

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.

@dhmlau
Copy link
Member

dhmlau commented Feb 21, 2020

Closing as resolved.

@dhmlau dhmlau closed this as completed Feb 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants