Skip to content

Commit bdbc6eb

Browse files
added migrations and seed script (#83)
1 parent 76e345b commit bdbc6eb

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed

.env.local.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ NEXT_PUBLIC_LIVEKIT_URL=wss://discord-clonelink.....
1010
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
1111
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
1212
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/
13-
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
13+
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
14+
SUPABASE_ACCESS_TOKEN=supabaseaccesstoken
15+
SUPABASE_DB_PASSWORD=supabasedbpassword

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"lint": "next lint"
9+
"lint": "next lint",
10+
"db:seed": "tsc seed.ts && node seed.js"
1011
},
1112
"dependencies": {
1213
"@clerk/nextjs": "^4.25.4",
@@ -59,9 +60,11 @@
5960
"@types/react-dom": "^18",
6061
"@types/uuid": "^9.0.5",
6162
"autoprefixer": "^10",
63+
"dotenv": "^16.4.5",
6264
"eslint": "^8",
6365
"eslint-config-next": "13.5.4",
6466
"postcss": "^8",
67+
"supabase": "^1.169.8",
6568
"tailwindcss": "^3",
6669
"typescript": "^5"
6770
}

seed.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
require('dotenv').config({ path: `.env.local` })
3+
4+
5+
const supabase = createClient(
6+
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
7+
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ""
8+
);
9+
10+
11+
12+
async function seedDataBase() {
13+
try {
14+
const profile = await supabase
15+
.from('Profile')
16+
.insert([{
17+
userId: `user_1`,
18+
name: `Dummy User`,
19+
imgUrl: "https://utfs.io/f/684fd07f-6421-4d5c-8ab4-4bc16c7e3cea-tpuwo3.jpg",
20+
21+
}])
22+
.select("id");
23+
24+
const server = await supabase
25+
.from('Server')
26+
.insert([
27+
{
28+
name: 'Sample Server',
29+
imgUrl: 'https://utfs.io/f/0686af8c-8c20-423d-a273-79e07239dac7-7v9i42.avif',
30+
inviteCode: 'invite_1',
31+
profileId: profile.data![0].id
32+
}
33+
34+
]).select("id");
35+
36+
const channel = await supabase
37+
.from('Channel')
38+
.insert([
39+
{
40+
name: 'Sample Channel',
41+
type: "TEXT",
42+
serverId: server.data![0].id,
43+
profileId: profile.data![0].id
44+
}
45+
]);
46+
47+
const memeber = await supabase.from('Member')
48+
.insert([{
49+
role: "ADMIN",
50+
profileId: profile.data![0].id,
51+
serverId: server.data![0].id
52+
}]);
53+
54+
55+
console.log("Database Seeded successfully");
56+
} catch (err) {
57+
console.error('Unexpected error:', err);
58+
}
59+
}
60+
seedDataBase()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
CREATE TYPE "channeltype" AS ENUM ('TEXT', 'AUDIO', 'VIDEO');
2+
CREATE TYPE "memberrole" AS ENUM ('MODERATOR', 'GUEST', 'ADMIN');
3+
4+
CREATE TABLE
5+
public."Profile" (
6+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
7+
"userId" TEXT,
8+
"name" TEXT,
9+
"imgUrl" TEXT,
10+
"email" TEXT,
11+
"createdAt" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
12+
"updatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
13+
);
14+
15+
16+
CREATE TABLE
17+
public."Server" (
18+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
19+
"name" TEXT,
20+
"imgUrl" TEXT,
21+
"inviteCode" TEXT,
22+
"profileId" TEXT,
23+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
24+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
25+
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id)
26+
);
27+
28+
CREATE TABLE
29+
"Channel" (
30+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
31+
"name" text,
32+
"type" "channeltype",
33+
"serverId" text,
34+
"profileId" text,
35+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
36+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
37+
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id),
38+
FOREIGN KEY ("serverId") REFERENCES public."Server" (id)
39+
);
40+
41+
CREATE TABLE
42+
"Member" (
43+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
44+
"role" "memberrole",
45+
"serverId" text,
46+
"profileId" text,
47+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
48+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
49+
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id),
50+
FOREIGN KEY ("serverId") REFERENCES public."Server" (id)
51+
);
52+
53+
CREATE TABLE
54+
"Message" (
55+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
56+
"CONTENT" text,
57+
"fileUrl" TEXT,
58+
"memberId" TEXT ,
59+
"channelId" TEXT,
60+
"deleted" BOOLEAN,
61+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
62+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
63+
FOREIGN KEY ("channelId") REFERENCES public."Channel" (id),
64+
FOREIGN KEY ("memberId") REFERENCES public."Member" (id)
65+
);
66+
67+
CREATE TABLE
68+
"Conversation" (
69+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
70+
"memberOneId" TEXT ,
71+
"memberTwoId" TEXT,
72+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
73+
FOREIGN KEY ("memberTwoId") REFERENCES public."Member" (id),
74+
FOREIGN KEY ("memberOneId") REFERENCES public."Member" (id)
75+
);
76+
77+
CREATE TABLE
78+
"DirectMessage" (
79+
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
80+
"CONTENT" TEXT,
81+
"fileUrl" TEXT,
82+
"memberId" TEXT,
83+
"conversationId" TEXT,
84+
"deleted" BOOLEAN,
85+
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
86+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
87+
FOREIGN KEY ("memberId") REFERENCES "Member" (id),
88+
FOREIGN KEY ("conversationId") REFERENCES "Conversation" (id)
89+
);

0 commit comments

Comments
 (0)