-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically serialize belongsToMany into relation meta attribute #74
base: master
Are you sure you want to change the base?
Conversation
Another to point to mention: Currently, the attributes returned as part of meta are not affected by the serializer's |
@chamini2 to clarify, the PR allows for pivot data (data contained in join tables) to be automatically serialized into Imagine that I have a three tables: |
So it's for |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add documentation for the new option
template[relName] = relTemplate; | ||
template.attributes.push(relName); | ||
}); | ||
|
||
return template; | ||
} | ||
|
||
function relationshipMeta(relation, models) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not declare this relationshipMeta
function here, because it is just the default function to use in case none was passed.
I would declare it where it is being used as a default.
/** | ||
* Recursively adds data-related properties to the | ||
* template to be sent to the serializer | ||
*/ | ||
function processSample(info: Information, sample: Model): SerialOpts { | ||
let { bookOpts, linkOpts }: Information = info; | ||
let { bookOpts, linkOpts, serialOpts }: Information = info; | ||
let { enableLinks }: BookOpts = bookOpts; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding it here, like:
const defaultRelationshipMeta = function (relation, models) {
if (isArray(models)) { ... }
}
const { relationshipMeta = defaultRelationshipMeta } = seralOpts;
expect(_.matches(expected)(result)).toBe(true); | ||
|
||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for the attribute omission done
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.