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 badges1 Answer
Reset to default 0update 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
版权声明:本文标题:postgresql - How to Handle transaction with Nextjs 14 and Drizzle ORM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303788a1932004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论