Loopback-next: Inclusion of related models for POST

Created on 15 Jan 2020  路  2Comments  路  Source: strongloop/loopback-next

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?

question

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.

      // 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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mhdawson picture mhdawson  路  3Comments

cloudwheels picture cloudwheels  路  3Comments

teambitcodeGIT picture teambitcodeGIT  路  3Comments

aceraizel picture aceraizel  路  3Comments

acrodrig picture acrodrig  路  3Comments