Comments (16)
@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.
from feathers-sequelize.
You can check out hook docs right here: http://docs.feathersjs.com/hooks/readme.html
from feathers-sequelize.
@lionelrudaz since you are using Postgres and therefore using Sequelize you could also utilize the include
directive. http://docs.sequelizejs.com/en/latest/docs/associations/
Then just normalize the data however you want it to look in an after
hook.
from feathers-sequelize.
Hi Erik,
Thanks for your quick answer.
I tried the first solution and got two issues:
-
The service retrieves all the messages. It's like the params aren't taken into account
-
I've got the error: Error: after hook for 'get' method returned invalid hook object
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
from feathers-sequelize.
Easy to miss, on the server, find
parameters have to be in a query
property:
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?
from feathers-sequelize.
Now the request works, thanks.
But I still get the following error:
Error: after hook for 'get' method returned invalid hook object
Here's the function:
var showMessages = function() {
return function(hook) {
return hook.app.service('/api/v1/messages').find({
query: {
conversationId: hook.result.conversation.id
}
}).then(result => {
return hook.result.conversation.messages = result;
});
}
}
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:
app.use('/api/v1/conversations', service({
Model: models.conversation
}));
Any thoughts?
from feathers-sequelize.
Hooks expect you to return hook
, a Promise that returns hook
or nothing. Yours would look like:
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;
});
}
}
from feathers-sequelize.
That's weird. The response isn't changed.
I tried this:
var showMessages = function() {
return function(hook) {
hook.result.conversation.test = "Test";
}
}
The response remain also unchanged.
{
conversation: {
id: 1,
title: "Test conversation",
createdAt: "2016-01-27T22:33:37.936Z",
updatedAt: "2016-01-27T22:33:37.936Z"
}
}
When I test without conversation:
var showMessages = function() {
return function(hook) {
hook.result.test = "Test";
}
}
It shows me the test value:
{
conversation: {
id: 1,
title: "Test conversation",
createdAt: "2016-01-27T22:33:37.936Z",
updatedAt: "2016-01-27T22:33:37.936Z"
},
test: "Test"
}
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?
from feathers-sequelize.
What does console.log(hook.result)
give you? One reason may be that it actually returns a model instance and doesn't allow adding properties to it (similar to Mongoose models). Try
var showMessages = function() {
return function(hook) {
var conversations = hook.result.conversations.get({ plain: true });
conversations.test = 'Test';
hook.result.conversations = conversations;
}
}
from feathers-sequelize.
All good, here's the final function:
var showMessages = function() {
return function(hook) {
var conversation = hook.result.conversation.get({ plain: true });
return hook.app.service('/api/v1/messages').find({
query: {
conversationId: hook.result.conversation.id
}
}).then(result => {
conversation.messages = result.messages.map(function(message) {
return message.id;
});
hook.result.conversation = conversation;
});
}
}
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?
from feathers-sequelize.
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.
from feathers-sequelize.
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.
from feathers-sequelize.
This may require a change in the plugin, adding a params.sequelize
to https://github.com/feathersjs/feathers-sequelize/blob/master/src/index.js#L31 like this:
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 find
(e.g. in a hook):
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.
from feathers-sequelize.
Should I close the issue?
from feathers-sequelize.
Ya I think we can close this now.
from feathers-sequelize.
Thanks again for the PR @lionelrudaz!
from feathers-sequelize.
Related Issues (20)
- Unable to modify hook result of referenced models HOT 1
- app.service('users').get(message.userId, params) returns createdAt and updatedAt dates as string rather than Date object on SQLite
- How can I use a limit and a required in an include? HOT 9
- Support Deep Aware for JSON Objects on Patch HOT 1
- Query by Association HOT 5
- id is not set correctly to a non-null value when using multi-create and throws an error instead of creating
- Got 404 No Record Found while using include in before get hooks HOT 1
- Optimize multi patch when $returning is false. HOT 3
- Use proper $and in patch and remove methods HOT 2
- Use consistent $select in all methods, optimize select function HOT 2
- iLike method with accent. HOT 1
- how to set field to NULL on patch request?
- Allow join queries on remove
- Better handle paranoid models HOT 2
- Since version 6.4.0 getting "error: GeneralError: column "id" does not exist" HOT 1
- feathers-sequelize remove method rewrite breaking change when deleting multiple items without id in table
- Associations Suggestion
- Service .find() method return a wrong type (not paginated)
- feathers sequlize type confict when overwritting find and calling super.find HOT 1
- Support withSchema [with PR] HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from feathers-sequelize.