PrismaP
Prisma14mo ago
4 replies
Elias

"upsertMany" with transaction timing out

I'm trying to build a database containing stock for different products from our suppliers. The stock can be fetched as a csv file from an FTP server. What I want to do is to periodically (every day) run a script that gets the csv file, parses it, and insert the values to a database. It should also save the previous history of the stock.

This is the code that I have currently.

await client
.$transaction(
  async (tx) =>
    await Promise.all(
      records.map(([reference, quantity, _]) =>
        tx.part.upsert({
          where: { reference },
          update: {
            quantity: parseInt(quantity),
            history: {
              create: { date: file.date, quantity: parseInt(quantity) },
            },
          },
          create: {
            reference,
            quantity: parseInt(quantity),
            supplier: { connect: { id: info.id } },
            history: {
              create: { date: file.date, quantity: parseInt(quantity) },
            },
          },
        })
      )
    ),
  {
    timeout: 300000,
  }
)
.catch((err) => {
  console.error(err)
})


The csv contains almost 5000 products/entries. This times the transaction query out.

Is there a better and more efficient way of doing this?
Solution
I switched to postgresql and now everything works as intended and a lot quicker!
Was this page helpful?