You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
{
"name": "Bob"
}
But if isGuest was not present or false:
{
"name": "Bob",
"secret": "123"
}
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
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:
It would be cool to have some way to pass options to toJSON that would get passed to the whole tree of relations, but your suggestion wouldn't work for couple of reasons.
$omitFromJson is private. You shouldn't be using it. Everything that's not mentioned in the documentation is considered private and can change at any given time. The function may be removed or renamed in any patch version.
$omitFromJson is a getter/setter. Passing values to $omitFromJson sets the omitted values.
Your suggestion would only solve your specific problem, nothing else. It would only allow omitting values based on some options. Not very useful in general. Adding any feature needs to be tested, documented and maintained. Any added feature needs to be generally useful.
We could add a second argument for $formatJson(json, options). Would that work for you?
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: