Skip to content

Commit

Permalink
added migrations and seed script (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devank-Sachdeva authored Jun 24, 2024
1 parent 76e345b commit bdbc6eb
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ NEXT_PUBLIC_LIVEKIT_URL=wss://discord-clonelink.....
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
SUPABASE_ACCESS_TOKEN=supabaseaccesstoken
SUPABASE_DB_PASSWORD=supabasedbpassword
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"db:seed": "tsc seed.ts && node seed.js"
},
"dependencies": {
"@clerk/nextjs": "^4.25.4",
Expand Down Expand Up @@ -59,9 +60,11 @@
"@types/react-dom": "^18",
"@types/uuid": "^9.0.5",
"autoprefixer": "^10",
"dotenv": "^16.4.5",
"eslint": "^8",
"eslint-config-next": "13.5.4",
"postcss": "^8",
"supabase": "^1.169.8",
"tailwindcss": "^3",
"typescript": "^5"
}
Expand Down
60 changes: 60 additions & 0 deletions seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createClient } from "@supabase/supabase-js";
require('dotenv').config({ path: `.env.local` })


const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ""
);



async function seedDataBase() {
try {
const profile = await supabase
.from('Profile')
.insert([{
userId: `user_1`,
name: `Dummy User`,
imgUrl: "https://utfs.io/f/684fd07f-6421-4d5c-8ab4-4bc16c7e3cea-tpuwo3.jpg",
email: `[email protected]`,
}])
.select("id");

const server = await supabase
.from('Server')
.insert([
{
name: 'Sample Server',
imgUrl: 'https://utfs.io/f/0686af8c-8c20-423d-a273-79e07239dac7-7v9i42.avif',
inviteCode: 'invite_1',
profileId: profile.data![0].id
}

]).select("id");

const channel = await supabase
.from('Channel')
.insert([
{
name: 'Sample Channel',
type: "TEXT",
serverId: server.data![0].id,
profileId: profile.data![0].id
}
]);

const memeber = await supabase.from('Member')
.insert([{
role: "ADMIN",
profileId: profile.data![0].id,
serverId: server.data![0].id
}]);


console.log("Database Seeded successfully");
} catch (err) {
console.error('Unexpected error:', err);
}
}
seedDataBase()
89 changes: 89 additions & 0 deletions supabase/migrations/20240528135826_create_structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
CREATE TYPE "channeltype" AS ENUM ('TEXT', 'AUDIO', 'VIDEO');
CREATE TYPE "memberrole" AS ENUM ('MODERATOR', 'GUEST', 'ADMIN');

CREATE TABLE
public."Profile" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"userId" TEXT,
"name" TEXT,
"imgUrl" TEXT,
"email" TEXT,
"createdAt" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);


CREATE TABLE
public."Server" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"name" TEXT,
"imgUrl" TEXT,
"inviteCode" TEXT,
"profileId" TEXT,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id)
);

CREATE TABLE
"Channel" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"name" text,
"type" "channeltype",
"serverId" text,
"profileId" text,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id),
FOREIGN KEY ("serverId") REFERENCES public."Server" (id)
);

CREATE TABLE
"Member" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"role" "memberrole",
"serverId" text,
"profileId" text,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
FOREIGN KEY ("profileId") REFERENCES public."Profile" (id),
FOREIGN KEY ("serverId") REFERENCES public."Server" (id)
);

CREATE TABLE
"Message" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"CONTENT" text,
"fileUrl" TEXT,
"memberId" TEXT ,
"channelId" TEXT,
"deleted" BOOLEAN,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
FOREIGN KEY ("channelId") REFERENCES public."Channel" (id),
FOREIGN KEY ("memberId") REFERENCES public."Member" (id)
);

CREATE TABLE
"Conversation" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"memberOneId" TEXT ,
"memberTwoId" TEXT,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
FOREIGN KEY ("memberTwoId") REFERENCES public."Member" (id),
FOREIGN KEY ("memberOneId") REFERENCES public."Member" (id)
);

CREATE TABLE
"DirectMessage" (
"id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
"CONTENT" TEXT,
"fileUrl" TEXT,
"memberId" TEXT,
"conversationId" TEXT,
"deleted" BOOLEAN,
"createdAt" TIMESTAMP WITHOUT TIME ZONE default current_timestamp,
"updatedAt" TIMESTAMP WITHOUT TIME ZONE default now(),
FOREIGN KEY ("memberId") REFERENCES "Member" (id),
FOREIGN KEY ("conversationId") REFERENCES "Conversation" (id)
);

0 comments on commit bdbc6eb

Please sign in to comment.