How do I handle transactions?

Hello! I am hoping to use transactions to handle the upload of a large JSON dataset. I am using the"drizzle-orm/neon-http" package as well as the serverless driver “@neondatabase/serverless” inside of a nextjs app.

Currently I’m running into an error: “Transaction failed: Error: No transactions support in neon-http driver”.

Since transactions are not supported in the neon-http driver, I am looking for alternative solutions, but I’m having trouble finding documentation about alternatives.

I’m hoping to understand - in my nextjs api routes, do I have to use the serverless driver? If I don’t have to and there is an alternative, what are the downsides? Is there any other way for me to handle a large file upload (converting a large JSON file into rows in a db)? Thanks.

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const rawData = body.data;
    const projectId = body.projectId;

    const chunkSize = 1000; // You can adjust the chunk size as needed

    await db.transaction(async (tx) => {
      for (let i = 0; i < data.length; i += chunkSize) {
        console.log(
          `Processing chunk ${i / chunkSize + 1} of ${data.length / chunkSize}`
        );
        const chunkData = data.slice(i, i + chunkSize);
        const preparedChunkData = chunkData.map((item: any, index: number) => ({
          ...item,
          projectId: projectId,
        }));

        await tx.insert(examplesTable).values(preparedChunkData);
      }
    });

    return new Response(
      `Uploaded ${data.length} examples successfully. ${duplicatesCount} duplicates were removed.`,
      { status: 200 }
    );
  } catch (error) {
    console.error("Transaction failed:", error);
    return new Response("An error occurred during the upload", { status: 500 });
  }
}

I am having this problem too, I thought they would have support for transactions for all postgres db’s…

Hey @williamlmao @Kshitij_Mohan , apologies for the delayed response. If you would like to use transactions, you will need to establish a connection over WebSocket, so you would import drizzle-orm/neon-serverless instead and create a Pool or Client instance. You can check out the README for more details

1 Like