Skip to content

Commit e0d7861

Browse files
committed
new getPrisma function
1 parent 5babe92 commit e0d7861

7 files changed

Lines changed: 30 additions & 47 deletions

File tree

app/(main)/actions.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
"use server";
22

3-
import { PrismaClient } from "@prisma/client";
4-
import { PrismaNeon } from "@prisma/adapter-neon";
5-
import { Pool } from "@neondatabase/serverless";
6-
import { notFound } from "next/navigation";
7-
import Together from "together-ai";
3+
import { getPrisma } from "@/lib/prisma";
84
import {
95
getMainCodingPrompt,
106
screenshotToCodePrompt,
117
softwareArchitectPrompt,
128
} from "@/lib/prompts";
9+
import { notFound } from "next/navigation";
10+
import Together from "together-ai";
1311

1412
export async function createChat(
1513
prompt: string,
1614
model: string,
1715
quality: "high" | "low",
1816
screenshotUrl: string | undefined,
1917
) {
20-
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
21-
const adapter = new PrismaNeon(neon);
22-
const prisma = new PrismaClient({ adapter });
18+
const prisma = getPrisma();
2319
let chat;
2420
try {
2521
chat = await prisma.chat.create({
@@ -195,9 +191,7 @@ export async function createMessage(
195191
text: string,
196192
role: "assistant" | "user",
197193
) {
198-
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
199-
const adapter = new PrismaNeon(neon);
200-
const prisma = new PrismaClient({ adapter });
194+
const prisma = getPrisma();
201195
const chat = await prisma.chat.findUnique({
202196
where: { id: chatId },
203197
include: { messages: true },

app/(main)/chats/[id]/chat-box.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ export default function ChatBox({
4343
<div className="mx-auto mb-5 flex w-full max-w-prose shrink-0 px-8">
4444
<form
4545
className="relative flex w-full"
46-
action={async (formData) => {
46+
action={async () => {
4747
startTransition(async () => {
48-
const prompt = formData.get("prompt");
49-
assert.ok(typeof prompt === "string");
50-
5148
const message = await createMessage(chat.id, prompt, "user");
5249
const streamPromise = fetch(
5350
"/api/get-next-completion-stream-promise",
@@ -66,7 +63,10 @@ export default function ChatBox({
6663
});
6764

6865
onNewStreamPromise(streamPromise);
69-
router.refresh();
66+
startTransition(() => {
67+
router.refresh();
68+
setPrompt("");
69+
});
7070
});
7171
}}
7272
>

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// import client from "@/lib/prisma";
2-
import PageClient from "./page.client";
2+
import { getPrisma } from "@/lib/prisma";
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";
5+
import PageClient from "./page.client";
86

97
export default async function Page({
108
params,
@@ -20,9 +18,7 @@ export default async function Page({
2018
}
2119

2220
const getChatById = cache(async (id: string) => {
23-
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
24-
const adapter = new PrismaNeon(neon);
25-
const prisma = new PrismaClient({ adapter });
21+
const prisma = getPrisma();
2622
return await prisma.chat.findFirst({
2723
where: { id },
2824
include: { messages: { orderBy: { position: "asc" } } },

app/share/[id]/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { notFound } from "next/navigation";
2-
import client from "@/lib/prisma";
32
import type { Metadata } from "next";
43
import { cache } from "react";
54
import CodeRunner from "@/components/code-runner";
5+
import { getPrisma } from "@/lib/prisma";
66

77
/*
88
This is the Share page for v1 apps, before the chat interface was added.
@@ -63,7 +63,8 @@ export default async function Page({
6363
}
6464

6565
const getGeneratedAppByID = cache(async (id: string) => {
66-
return client.generatedApp.findUnique({
66+
const prisma = getPrisma();
67+
return prisma.generatedApp.findUnique({
6768
where: {
6869
id,
6970
},

app/share/v2/[messageId]/_opengraph-image.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @next/next/no-img-element */
2+
import { getPrisma } from "@/lib/prisma";
23
import { ImageResponse } from "next/og";
34
import { readFile } from "node:fs/promises";
45
import { join } from "node:path";
5-
import client from "@/lib/prisma";
66

77
export const size = {
88
width: 1200,
@@ -17,7 +17,8 @@ export default async function Image({
1717
params: { messageId: string };
1818
}) {
1919
let messageId = params.messageId;
20-
let message = await client.message.findUnique({
20+
const prisma = getPrisma();
21+
let message = await prisma.message.findUnique({
2122
where: {
2223
id: messageId,
2324
},

app/share/v2/[messageId]/page.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import CodeRunner from "@/components/code-runner";
2-
import client from "@/lib/prisma";
2+
import { getPrisma } from "@/lib/prisma";
33
import { extractFirstCodeBlock } from "@/lib/utils";
44
import { Metadata } from "next";
55
import { notFound } from "next/navigation";
@@ -41,7 +41,8 @@ export default async function SharePage({
4141
}) {
4242
const { messageId } = await params;
4343

44-
const message = await client.message.findUnique({ where: { id: messageId } });
44+
const prisma = getPrisma();
45+
const message = await prisma.message.findUnique({ where: { id: messageId } });
4546
if (!message) {
4647
notFound();
4748
}
@@ -59,7 +60,8 @@ export default async function SharePage({
5960
}
6061

6162
const getMessage = cache(async (messageId: string) => {
62-
return client.message.findUnique({
63+
const prisma = getPrisma();
64+
return prisma.message.findUnique({
6365
where: {
6466
id: messageId,
6567
},

lib/prisma.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import { PrismaClient } from "@prisma/client";
22
import { PrismaNeon } from "@prisma/adapter-neon";
33
import { Pool } from "@neondatabase/serverless";
4+
import { cache } from "react";
45

5-
declare global {
6-
var prisma: PrismaClient | undefined;
7-
}
8-
9-
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
10-
const adapter = new PrismaNeon(neon);
11-
12-
let client: PrismaClient;
13-
14-
if (process.env.NODE_ENV !== "production") {
15-
client = globalThis.prisma || new PrismaClient({ adapter });
16-
globalThis.prisma = client;
17-
} else {
18-
client = new PrismaClient({ adapter });
19-
}
20-
21-
export default client;
6+
export const getPrisma = cache(() => {
7+
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
8+
const adapter = new PrismaNeon(neon);
9+
return new PrismaClient({ adapter });
10+
});

0 commit comments

Comments
 (0)