-
Notifications
You must be signed in to change notification settings - Fork 639
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
Ability to pass custom options to JSON serialization that will be also be passed to children/relations #1374
Comments
Whoops, I missed the "passing to children" part. |
Yes I see that, but I any custom options I provide to super.toJSON will not pass those options to its children/relations. Which is what I need. |
It would be cool to have some way to pass options to
We could add a second argument for |
Yes I think |
Often I find myself needing to alter the JSON results of serialization.
For example:
Perhaps I have a model 'ModelA' with attributes 'name' and 'secret'
I would like to be able to alter the results of toJSON by providing some options:
myModel.toJSON({ isGuest: true })
would yield
But if isGuest was not present or false:
Looking through the API and code, I can't find a proper way to do this. I'm aware of $omitFromJson and objection-visibility, but those don't allow you to provide custom options. I also need this option to propagate down to all children/relations.
Am I missing something?
A relatively simple solution I came up with is like so (I could make a PR):
in objection.js/lib/model/modelToJson.js
I simply allow a 'custom' option to be passed into toJSON, and passed along to $omitFromJson
I can then do something like:
Alternatively, this custom option could be passed to $formatJson instead (as a second, optional parameter).
Let me know if this makes sense. I could make a PR that could flexibly handle most inputs to 'custom'. With the exception of promises, which I think should be left out of the serialization flow.
The text was updated successfully, but these errors were encountered: