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

Return associated data from joined table? #177

Closed
kerimdragolj opened this issue Nov 8, 2017 · 1 comment
Closed

Return associated data from joined table? #177

kerimdragolj opened this issue Nov 8, 2017 · 1 comment

Comments

@kerimdragolj
Copy link

I have associations like this

  events.belongsTo(models.users, {
      foreignKey: {
        name: 'creatorId',
        allowNull: false
      },
      onDelete: 'CASCADE'
    });
    events.belongsToMany(models.users, {
      through: 'event_participants'
    });
    models.users.belongsToMany(events, {
      through: 'event_participants'
    });

So i have tables events, users and event_participants. For now I'm returning just events by creatorId like this

hook.params.query = {
            $or: [
              { creatorId: '' + hook.params.user.id },
              {
                //TODO Model participants as many to many
              }
            ]
          }

But i also need to return associated data from joined table, so i need to return all events that this current user is associated with, so not only where he is creator, but where he is participant through event_participants table.

Also I'm trying to make method that will be called in after: hooks on find and get. In this method i should already have all needed events, but now for each event i need to make array of paricipants where i'm going to include all users that are associated with each event in joined table (event_participants). I was reading docs and i think it has to do something with hydrate/populate/include but couldnt make it work. For now it looks like this unction

includeParticipants(hook) {
  return hook.app.service('users').get(hook.params.payload.userId).then(user => {
    const model = hook.app.service('users').Model;
    const association = { include: [{ model: model, as: 'participants', attributes: ['userId'] }] };
    // TODO INCLUDE CRETOR AND PARTICIPANTS
    hydrate(association).call(this, hook);
    //but it gives me error: 'cannot read property 'Instance' of undefined

    hook.result.creator = { fulname: user.ime + ' ' + user.prezime };
    return hook;
  });
}

Hope its understendable and not too messy. I'm newbie and still trying to figure out how hooks and feathers work.

@thinksentient
Copy link

In case anyone else comes across a similar issue, looks like hydrate() expects to be called in the context of a service. This worked for me:
hydrate(association).call(hook.service, hook);

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

2 participants