Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda