Replace Supabase with Prisma/PostgreSQL on DO

This commit is contained in:
Hanzo Dev
2026-01-23 17:51:50 -08:00
parent 46d7b3205e
commit eb17e91dc2
5 changed files with 722 additions and 403 deletions
+74 -71
View File
@@ -1,112 +1,115 @@
'use server'
import 'server-only'
import { createServerActionClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { Database } from '@/lib/db_types'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { getDocs, where, query } from "firebase/firestore"
import { prisma } from '@/lib/db'
import { type Chats } from '@/lib/types'
import { docRef, db } from '@/firebase/firebase'
export async function getChats(userId?: string | null) {
if (!userId) {
return []
}
try {
const cookieStore = cookies()
const supabase = createServerActionClient<Database>({
cookies: () => cookieStore
const chats = await prisma.chat.findMany({
where: { userId },
orderBy: { createdAt: 'desc' },
select: {
id: true,
title: true,
messages: true,
sharePath: true,
createdAt: true,
userId: true
}
})
const { data } = await supabase
.from('chats')
.select('payload')
.order('payload->createdAt', { ascending: false })
.eq('user_id', userId)
.throwOnError()
return (data?.map((entry: any) => entry.payload) as Chats[]) ?? []
return chats as unknown as Chats[]
} catch (error) {
console.error('Error fetching chats:', error)
return []
}
}
export const getChat = async (id: string) => {
const q = query(docRef, where('user_id', '==', id))
const snaps = await getDocs(q)
let result = null;
result = await getDocs(docRef);
return result?.docs[0]
export async function getChat(id: string, userId?: string) {
try {
const chat = await prisma.chat.findFirst({
where: { id, ...(userId ? { userId } : {}) }
})
return chat as unknown as Chats | null
} catch (error) {
console.error('Error fetching chat:', error)
return null
}
}
export async function removeChat({ id, path }: { id: string; path: string }) {
try {
const cookieStore = cookies()
const supabase = createServerActionClient<Database>({
cookies: () => cookieStore
})
await supabase.from('chats').delete().eq('id', id).throwOnError()
await prisma.chat.delete({ where: { id } })
revalidatePath('/')
return revalidatePath(path)
} catch (error) {
return {
error: 'Unauthorized'
}
return { error: 'Unauthorized' }
}
}
export async function clearChats() {
export async function clearChats(userId: string) {
try {
const cookieStore = cookies()
const supabase = createServerActionClient<Database>({
cookies: () => cookieStore
})
await supabase.from('chats').delete().throwOnError()
await prisma.chat.deleteMany({ where: { userId } })
revalidatePath('/')
return redirect('/')
} catch (error) {
console.log('clear chats error', error)
return {
error: 'Unauthorized'
}
return { error: 'Unauthorized' }
}
}
export async function getSharedChat(id: string) {
const cookieStore = cookies()
const supabase = createServerActionClient<Database>({
cookies: () => cookieStore
})
const { data } = await supabase
.from('chats')
.select('payload')
.eq('id', id)
.not('payload->sharePath', 'is', null)
.maybeSingle()
return ((data as any)?.payload as Chats) ?? null
try {
const chat = await prisma.chat.findFirst({
where: {
id,
sharePath: { not: null }
}
})
return chat as unknown as Chats | null
} catch (error) {
console.error('Error fetching shared chat:', error)
return null
}
}
export async function shareChat(chat: Chats) {
const payload = {
...chat,
sharePath: `/share/${chat.id}`
const sharePath = `/share/${chat.id}`
try {
await prisma.chat.update({
where: { id: chat.id },
data: { sharePath }
})
return { ...chat, sharePath }
} catch (error) {
console.error('Error sharing chat:', error)
return chat
}
}
export async function saveChat(chat: Chats) {
try {
await prisma.chat.upsert({
where: { id: chat.id },
update: {
title: chat.title,
messages: chat.messages as any,
sharePath: chat.sharePath
},
create: {
id: chat.id,
title: chat.title,
userId: chat.userId,
messages: chat.messages as any,
sharePath: chat.sharePath
}
})
} catch (error) {
console.error('Error saving chat:', error)
}
const cookieStore = cookies()
const supabase = createServerActionClient<Database>({
cookies: () => cookieStore
})
await supabase
.from('chats')
.update({ payload: payload as any })
.eq('id', chat.id)
.throwOnError()
return payload
}
+9
View File
@@ -0,0 +1,9 @@
import { PrismaClient } from '@prisma/client'
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
+2 -5
View File
@@ -18,6 +18,7 @@
"@emotion/styled": "^11.11.0",
"@google/model-viewer": "1.10.1",
"@mui/material": "^5.14.7",
"@prisma/client": "^7.3.0",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.5",
@@ -29,10 +30,6 @@
"@radix-ui/react-tooltip": "^1.0.6",
"@stripe/react-stripe-js": "^2.1.1",
"@stripe/stripe-js": "^1.54.0",
"@supabase/auth-helpers-nextjs": "^0.7.2",
"@supabase/auth-helpers-react": "^0.4.2",
"@supabase/gotrue-js": "^2.57.0",
"@supabase/supabase-js": "^2.26.0",
"@vercel/analytics": "^1.0.1",
"@vercel/og": "^0.5.7",
"ai": "^2.1.6",
@@ -80,7 +77,7 @@
"eslint-plugin-tailwindcss": "^3.12.0",
"postcss": "^8.4.23",
"prettier": "^2.8.8",
"supabase": "^1.150.0",
"prisma": "^7.3.0",
"tailwind-merge": "^1.12.0",
"tailwindcss": "^3.3.2",
"tailwindcss-animate": "^1.0.5",
+606 -327
View File
File diff suppressed because it is too large Load Diff
+31
View File
@@ -0,0 +1,31 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
chats Chat[]
}
model Chat {
id String @id @default(cuid())
title String?
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
messages Json @default("[]")
sharePath String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
}