No transactions support in neon-http driver even though neon provides a transaction function

https://neon.tech/docs/serverless/serverless-driver#when-to-use-the-neon-function-vs-pool-or-client I tried to execute a db operation such as below. I am getting throw new Error('No transactions support in neon-http driver'); I'm not sure if the kind of code below is an "interactive" or "non-interactive" transaction, but wondering if maybe I am missing something. Does drizzle support the neon(...) transaction() function?
await db.transaction(async tx => {
// Insert New Contact
const insertedContact = await tx
.insert(contacts)
.values(newContact)
.returning()
if (newEmails) {
// Take in array of email objects
for (const email of newEmails) {
// Insert or update New Email
const insertedEmail = await tx
.insert(emails)
.values(email)
.onConflictDoUpdate({ target: emails.emailId, set: email })
.returning({ id: emails.emailId })
// For each email, add relationship to join table
await tx.insert(emailsToContacts).values({
contactId: insertedContact[0].contactId,
emailId: insertedEmail[0].id,
})
}
}
await db.transaction(async tx => {
// Insert New Contact
const insertedContact = await tx
.insert(contacts)
.values(newContact)
.returning()
if (newEmails) {
// Take in array of email objects
for (const email of newEmails) {
// Insert or update New Email
const insertedEmail = await tx
.insert(emails)
.values(email)
.onConflictDoUpdate({ target: emails.emailId, set: email })
.returning({ id: emails.emailId })
// For each email, add relationship to join table
await tx.insert(emailsToContacts).values({
contactId: insertedContact[0].contactId,
emailId: insertedEmail[0].id,
})
}
}
Neon
Neon serverless driver - Neon Docs
The Neon serverless driver is a low-latency Postgres driver for JavaScript and TypeScript that allows you to query data from serverless and edge environments over HTTP or WebSockets in place of TCP. Y...
11 Replies
Andrii Sherman
Andrii Sherman15mo ago
It's not supported on a neon side that's why it's not supported in drizzle at least it wasn't when we were adding a support for new neon driver I need to double check their docs, maybe it's supported already
Noahh
Noahh15mo ago
GitHub
GitHub - neondatabase/serverless: Connect to Neon PostgreSQL from s...
Connect to Neon PostgreSQL from serverless/worker/edge functions - GitHub - neondatabase/serverless: Connect to Neon PostgreSQL from serverless/worker/edge functions
Andrii Sherman
Andrii Sherman15mo ago
oh, nice, then we should add a support for it
NinjaBunny
NinjaBunny15mo ago
I thought neon only allowed transactions while using the serverless driver + a web socket connection let me find it
NinjaBunny
NinjaBunny15mo ago
Neon
How do I handle transactions?
I am having this problem too, I thought they would have support for transactions for all postgres db’s…
DiamondDragon
DiamondDragonOP15mo ago
When to use the neon function vs Pool or Client The Neon serverless driver supports the neon function for queries over HTTP, and Pool and Client constructors for querying over WebSockets. The neon function Querying over an HTTP fetch request is faster for single, non-interactive transactions. If you do not require sessions or transactions, consider using HTTP for faster response times. ... The neon(...) function also supports issuing multiple queries at once in a single, non-interactive transaction using the transaction() function, which is exposed as a property of the query function. For example:
Andrii Sherman
Andrii Sherman15mo ago
oh yeah than it's the same as it was becuase we have 2 drivers for neon neon-serverless and neon-http
DiamondDragon
DiamondDragonOP15mo ago
ah, i didn't understand if transactions in drizzle were "interactive" or "non-interactive" but it sounds like with rollbacks/savepoints this means they are interactive, thus no tx in the http driver. Sounds like tx could be supported in http driver only if non-interactive but not sure the use cases for non-interactive tx's
outerhell
outerhell4mo ago
Hello, I'm using drizzle neon-http driver and I was trying to create a transaction to do a many2many insert but it's not permitted. If I do the following it works
import { neon } from "@neondatabase/serverless";
const sql = neon(connectionStrPool.href);
await sql.transaction((tx) => [
tx`
INSERT INTO video (id, author_name, author_url, thumbnail_height, thumbnail_url, thumbnail_width, title, url)
VALUES (${videoId}, ${videoData.author_name}, ${videoData.author_url}, ${videoData.thumbnail_height}, ${videoData.thumbnail_url}, ${videoData.thumbnail_width}, ${videoData.title}, ${videoUrl})
ON CONFLICT DO NOTHING
`,
tx`
INSERT INTO user_video ("userId", "videoId")
VALUES (${userId}, ${videoId})
ON CONFLICT DO NOTHING
RETURNING "videoId"
`,
]);
import { neon } from "@neondatabase/serverless";
const sql = neon(connectionStrPool.href);
await sql.transaction((tx) => [
tx`
INSERT INTO video (id, author_name, author_url, thumbnail_height, thumbnail_url, thumbnail_width, title, url)
VALUES (${videoId}, ${videoData.author_name}, ${videoData.author_url}, ${videoData.thumbnail_height}, ${videoData.thumbnail_url}, ${videoData.thumbnail_width}, ${videoData.title}, ${videoUrl})
ON CONFLICT DO NOTHING
`,
tx`
INSERT INTO user_video ("userId", "videoId")
VALUES (${userId}, ${videoId})
ON CONFLICT DO NOTHING
RETURNING "videoId"
`,
]);
non-interactive transactions do the job, if the second insert fails nothing is added in the first insert. Is it supported by drizzle?
Want results from more Discord servers?
Add your server