Open jamesdixon opened 8 years ago
Another to point to mention:
Currently, the attributes returned as part of meta are not affected by the serializer's typeForAttribute
option. In my case, everything should be camelCased, but those attributes are snake_case.
@chamini2 to clarify, the PR allows for pivot data (data contained in join tables) to be automatically serialized into meta
data on a relationship.
Imagine that I have a three tables: appointment
, appointment_service
and appointment_pet
. The latter two tables are join tables that associate service
s and pet
s with an appointment
. However, what if I needed to specify that certain pet
s are only applicable to a specific service
? For example, I have a feeding service that should only apply to the dog, Max. The place to specify that information would be another field in appointment_service
-- let's call it specific_pets
. According to the JSON API folks, the place to put this type of information would be the meta
attr of the relation. This PR enables serialization of those pivot/join properties into the meta
attr of the relation.
So it's for belongsToMany
relations. But the test I see in this PR talks about a belongsTo
relation, should it be changed to belongsToMany
then?
Correct. The description in the test is a typo :)
@chamini2 any comment on this other than the typo in the test name?
Actually, I haven't reviewed it yet! I tried to understand the feature with the spec and since I'm not entirely sure how pivot tables work I can't really review it right now. I was waiting for some time available to understand what's accomplished here! 😄
Are you needing this at the moment? I can take a look for next monday for sure.
No rush on it at all, so please take your time. I'd prioritize the plugin-related tickets before this.
Maybe rebase from master?
This PR adds the ability to automatically serialize pivot/join table data returned by Bookshelf into a relationships'
meta
attribute forbelongsToMany
relations.The serializer already has a
relationshipMeta
option that accepts an object containing a string or function. This PR adds a function that is passed torelationshipMeta
by default that will serialize pivot data into the relationships'meta
attribute. This function can be overridden by passing your own function torelationshipMeta
as an option.A few questions/comments:
relationshipMeta
function with their own method, do we couple this with the option to enable or disable serialization of belongsToMany pivot data to allow chaining of the provided method and the method we provide so they don't have to provide their own function for serializing belongsToMany relations into metadata?toJSON
method were made to add pivot data to the model that's ultimately passed into the function provided torelationshipMeta
. Currently,.serialize({ shallow: true })
will not include pivot data. I looked at the Bookshelf code (https://github.com/tgriesser/bookshelf/blob/9c7b56cb3145930d56c55b07ddf4d36a702e31b3/src/base/model.js#L263) for this and it doesn't appear that there would be an adverse affect for allowing pivot data to be included even when doing a shallow serialization, but for now, I've done the work on our end.