So the issue is that with Lucid 4.0 it is now impossible to hide unneeded fields like __meta__ or pivot when requested via manyToMany relation.
Unfortunately, this seems to have no effect:
static get hidden() {
return ['__meta__', 'pivot']
}
See for the initial explanation:
https://forum.adonisjs.com/t/tojson-hide-pivot-but-extract-some-pivot-attrs-as-instance-attrs/299
What I also noted is that with some methods pivot is returned as pivot and with other methods as a __meta__
P. S. Probably, custom serialiser will solve the issue but I still feel like most users will want to exclude meta/pivot fields hence it makes sense to fix the default serializer
hence it makes sense to fix the default serializer
Since it's not a bug, so there is no need of fixing the serializer.
Also framework cannot make assumptions on when you need these field and when you don't. So the options are
@thetutlage Still, it is strange that the docs recommend to use hidden fields for hiding things but hidden fields do not actually remove fields like __meta__ or pivot…
For anyone facing the same issue, here is a simple fix with extending VanillaSerializer:
const VanillaSerializer = require('@adonisjs/lucid/src/Lucid/Serializers/Vanilla')
class GenericSerializer extends VanillaSerializer {
// Taken from https://github.com/adonisjs/adonis-lucid/blob/21d42c0d80e7d98397336d55312bcb284925584c/src/Lucid/Serializers/Vanilla.js
_getRowJSON(modelInstance) {
const json = modelInstance.toObject()
this._attachRelations(modelInstance, json)
this._attachMeta(modelInstance, json)
delete json.__meta__
delete json.pivot
return json
}
}
module.exports = GenericSerializer
@SAGV The visible and hidden are used for the actual attributes. But what's important to know is why I added the layer of serializers.
Everyone will need data one or the other format, some wants to follow pre-defined conventions like JSONAPI and some has their own formats.
Exposing too many parts in the code, will make it hard for everyone. Hard for me to manage and hard to others to find what to use and when.
So idea is, as soon as your first customization need arises, simple create a serializer and do whatever you can. It can be single method serailizer ( the way u did ) or a complete implementation.
Most helpful comment
For anyone facing the same issue, here is a simple fix with extending VanillaSerializer: