Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

978
Visualizações
How to map with the help of Automapper (TypeScript) the entries of an array filled with different DTOs?

I'm using the TypeScript library "Automapper" in my NestJS backend to convert an answer of MongoDb/Mongoose to an DTO. That's how the answer looks like:

{
    "_id": "mongo-object-id",
    "widgets": [
      {
        "id": "UUID",
        "type": "IMAGE",
        "zIndex": "0",
        "position": { "x": 1, "y": 2 },
        "pinned": false,
        "height":500,
        "width":1000
      },
      {
        "id": "UUID",
        "type": "CONVERSATION",
        "zIndex": "1",
        "position": { "x": 3, "y": 7 },
        "pinned": false,
        "participants": ["UUID1","UUID2"]
      }
    ]
  }

My problem is, that I don't know how to convert the entries of the "widgets" array to different DTOs (ImageDTO, ConversationDTO) based on the "type" attribute/discriminator.

How can I solve that?

Currently I use the following:

mapper.createMap(MeetingSpace, MeetingSpaceDto).forMember(
        (destination) => destination.widgets,
        mapWith(WidgetDto, Widget, (source) => source.widgets)
      );

But I have to find a way to map a "Widget" to an "ImageDto" or "ConversationDto" because of the "type".

mapWith(ImageDto, Widget, (source) => source.widgets)
mapWith(ConversationDto, Widget, (source) => source.widgets)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I know this is a bit late - but I just came across the same need.

You need to define the mapping from Widget to ImageDTO

createMap(mapper, Widget, ImageDTO);

Then use the map function on the collection:

return source.widgets.map(widget => mapper.map(widget, Widget, ImageDTO);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda