Loopback: nested create through REST

Created on 10 Mar 2015  路  7Comments  路  Source: strongloop/loopback

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.

feature stale

Most helpful comment

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

All 7 comments

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

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

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

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings