keithwhor / nodal

API Services Made Easy With Node.js
http://www.nodaljs.com/
MIT License
4.51k stars 209 forks source link

Show() to generate joins #249

Closed iezekiel closed 8 years ago

iezekiel commented 8 years ago

How can i get from show() the same details as index() For example. Tweet.query() .where(this.params.query) .join('user') .end((err, models) => { this.respond(err || models, ['id', 'body', 'created_at', 'user']); });

But for show() where i can add join('user') to also select user into model Tweet.find(this.params.route.id, (err, model) => { this.respond(err || model); });

keithwhor commented 8 years ago

You can either do use the index query with Tweet.query({id: this.params.id}).join('user').end(...) or you can do (in the find call):

[...].find((err, model) => {
  if (err) {
    return this.respond(err);
  }

  model.include((err, user) => {
    // include runs a separate query to grab the user model, returns it as a parameter
    // does matching based on parameter name
    this.respond(err || model, ['id', 'body', 'created_at', 'user']);
  });

});