admin管理员组

文章数量:1122846

I am developing an application using Next.js 14, Drizzle ORM, and NeonDB/PostgreSQL, and I'm encountering issues when running transaction queries.

Here’s a simplified version of my setup:

Database Initialization (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 });

Drizzle Config (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!,
  },
});

Transaction Query:

const data = await tx.transaction(async (tx) => {
  const productIdResult = await tx.insert(products).values({ type: "music" }).returning({ id: products.id });
  const productId = productIdResult[0].id;
  await tx.insert(music).values({ ...product, id: productId });
  await tx.insert(musicCategories).values(categoryIds);
  return productId;
});

When I run the above transaction query, I get the following error:

No transactions support in neon-http driver

After some research, I found this article, which mentions using Neon’s Pool and Client for transactions. However, I am unsure how to integrate this approach into my setup with Next.js and Drizzle ORM.

Any help or guidance would be greatly appreciated!

Thanks in advance!

I am developing an application using Next.js 14, Drizzle ORM, and NeonDB/PostgreSQL, and I'm encountering issues when running transaction queries.

Here’s a simplified version of my setup:

Database Initialization (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 });

Drizzle Config (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!,
  },
});

Transaction Query:

const data = await tx.transaction(async (tx) => {
  const productIdResult = await tx.insert(products).values({ type: "music" }).returning({ id: products.id });
  const productId = productIdResult[0].id;
  await tx.insert(music).values({ ...product, id: productId });
  await tx.insert(musicCategories).values(categoryIds);
  return productId;
});

When I run the above transaction query, I get the following error:

No transactions support in neon-http driver

After some research, I found this article, which mentions using Neon’s Pool and Client for transactions. However, I am unsure how to integrate this approach into my setup with Next.js and Drizzle ORM.

Any help or guidance would be greatly appreciated!

Thanks in advance!

Share Improve this question edited Nov 23, 2024 at 13:03 Dinesh asked Nov 22, 2024 at 12:28 DineshDinesh 8552 gold badges19 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

update drizzle.ts with the following code

import { Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import * as schema from "./schema";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
export const db = drizzle({ client: pool,schema: schema })

本文标签: postgresqlHow to Handle transaction with Nextjs 14 and Drizzle ORMStack Overflow