Enhancements to "with"
Just logging some notes to think about at this point
Retrieve a subset of properties
- asked by aaike on Slack#i-need-help
var users = yield Event.findAll({...}, {
with: [
// load all comments, what we currently support
'comments',
// load only comments with category_id = 1 (shorthand for '==')
{comments: {category_id: 1}},
// load only comments with category_id > 1
{comments: {category_id: {'>': 1}}}
]
});
Support for adding calculated/aggregate properties
- discussed in Slack#js-data-sql
var events = yield Event.findAll({...}, {
with: [
// add `review_count` property to each event
{review_count: {'review.rating': 'count'}},
// add `review_average` property to each event
{review_average: {'review.rating': 'average'}},
]
});
I'm thinking rather than {comments: {category_id: 1}} we should do:
{
relation: 'comments', // "relation" is either the "localField" or name of the related resource
query: { category_id: 1 } // "query" follows JSData's query syntax
}
hmm, it's more verbose (which might not be a bad thing). I had all intentions of support all of JSData's query syntax with {comments: /* query syntax */}
What are you thoughts on the calculated/aggregate properties?
The issue with {comments: /* query syntax */} is it has to be parsed using something like Object.keys, whereas { relation: 'comments' ... gives instance information about which relation we're dealing with.
Maybe with isn't the right place to specify aggregations that should be included in the result? with is an array, seems like we need an object here, e.g.
var events = yield Event.findAll({...}, {
// This object is essentially "merged" into the result
// This seems fully expressive
compute: {
// add `review_count` property to each event
review_count: {
relation: 'review',
query: {}, // optional,
op: 'count'
},
// add `review_average` property to each event
review_average: {
relation: 'review',
query: {}, // optional selection query
op: 'average',
field: 'rating'
}
}
});