How to Handle transaction with Nextjs 14 and Drizzle ORM
I am developing a app with nextjs14, drizzle orm & neonDB/postgress and having issue with transaction quries
here is my code
src/db/drizzle.ts
import { config } from "dotenv";
import { drizzle } from 'drizzle-orm/neon-http';
import * as schema from "./schema";
config({ path: ".env" }); // or .env.local
export const db = drizzle(process.env.DATABASE_URL!,{ schema: schema });
drizzle.config.ts
import { config } from 'dotenv';
import { defineConfig } from "drizzle-kit";
config({ path: '.env' });
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
Truncation query
const data = await db.transaction(async (tx) => {
const productId = await tx.insert(products).values({type:"music"}).returning({id:products.id});
await tx.insert(music).values({ ...product, id: productId });
await db.insert(musicCategories).values(categoryIds);
});
when I run a query got this error : No transactions support in neon-http driver
with some search found this article https://github.com/neondatabase/serverless#pool-and-client
but no idea how to implement with nextsj & drizzle
Thanks in advance for help
GitHub
GitHub - neondatabase/serverless: Connect to Neon PostgreSQL from s...
Connect to Neon PostgreSQL from serverless/worker/edge functions - neondatabase/serverless
1 Reply
stormy-gold•12mo ago
@dinesh
Can you share a minimal reproducible?
I am trying out the transactions meanwhile using https://github.com/neondatabase/examples/blob/main/with-nextjs-drizzle-edge/lib/drizzle.server.ts as the reference.
GitHub
examples/with-nextjs-drizzle-edge/lib/drizzle.server.ts at main · n...
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples