Skip to content

Commit 246928c

Browse files
committed
feat: add rooms list
1 parent 894c16e commit 246928c

File tree

5 files changed

+493
-37
lines changed

5 files changed

+493
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ yarn-error.log*
2727

2828
# local env files
2929
.env*.local
30+
.env
3031

3132
# vercel
3233
.vercel

app/messaging/page.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getRooms } from "@/modules/messaging"
2+
3+
export default async function Page() {
4+
const rooms = await getRooms()
5+
6+
return (
7+
<div>
8+
<h1 className="mb-4 text-4xl">Messages</h1>
9+
<ul>
10+
{rooms.map(({ id, name }) => (
11+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
12+
<li key={id}>{name}</li>
13+
))}
14+
</ul>
15+
</div>
16+
)
17+
}

modules/messaging/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Database } from "@/modules/types"
2+
import { createClient } from "@/modules/utils/server"
3+
4+
export type Room = Database["public"]["Tables"]["rooms"]["Row"]
5+
6+
type ServerError = {
7+
error: { message: string }
8+
}
9+
10+
export async function createRoom({
11+
name,
12+
}: {
13+
name: string
14+
}): Promise<ServerError | void> {
15+
const supabase = createClient()
16+
const { error } = await supabase.from("rooms").insert({
17+
name,
18+
})
19+
if (error) return { error: { message: error.message } }
20+
}
21+
22+
export async function getRooms(): Promise<Room[]> {
23+
const supabase = createClient()
24+
const { data, error } = await supabase.from("rooms").select("*")
25+
26+
if (error) throw new Error(error.message)
27+
28+
return data
29+
}

