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

192
Visualizações
How to insert bulk queries in prisma from real backend?

So I am looking through the Prisma docs and I come across an example to create a relational query. This query inserts a new post and assigns it an author with an existing category.

const assignCategories = await prisma.post.create({
  data: {
    title: 'How to be Bob',
    categories: {
      create: [
        {
          assignedBy: 'Bob',
          assignedAt: new Date(),
          category: {
            connect: {
              id: 9,
            },
          },
        },
        {
          assignedBy: 'Bob',
          assignedAt: new Date(),
          category: {
            connect: {
              id: 22,
            },
          },
        },
      ],
    },
  },
})

I can understand what this query does but I don't understand how to implement this on a backend with the incoming request body.

Suppose I have this request body

{
    "title": "how to be bob",
    "categories": [
        {
            "assignedby": "bob",
            "category": {
                "id": 9
            }
        },
        {
            "assignedby": "bob",
            "category": {
                "id": 22
            }
        }
    ]
}

How do I transform this request body to the data object in the first codeblock?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I got it. It was in my face all along. Just use .map to map through the categories

const data = {
    title: 'how to be bob',
    categories: [
        {
            assignedby: 'bob',
            category: {
                id: 9,
            },
        },
        {
            assignedby: 'bob',
            category: {
                id: 22,
            },
        },
    ],
};


const mappedData = {
    title: data.title,
    categories: {
        create: data.categories.map((i) => ({
            assignedBy: i.assignedby,
            assignedAt: new Date(),
            category: {
                connect: {
                    id: i.category.id,
                },
            },
        })),
    },
};

console.log(mappedData);

which logs this

    "title": "how to be bob",
    "categories": {
        "create": [
            {
                "assignedBy": "bob",
                "assignedAt": "2021-10-24T12:10:00.397Z",
                "category": {
                    "connect": {
                        "id": 9
                    }
                }
            },
            {
                "assignedBy": "bob",
                "assignedAt": "2021-10-24T12:10:00.397Z",
                "category": {
                    "connect": {
                        "id": 22
                    }
                }
            }
        ]
    }
}

Just what we exactly need.

about 4 years ago · Juan Pablo Isaza 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