Extra attributes given input values in query string


#1

How to expand a resource with new attributes given a value in query string?

I have a resource named product that has an attribute named units.
This attribute specifies the number of times the resource can be bought in a specific day.
Everytime the user buys the resource is created a new transaction resource related to the product resource. The relation is completed with the buy date.

So, the api /products returns all the product’s attributes and the value of ‘units’.

I’d like to obtain by that api the actual number of units (calculated by counting the transaction objects) when is given in query string the transaction date.

What’s the best way to achieve this?
How to specify the transaction date in query string?

Options:

  1. update the units attribute with the actual number of units
  2. always add an attribute named actual_units filled with null when the transaction date is not provided
  3. add an attribute actual_units only only when is set the transaction date.

None of the include or field keys described in the json specifications satisfy this situation.

Thanks :wink:


#2

You could model the days as a collection resource related to product (or vice versa - a day could be a first class resource that has related products). I think this is mostly a modelling issue, not one related to the JSON API format itself.


#3

I don’t think it’s modelling problem.
In this situation I basically have a query to do. Generally the query is intended to filter results but in this case it would be used to expand the attributes of a resource.

In other words what I need is to include attributes and not relations in the main resource object. And this inclusion is made only having the transaction date in query string (I can decide to use a default value if not specified and so use the option 2, but I still need to introduce a concept of extension to the resource object)

Even a different resources modelling is not enough because I’d still need to specify the transaction date in someway.

Plus, I’d prefer not to return all the product’s transactions just to made a count on it…

Has nobody ever had this need?


#4

Plus, I’d prefer not to return all the product’s transactions just to made a count on it…

You don’t have to - if the transactions are modelled as a collection, you can use the meta tag to return a count for the entire collection (the collection could be a thin & paged representation of the collection, e.g. IDs of first 10 ten transactions only).

You would then need separate transaction collections for each day, but that’s a modelling issue again (could be a collection of collections, or could be flattened into lots of relationships directly against the product).