loopback icon indicating copy to clipboard operation
loopback copied to clipboard

nested create through REST

Open mkeremguc opened this issue 11 years ago • 7 comments

either i couldn't find in documentation or this is a feature request:

Country hasMany Cities City belongsTo Country

is it possible to create/update models through REST like:

POST api/country
{
 "name": "England",
 "cities": [{"name":"London"}, {"name":"Manchester"}]
}

that would create the country and then create 2 cities with the names accordingly and set the hasmany/belongsto relation between. and even more:

POST api/country
{
 "name": "England",
 "cities": [{"id":1}, {"id":2}]
}

that would create the country and set the relation accordingly with the specified ids.

I tried this without any success and i wonder if this is already possible with a different way. . i'm new to loopback and trying to discover the capabilities. sorry if this is a duplicate or already implemented feature. i would really want to know if so.

mkeremguc avatar Mar 10 '15 00:03 mkeremguc

It sounds like City should be an Embedded Model http://docs.strongloop.com/display/public/LB/Embedded+models+and+relations

esco avatar Mar 15 '15 05:03 esco

ok but how to nested CREATE/UPDATE over REST api ?

mkeremguc avatar Mar 22 '15 14:03 mkeremguc

I want to +1 the question of @mkeremguc, how are you supposed to achieve a nested create/update over the REST API?

I started trying to hook into the after save hook, however, at that state the posted data for the relation had already been stripped away. Then I started fiddling with the dataSourceAttached event, and override the create method with a custom one. At that point I do have access to all the data that was sent from the client, but it seems like I have to (?) call the default create function in the end which makes it impossible for me to just create the model and the relations and then respond to the client.

This is what I have so far:

module.exports = function (Subject) {
  Subject.on('dataSourceAttached', function (obj) {
    var create = Subject.create;
    Subject.create = function (data, cb) {
      // I cant create the relations here since the "Subject" object
      // isn't created until the "create.apply" has been run. So why
      // don't I just put it after? The original create function sends
      // a response to the client and I want to do it before.

      return create.apply(this, arguments);
    };
  });
};

How would you handle creation of nested resources in LoopBack?

EDIT: And I don't want an embedded model since this would make it store it as JSON in MySQL (if I've understood it correctly).

Bazze avatar Jul 23 '15 14:07 Bazze

Knowing whether it is possible to create embedded models over REST would be very useful for architectural decisions I'm making now.

gausie avatar Jul 28 '15 11:07 gausie

has there been any progress or a decision made on resolving this issue, or what is the (official) recommended workaround in the interim?

athieme avatar Oct 05 '16 17:10 athieme

It seems this is unsupported. You have to create separate POST requests or implement your own handler.

https://loopback.io/doc/en/lb3/Relation-REST-API.html

nakamorichi avatar Apr 07 '17 07:04 nakamorichi

Hi there,

Is there any progress on this? I am currently migrating our existing API services application from .NET Entity framework to loopback , basically preparing the POC for this. New to this and exploring more, Very interesting indeed, But stuck at this point, because in .net entity side have nested insert with relational models, It will be much easier for me. Sorry if it is wrong thread

jersonjohn avatar Nov 17 '17 07:11 jersonjohn