Lucid: Impossible to hide `__meta__` and `pivot` in with static get hidden() method

Created on 6 Nov 2017  Â·  4Comments  Â·  Source: adonisjs/lucid

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

Most helpful comment

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

All 4 comments

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

  1. Custom serializer that extends the Vanilla Serializer
  2. Manually remove it from the payload, before returning it to the end user.

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

enniel picture enniel  Â·  7Comments

hailwood picture hailwood  Â·  5Comments

watzon picture watzon  Â·  3Comments

HigoRibeiro picture HigoRibeiro  Â·  3Comments

italoiz picture italoiz  Â·  6Comments