Skip to content

Question: best way to aggregate relations of a model instance #198

Closed

Description

@Cadrach

Hi,

I am trying to create a remoteMethod that will return the requested object and all its related sub objects (like getting a User and all his posts at the same time).

So Im doing the following:

User.full = function(id, callback){
    User.findById(id, function(err, user){
        user.posts(function(err, posts){
            //user.posts = posts; << this is completely ignored
            callback(err, user, posts);
        })
    });
}

loopback.remoteMethod(
    User.full,
    {
        description: 'User+Posts',
        accepts: [
            {arg: 'id', type: 'String', required: true, description: 'The id of the user'}
        ],
        returns: [
            {arg: 'user', type: 'object'},
            {arg: 'posts', type: 'array' }
        ]
    }
);

I have a few issues with this:

  • It does not reproduce the structure of the object (I have a "user" property and a "posts" property at the same level)
  • I tried affecting the "posts" to user.posts before launching the callback, but it looks like since there is a "posts" function declared in the prototype of the model it is ignored completely.

So how should I go about it if I want to fetch a model instance and all its related instances and return them nicely?

Activity

  1. raymondfeng commented on Feb 23, 2014

    @raymondfeng
    Member

    I put together an example, please check it out at:

    https://github.com/strongloop-community/loopback-example-datagraph

    Thanks,
    Raymond

    On Feb 22, 2014, at 4:17 PM, Cadrach notifications@github.com wrote:

    Hi,

    I am trying to create a remoteMethod that will return the requested object and all its related sub objects (like getting a User and all his posts at the same time).

    So Im doing the following:

    User.full = function(id, callback){
    User.findById(id, function(err, user){
    user.posts(function(err, posts){
    //user.posts = posts; << this is completely ignored
    callback(err, user, posts);
    })
    });
    }

    loopback.remoteMethod(
    User.full,
    {
    description: 'User+Posts',
    accepts: [
    {arg: 'id', type: 'String', required: true, description: 'The id of the user'}
    ],
    returns: [
    {arg: 'user', type: 'object'},
    {arg: 'posts', type: 'array' }
    ]
    }
    );
    I have a few issues with this:

    It does not reproduce the structure of the object (I have a "user" property and a "posts" property at the same level)
    I tried affecting the "posts" to user.posts before launching the callback, but it looks like since there is a "posts" function declared in the prototype of the model it is ignored completely.
    So how should I go about it if I want to fetch a model instance and all its related instances and return them nicely?

    —
    Reply to this email directly or view it on GitHub.

  2. Cadrach commented on Feb 23, 2014

    @Cadrach
    Author

    Thanks, the example allowed me to do exactly what I wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions