-
Notifications
You must be signed in to change notification settings - Fork 74
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
How do you retrieve associations in the response? #12
Comments
@lionelrudaz It's late here so I'm just off to bed but you should be able to just use an after hook and make a call to your other service. // after hook
function(options) {
return function(hook) {
return hook.app.service('/messages').find({ conversationId: hook.result.conversation.id}).then(result => {
return hook.result.conversation.messages = result;
});
}
} I think that should be the idea, or at least be close. |
You can check out hook docs right here: http://docs.feathersjs.com/hooks/readme.html |
@lionelrudaz since you are using Postgres and therefore using Sequelize you could also utilize the Then just normalize the data however you want it to look in an |
Hi Erik, Thanks for your quick answer. I tried the first solution and got two issues:
With Sequelize, the thing is that include seems to be available only on an instance, and examples show only that it works with creation. So I found that we can query for associated objects here http://docs.sequelizejs.com/en/latest/docs/associations/#associating-objects. But in that way, that means that I have to create a conversation instance first, right? Sorry if my questions sound newbie. That's what I am :-) Thanks again |
Easy to miss, on the server, hook.app.service('/messages').find({
query: {
conversationId: hook.result.conversation.id
}
}) Don't the service methods return Sequelize model instances? I'm just familiar enough with Sequelize to get the adapter to work so I'm not sure. How do you retrieve a model and it's associations? |
Now the request works, thanks. But I still get the following error:
Here's the function:
You can view the code here: https://github.com/lionelrudaz/wellnow-node The services are in app.js for the moment and the models are in the models directory. I set the model to the service like that:
Any thoughts? |
Hooks expect you to return var showMessages = function() {
return function(hook) {
hook.app.service('/api/v1/messages').find({
query: {
conversationId: hook.result.conversation.id
}
}).then(result => {
hook.result.conversation.messages = result;
});
}
} |
That's weird. The response isn't changed. I tried this:
The response remain also unchanged.
When I test without conversation:
It shows me the test value:
I wonder if it's because I have two after hooks for the conversation service. One to add the results in conversation, one to add the association. Any clue? |
What does var showMessages = function() {
return function(hook) {
var conversations = hook.result.conversations.get({ plain: true });
conversations.test = 'Test';
hook.result.conversations = conversations;
}
} |
All good, here's the final function:
Now I'm struggling to do the same for the find method where I have to do that for each conversation. I think my limited competencies in JavaScript don't help there. Any recommendation? |
I'm not too familiar with Sequelize but isn't there a way to tell it to retrieve associated models? Making a query for each item might become a little inefficient this way. |
Yes, there is. http://docs.sequelizejs.com/en/latest/docs/querying/#relations-associations But I don't know how to fit that in my code. |
This may require a change in the plugin, adding a let query = Object.assign({
where, order,
limit: filters.$limit,
offset: filters.$skip,
attributes: filters.$select || null
}, params.sequelize); That way you could then pass those options to app.service('conversations').find({
sequelize: {
include: [{
model: Task,
where: { state: Sequelize.col('project.state') }
}]
}
}); If you make a pull request I can get it out as a new release pretty quick. |
Should I close the issue? |
Ya I think we can close this now. |
Thanks again for the PR @lionelrudaz! |
Hi guys,
I can't find that in any documentation.
I have a conversation model that has many messages.
I'd like to add messageIds to the response of the REST calls to conversation.
How can I do that? Is it with a hook?
I'd like to follow the recommandations from here: https://guides.emberjs.com/v1.10.0/models/the-rest-adapter/
Let me know if you need more information.
Thanks in advance,
Lionel
The text was updated successfully, but these errors were encountered: