lucid icon indicating copy to clipboard operation
lucid copied to clipboard

CherryPick - remove relationships

Open enixsoft opened this issue 3 years ago • 4 comments

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

enixsoft avatar May 25 '22 12:05 enixsoft

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.

stale[bot] avatar Jul 30 '22 16:07 stale[bot]

Hey @enixsoft! 👋🏻

Why would you preload a relationship to remove it afterward in the serialization process?

RomainLanz avatar Aug 01 '22 09:08 RomainLanz

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.

enixsoft avatar Aug 05 '22 11:08 enixsoft

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.

stale[bot] avatar Oct 12 '22 10:10 stale[bot]

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.

ivan-suhorukov avatar Apr 24 '24 13:04 ivan-suhorukov