Reputation: 9229
I am trying to create a simple app, with a data one-to-many relationship. Where a site contains many locations. I am using feathers js and sequelize (with sqlite). The standard GET
functionality in feathers will return a site or a list of sites. However, I can't see how you are "supposed too" return child records.
If I was doing this in sequelize directly, I would do something like:
db.Site.findAll({
include: [db.Location]
}).then(function (sites) {
res.status(200).json({
data: sites
});
}).catch(err => res.status(500).send({ error: err }));
From the feathers guides, it looks as though I could put this logic in a hook (after the standard query?), and replace the collected data with the result of this query (and double the number of db calls). Or I could query just for locations with the current siteId
as their parentId
(again douubling the number of database calls).
However, I wondered whether there was a neater way built into feathers, to ensure that every time the standard GET
call is made, it's child elements are included.
Upvotes: 0
Views: 213
Reputation: 44215
This is done via params.sequelize
and documented in more detail in this FAQ and the feathers-sequelize documentation which shows using a before
hook on the server like this:
// GET /my-service?name=John&include=1
function (context) {
const { include, ...query };
if (include) {
const AssociatedModel = context.app.service('fooservice').Model;
context.params.sequelize = {
include: [{ model: AssociatedModel }]
};
// Update the query to not include `include`
context.params.query = query;
}
return context;
}
Upvotes: 0