Skip to content

Commit 5babe92

Browse files
committed
create prisma at request time
1 parent fe134fe commit 5babe92

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

app/(main)/actions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use server";
22

3-
import prisma from "@/lib/prisma";
3+
import { PrismaClient } from "@prisma/client";
4+
import { PrismaNeon } from "@prisma/adapter-neon";
5+
import { Pool } from "@neondatabase/serverless";
46
import { notFound } from "next/navigation";
57
import Together from "together-ai";
68
import {
@@ -15,7 +17,9 @@ export async function createChat(
1517
quality: "high" | "low",
1618
screenshotUrl: string | undefined,
1719
) {
18-
console.log("--- createChat");
20+
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
21+
const adapter = new PrismaNeon(neon);
22+
const prisma = new PrismaClient({ adapter });
1923
let chat;
2024
try {
2125
chat = await prisma.chat.create({
@@ -191,6 +195,9 @@ export async function createMessage(
191195
text: string,
192196
role: "assistant" | "user",
193197
) {
198+
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
199+
const adapter = new PrismaNeon(neon);
200+
const prisma = new PrismaClient({ adapter });
194201
const chat = await prisma.chat.findUnique({
195202
where: { id: chatId },
196203
include: { messages: true },

app/(main)/chats/[id]/page.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import client from "@/lib/prisma";
1+
// import client from "@/lib/prisma";
22
import PageClient from "./page.client";
33
import { notFound } from "next/navigation";
44
import { cache } from "react";
5+
import { PrismaClient } from "@prisma/client";
6+
import { PrismaNeon } from "@prisma/adapter-neon";
7+
import { Pool } from "@neondatabase/serverless";
58

69
export default async function Page({
710
params,
@@ -17,7 +20,10 @@ export default async function Page({
1720
}
1821

1922
const getChatById = cache(async (id: string) => {
20-
return await client.chat.findFirst({
23+
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
24+
const adapter = new PrismaNeon(neon);
25+
const prisma = new PrismaClient({ adapter });
26+
return await prisma.chat.findFirst({
2127
where: { id },
2228
include: { messages: { orderBy: { position: "asc" } } },
2329
});

app/api/get-next-completion-stream-promise/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import prisma from "@/lib/prisma";
1+
import { PrismaClient } from "@prisma/client";
2+
import { PrismaNeon } from "@prisma/adapter-neon";
3+
import { Pool } from "@neondatabase/serverless";
24
import { z } from "zod";
35
import Together from "together-ai";
46

57
export async function POST(req: Request) {
8+
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
9+
const adapter = new PrismaNeon(neon);
10+
const prisma = new PrismaClient({ adapter });
611
const { messageId, model } = await req.json();
712

813
const message = await prisma.message.findUnique({

lib/prisma.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const adapter = new PrismaNeon(neon);
1111

1212
let client: PrismaClient;
1313

14-
console.log("NODE_ENV IS: ");
15-
console.log(process.env.NODE_ENV);
1614
if (process.env.NODE_ENV !== "production") {
1715
client = globalThis.prisma || new PrismaClient({ adapter });
1816
globalThis.prisma = client;

0 commit comments

Comments
 (0)