js-data-sql icon indicating copy to clipboard operation
js-data-sql copied to clipboard

Enhancements to "with"

Open techniq opened this issue 9 years ago • 3 comments

Just logging some notes to think about at this point

Retrieve a subset of properties

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

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'}},
  ]
});

techniq avatar Feb 16 '16 04:02 techniq

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
}

jmdobry avatar Feb 16 '16 04:02 jmdobry

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?

techniq avatar Feb 16 '16 04:02 techniq

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'
    }
  }
});

jmdobry avatar Feb 16 '16 04:02 jmdobry