Skip to content

Vercel Postgres, no transactions?

New Course Coming Soon:

Get Really Good at Git

I had problems using Kysely on Vercel Postgres, with the @vercel/postgres and @vercel/postgres-kysely adapters.

Basically, transactions are not supported.

I had some code like this:

const data = await db.transaction().execute(async (trx) => {
  const somedata = await trx
    .selectFrom('table1')
    .selectAll()
    .where('id', '=', token)
    .executeTakeFirst()

  if (!somedata) throw new Error('Invalid data')

  await trx
    .deleteFrom('table2')
    .where('id', '=', token)
    .executeTakeFirst()

  return somedata
})

console.log(data)

and I had to remove the transaction, I just used the queries without the transaction:

const data = await db.selectFrom('table1')
  .selectAll()
  .where('id', '=', token)
  .executeTakeFirst()

if (!data) throw new Error('Invalid data')

await db.deleteFrom('table2')
  .where('id', '=', token)
  .executeTakeFirst()

console.log(data)

Hopefully transactions will be implemented.

If you absolutely need them you can still use a direct connection to the database using pg

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Read my SQL Tutorial on The Valley of Code

Here is how can I help you: