N
Neon13mo ago
like-gold

Conditional batch on Neon Batch API?

Hi, it's possible to do something like:
const deleteImages = true // can be true or false

await db.batch([
db.update(users).set({ ... }).where(...),
deleteImages ? db.delete(userImages).where(...) : false
])
const deleteImages = true // can be true or false

await db.batch([
db.update(users).set({ ... }).where(...),
deleteImages ? db.delete(userImages).where(...) : false
])
4 Replies
foreign-sapphire
foreign-sapphire13mo ago
There's a transaction API for the HTTP version fo the driver https://github.com/neondatabase/serverless?tab=readme-ov-file#transaction
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);
const showLatestN = 10;

const [posts, tags] = await sql.transaction([
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
sql`SELECT * FROM tags`,
]);
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);
const showLatestN = 10;

const [posts, tags] = await sql.transaction([
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
sql`SELECT * FROM tags`,
]);
GitHub
GitHub - neondatabase/serverless: Connect to Neon PostgreSQL from s...
Connect to Neon PostgreSQL from serverless/worker/edge functions - neondatabase/serverless
fascinating-indigo
fascinating-indigo13mo ago
I don't think that's what they are asking for 😄 It's more of a javascript question really, how you could conditionally do
[
db.update(users).set({ ... }).where(...),
]
[
db.update(users).set({ ... }).where(...),
]
or
[
db.update(users).set({ ... }).where(...),
db.delete(userImages).where(...),
]
[
db.update(users).set({ ... }).where(...),
db.delete(userImages).where(...),
]
foreign-sapphire
foreign-sapphire13mo ago
oh TIL about this Drizzle API https://orm.drizzle.team/docs/batch-api
Drizzle ORM - Batch
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
foreign-sapphire
foreign-sapphire13mo ago
I tried it out but I get a type error
No description

Did you find this page helpful?