# Extra attributes given input values in query string

**URL:** <https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418>\
**Category:** Uncategorized\
**Created:** [April 5, 2016, 9:30pm UTC](https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418 "2016-04-05T21:30:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielebr](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@danielebr](https://discuss.jsonapi.org/u/danielebr)\
**Post date:** [April 5, 2016, 9:30pm UTC](https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418/1 "2016-04-05T21:30:03Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![jlangley](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jlangley](https://discuss.jsonapi.org/u/jlangley)\
**Post date:** [April 6, 2016, 3:23pm UTC](https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418/2 "2016-04-06T15:23:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielebr](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@danielebr](https://discuss.jsonapi.org/u/danielebr)\
**Post date:** [April 6, 2016, 6:14pm UTC](https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418/3 "2016-04-06T18:14:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jlangley](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jlangley](https://discuss.jsonapi.org/u/jlangley)\
**Post date:** [April 7, 2016, 7:47am UTC](https://discuss.jsonapi.org/t/extra-attributes-given-input-values-in-query-string/418/4 "2016-04-07T07:47:51Z")

</div>

> 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).
