Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!