modules/messaging/migration.sql

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
create table "public"."messages" (
2+
"id" bigint generated by default as identity not null,
3+
"created_at" timestamp with time zone not null default now(),
4+
"room_id" bigint not null,
5+
"user_id" uuid not null default auth.uid(),
6+
"content" text not null
7+
);
8+
9+
10+
alter table "public"."messages" enable row level security;
11+
12+
create table "public"."room_participants" (
13+
"id" bigint generated by default as identity not null,
14+
"joined_at" timestamp with time zone not null default now(),
15+
"user_id" uuid not null,
16+
"room_id" bigint not null
17+
);
18+
19+
20+
alter table "public"."room_participants" enable row level security;
21+
22+
create table "public"."rooms" (
23+
"id" bigint generated by default as identity not null,
24+
"created_at" timestamp with time zone not null default now(),
25+
"name" character varying not null,
26+
"is_private" boolean not null default true
27+
);
28+
29+
30+
alter table "public"."rooms" enable row level security;
31+
32+
CREATE UNIQUE INDEX messages_pkey ON public.messages USING btree (id);
33+
34+
CREATE UNIQUE INDEX room_participants_pkey ON public.room_participants USING btree (id);
35+
36+
CREATE UNIQUE INDEX rooms_pkey ON public.rooms USING btree (id);
37+
38+
alter table "public"."messages" add constraint "messages_pkey" PRIMARY KEY using index "messages_pkey";
39+
40+
alter table "public"."room_participants" add constraint "room_participants_pkey" PRIMARY KEY using index "room_participants_pkey";
41+
42+
alter table "public"."rooms" add constraint "rooms_pkey" PRIMARY KEY using index "rooms_pkey";
43+
44+
alter table "public"."messages" add constraint "messages_room_id_fkey" FOREIGN KEY (room_id) REFERENCES rooms(id) ON DELETE CASCADE not valid;
45+
46+
alter table "public"."messages" validate constraint "messages_room_id_fkey";
47+
48+
alter table "public"."messages" add constraint "messages_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
49+
50+
alter table "public"."messages" validate constraint "messages_user_id_fkey";
51+
52+
alter table "public"."room_participants" add constraint "room_participants_room_id_fkey" FOREIGN KEY (room_id) REFERENCES rooms(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
53+
54+
alter table "public"."room_participants" validate constraint "room_participants_room_id_fkey";
55+
56+
alter table "public"."room_participants" add constraint "room_participants_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
57+
58+
alter table "public"."room_participants" validate constraint "room_participants_user_id_fkey";
59+
60+
grant delete on table "public"."messages" to "anon";
61+
62+
grant insert on table "public"."messages" to "anon";
63+
64+
grant references on table "public"."messages" to "anon";
65+
66+
grant select on table "public"."messages" to "anon";
67+
68+
grant trigger on table "public"."messages" to "anon";
69+
70+
grant truncate on table "public"."messages" to "anon";
71+
72+
grant update on table "public"."messages" to "anon";
73+
74+
grant delete on table "public"."messages" to "authenticated";
75+
76+
grant insert on table "public"."messages" to "authenticated";
77+
78+
grant references on table "public"."messages" to "authenticated";
79+
80+
grant select on table "public"."messages" to "authenticated";
81+
82+
grant trigger on table "public"."messages" to "authenticated";
83+
84+
grant truncate on table "public"."messages" to "authenticated";
85+
86+
grant update on table "public"."messages" to "authenticated";
87+
88+
grant delete on table "public"."messages" to "service_role";
89+
90+
grant insert on table "public"."messages" to "service_role";
91+
92+
grant references on table "public"."messages" to "service_role";
93+
94+
grant select on table "public"."messages" to "service_role";
95+
96+
grant trigger on table "public"."messages" to "service_role";
97+
98+
grant truncate on table "public"."messages" to "service_role";
99+
100+
grant update on table "public"."messages" to "service_role";
101+
102+
grant delete on table "public"."room_participants" to "anon";
103+
104+
grant insert on table "public"."room_participants" to "anon";
105+
106+
grant references on table "public"."room_participants" to "anon";
107+
108+
grant select on table "public"."room_participants" to "anon";
109+
110+
grant trigger on table "public"."room_participants" to "anon";
111+
112+
grant truncate on table "public"."room_participants" to "anon";
113+
114+
grant update on table "public"."room_participants" to "anon";
115+
116+
grant delete on table "public"."room_participants" to "authenticated";
117+
118+
grant insert on table "public"."room_participants" to "authenticated";
119+
120+
grant references on table "public"."room_participants" to "authenticated";
121+
122+
grant select on table "public"."room_participants" to "authenticated";
123+
124+
grant trigger on table "public"."room_participants" to "authenticated";
125+
126+
grant truncate on table "public"."room_participants" to "authenticated";
127+
128+
grant update on table "public"."room_participants" to "authenticated";
129+
130+
grant delete on table "public"."room_participants" to "service_role";
131+
132+
grant insert on table "public"."room_participants" to "service_role";
133+
134+
grant references on table "public"."room_participants" to "service_role";
135+
136+
grant select on table "public"."room_participants" to "service_role";
137+
138+
grant trigger on table "public"."room_participants" to "service_role";
139+
140+
grant truncate on table "public"."room_participants" to "service_role";
141+
142+
grant update on table "public"."room_participants" to "service_role";
143+
144+
grant delete on table "public"."rooms" to "anon";
145+
146+
grant insert on table "public"."rooms" to "anon";
147+
148+
grant references on table "public"."rooms" to "anon";
149+
150+
grant select on table "public"."rooms" to "anon";
151+
152+
grant trigger on table "public"."rooms" to "anon";
153+
154+
grant truncate on table "public"."rooms" to "anon";
155+
156+
grant update on table "public"."rooms" to "anon";
157+
158+
grant delete on table "public"."rooms" to "authenticated";
159+
160+
grant insert on table "public"."rooms" to "authenticated";
161+
162+
grant references on table "public"."rooms" to "authenticated";
163+
164+
grant select on table "public"."rooms" to "authenticated";
165+
166+
grant trigger on table "public"."rooms" to "authenticated";
167+
168+
grant truncate on table "public"."rooms" to "authenticated";
169+
170+
grant update on table "public"."rooms" to "authenticated";
171+
172+
grant delete on table "public"."rooms" to "service_role";
173+
174+
grant insert on table "public"."rooms" to "service_role";
175+
176+
grant references on table "public"."rooms" to "service_role";
177+
178+
grant select on table "public"."rooms" to "service_role";
179+
180+
grant trigger on table "public"."rooms" to "service_role";
181+
182+
grant truncate on table "public"."rooms" to "service_role";
183+
184+
grant update on table "public"."rooms" to "service_role";
185+
186+

0 commit comments

Comments
 (0)