title: Map over collection to upsert into the database. How to batch upsert?
body: <p>Say, I have a data structure coming in from the frontend as follows:</p>
  <pre><code>const userData = [
    {
      id: 11223,
      bb: [
        {
          id: 12,
        },
        {
          id: 34,
          bbb: &quot;bbb&quot;,
        },
      ],
    },
    {
      id:4234,
      ...
    },
  ];
  </code></pre>
  <p>Because, none/ some/ all of the data may already be in the database, here is what I have come up with:</p>
  <pre><code>const collection = [];
  for (let i = 0; i &lt; userData.length; i++) {
    const cur = userData[i];
    const subCur = cur.bb;
    const updatedCur = await db.cur.upsert({
        where: {
          id : cur.id
        },
        update: {
          ...
        },
        create: {
          ...
        },
      })
    );
    collection.push(updatedCur);
    for (let j = 0; j &lt; subCur.length; j++) {
      const latest = subCur[j];
      await db.subcur.upsert({
        where: {
          id : latest.id
        },
        update: {
          ...
        },
        create: {
          ...
        },
      });
    }
  }
  </code></pre>
  <p>To summarise, I am mapping over the userData &amp; <code>upsert</code> each object one by one. Within the loop, I map over the child collections &amp; <code>upsert</code> them in the db.</p>
  <p>My concern is that I am making a lot of entries into the Db this way. Is this the best way to do this?</p>
  <p>Aside:
  I previously, tried to do multiple <code>inserts</code> within the <code>upsert</code>, however, I got stuck with the <code>update</code> section as to my knowledge, we cannot upsert multiple records within the <code>update</code> nested within <code>upsert</code>. Is this correct?</p>
  <p><strong>UPDATE:</strong></p>
  <p>As requested by Ryan, here is what the Schema looks like:</p>
  <pre><code>model Cur {
    id      Int,
    subCur  SubCur[]
    ...
  }
  
  model SubCur {
    id      Int,
    cur     Cur  @relation(fields: [curId], references : [id])
    curId   Int
    ...
  }
  </code></pre>
  <p>To summarise, there are many models like 'SubCur' with 1-n relation with 'Cur' model. As the 'UserData' payload, may have some data that is new, some that is update for existing data already in Db, I was curious, whats the best approach to <code>upsert</code> the data into the db. To be specific, do I have to insert each one, one at a time?</p>