CherryPick - remove relationships
We have a way to pick or omit relationship fields before serialization using CherryPick:
const post = await Post
.query()
.preload('comments')
.preload('category')
.preload('author')
.first()
post.serialize({
fields: {
pick: ['id', 'title', 'body'],
},
relations: {
comments: {
fields: ['id', 'body'],
},
author: {
fields: ['id', 'email', 'avatar_url'],
},
}
})
Would it be possible to add feature to completely remove specified relationships? It could be set up like this:
post.serialize({
fields: {
pick: ['id', 'title', 'body'],
},
relations: {
comments: null
author: null,
}
})
Package version
@adonisjs/lucid": "^17.2.0
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hey @enixsoft! 👋🏻
Why would you preload a relationship to remove it afterward in the serialization process?
Hey @RomainLanz
For an example I have translatable model with some translated properties in many languages and those translations are saved in another table and loaded as a relationship. So I preload translations, use them to translate computed properties on the model. If I am sending the model to regular user, I do not need the relationship's array of translations - they see just the translated properties. But I need the translations if I am sending it to administrator who can edit them so I can't just disable the relationship's serialization completely on the model.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi! If it make difference, i also want this feature)
Now i use this:
relations: {
file: {
fields: [],
},
},
but it makes an empty file: {} field on serialization.