added prisma schema, db and initial profile
This commit is contained in:
parent
db7c2350c5
commit
c7b61d05cb
26
app/(setup)/page.tsx
Normal file
26
app/(setup)/page.tsx
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { initialProfile } from "@/lib/initial-profile";
|
||||||
|
|
||||||
|
const SetupPage = async () => {
|
||||||
|
const profile = await initialProfile();
|
||||||
|
|
||||||
|
const server = await db.server.findFirst({
|
||||||
|
where: {
|
||||||
|
members: {
|
||||||
|
some: {
|
||||||
|
profileId: profile.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (server) {
|
||||||
|
return redirect(`/servers/${server.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div>Create a Server</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SetupPage;
|
9
lib/db.ts
Normal file
9
lib/db.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
var prisma: PrismaClient | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const db = globalThis.prisma || new PrismaClient()
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'production') globalThis.prisma = db
|
32
lib/initial-profile.ts
Normal file
32
lib/initial-profile.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { currentUser, redirectToSignIn } from "@clerk/nextjs";
|
||||||
|
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
|
||||||
|
export const initialProfile = async () => {
|
||||||
|
const user = await currentUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return redirectToSignIn();
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await db.profile.findUnique({
|
||||||
|
where: {
|
||||||
|
userId: user.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (profile) {
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newProfile = await db.profile.create({
|
||||||
|
data: {
|
||||||
|
userId: user.id,
|
||||||
|
name: `${user.firstName} ${user.lastName}`,
|
||||||
|
imageUrl : user.imageUrl,
|
||||||
|
email: user.emailAddresses[0].emailAddress,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return newProfile;
|
||||||
|
}
|
89
prisma/schema.prisma
Normal file
89
prisma/schema.prisma
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "mysql"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
relationMode = "prisma"
|
||||||
|
}
|
||||||
|
|
||||||
|
model Profile{
|
||||||
|
id String @id @default(uuid())
|
||||||
|
userId String @unique
|
||||||
|
name String
|
||||||
|
imageUrl String @db.Text
|
||||||
|
email String @db.Text
|
||||||
|
|
||||||
|
servers Server[]
|
||||||
|
members Member[]
|
||||||
|
channels Channel[]
|
||||||
|
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
model Server{
|
||||||
|
id String @id @default(uuid())
|
||||||
|
name String
|
||||||
|
imageUrl String @db.Text
|
||||||
|
inviteCode String @db.Text
|
||||||
|
|
||||||
|
profileId String
|
||||||
|
profile Profile @relation(fields: [profileId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
members Member[]
|
||||||
|
channels Channel[]
|
||||||
|
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
@@index([profileId])
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MemberRole{
|
||||||
|
ADMIN
|
||||||
|
MODERATOR
|
||||||
|
GUEST
|
||||||
|
}
|
||||||
|
|
||||||
|
model Member{
|
||||||
|
id String @id @default(uuid())
|
||||||
|
role MemberRole @default(GUEST)
|
||||||
|
|
||||||
|
profileId String
|
||||||
|
profile Profile @relation(fields: [profileId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
serverId String
|
||||||
|
server Server @relation(fields: [serverId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
@@index([profileId])
|
||||||
|
@@index([serverId])
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ChannelType{
|
||||||
|
TEXT
|
||||||
|
AUDIO
|
||||||
|
VIDEO
|
||||||
|
}
|
||||||
|
|
||||||
|
model Channel{
|
||||||
|
id String @id @default(uuid())
|
||||||
|
name String
|
||||||
|
type ChannelType @default(TEXT)
|
||||||
|
|
||||||
|
profileId String
|
||||||
|
profile Profile @relation(fields: [profileId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
serverId String
|
||||||
|
server Server @relation(fields: [serverId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
@@index([profileId])
|
||||||
|
@@index([serverId])
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user