|
| 1 | +# Supabase Database Schema |
| 2 | + |
| 3 | +This document describes the database schema required for the BroCode application. |
| 4 | + |
| 5 | +## Tables |
| 6 | + |
| 7 | +### 1. `profiles` table |
| 8 | + |
| 9 | +Stores user profile information. |
| 10 | + |
| 11 | +```sql |
| 12 | +CREATE TABLE profiles ( |
| 13 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 14 | + name TEXT NOT NULL, |
| 15 | + username TEXT UNIQUE NOT NULL, |
| 16 | + email TEXT, |
| 17 | + phone TEXT, |
| 18 | + password TEXT, |
| 19 | + role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('admin', 'user', 'guest')), |
| 20 | + profile_pic_url TEXT, |
| 21 | + location TEXT, |
| 22 | + date_of_birth TEXT, |
| 23 | + is_verified BOOLEAN DEFAULT false, |
| 24 | + latitude NUMERIC, |
| 25 | + longitude NUMERIC, |
| 26 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 27 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 28 | +); |
| 29 | + |
| 30 | +-- Create unique index on username |
| 31 | +CREATE UNIQUE INDEX profiles_username_unique ON profiles(username); |
| 32 | + |
| 33 | +-- Enable Row Level Security (RLS) |
| 34 | +ALTER TABLE profiles ENABLE ROW LEVEL SECURITY; |
| 35 | + |
| 36 | +-- Policy: Users can read all profiles |
| 37 | +CREATE POLICY "Users can read all profiles" ON profiles |
| 38 | + FOR SELECT USING (true); |
| 39 | + |
| 40 | +-- Policy: Users can update their own profile |
| 41 | +CREATE POLICY "Users can update own profile" ON profiles |
| 42 | + FOR UPDATE USING (auth.uid() = id); |
| 43 | +``` |
| 44 | + |
| 45 | +### 2. `spots` table |
| 46 | + |
| 47 | +Stores spot/meetup information. |
| 48 | + |
| 49 | +```sql |
| 50 | +CREATE TABLE spots ( |
| 51 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 52 | + date TIMESTAMP WITH TIME ZONE NOT NULL, |
| 53 | + day TEXT NOT NULL, |
| 54 | + timing TEXT NOT NULL, |
| 55 | + budget NUMERIC NOT NULL DEFAULT 0, |
| 56 | + location TEXT NOT NULL, |
| 57 | + created_by UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, |
| 58 | + description TEXT, |
| 59 | + feedback TEXT, |
| 60 | + latitude NUMERIC, |
| 61 | + longitude NUMERIC, |
| 62 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 63 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 64 | +); |
| 65 | + |
| 66 | +-- Create index on date for faster queries |
| 67 | +CREATE INDEX spots_date_idx ON spots(date); |
| 68 | + |
| 69 | +-- Enable RLS |
| 70 | +ALTER TABLE spots ENABLE ROW LEVEL SECURITY; |
| 71 | + |
| 72 | +-- Policy: Everyone can read spots |
| 73 | +CREATE POLICY "Everyone can read spots" ON spots |
| 74 | + FOR SELECT USING (true); |
| 75 | + |
| 76 | +-- Policy: Only admins can create spots |
| 77 | +CREATE POLICY "Admins can create spots" ON spots |
| 78 | + FOR INSERT WITH CHECK ( |
| 79 | + EXISTS ( |
| 80 | + SELECT 1 FROM profiles |
| 81 | + WHERE profiles.id = auth.uid() |
| 82 | + AND profiles.role = 'admin' |
| 83 | + ) |
| 84 | + ); |
| 85 | + |
| 86 | +-- Policy: Only admins can update spots |
| 87 | +CREATE POLICY "Admins can update spots" ON spots |
| 88 | + FOR UPDATE USING ( |
| 89 | + EXISTS ( |
| 90 | + SELECT 1 FROM profiles |
| 91 | + WHERE profiles.id = auth.uid() |
| 92 | + AND profiles.role = 'admin' |
| 93 | + ) |
| 94 | + ); |
| 95 | + |
| 96 | +-- Policy: Only admins can delete spots |
| 97 | +CREATE POLICY "Admins can delete spots" ON spots |
| 98 | + FOR DELETE USING ( |
| 99 | + EXISTS ( |
| 100 | + SELECT 1 FROM profiles |
| 101 | + WHERE profiles.id = auth.uid() |
| 102 | + AND profiles.role = 'admin' |
| 103 | + ) |
| 104 | + ); |
| 105 | +``` |
| 106 | + |
| 107 | +### 3. `invitations` table |
| 108 | + |
| 109 | +Stores RSVP invitations for spots. |
| 110 | + |
| 111 | +```sql |
| 112 | +CREATE TABLE invitations ( |
| 113 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 114 | + spot_id UUID NOT NULL REFERENCES spots(id) ON DELETE CASCADE, |
| 115 | + user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, |
| 116 | + status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('confirmed', 'pending', 'declined')), |
| 117 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 118 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 119 | + UNIQUE(spot_id, user_id) |
| 120 | +); |
| 121 | + |
| 122 | +-- Create indexes for faster queries |
| 123 | +CREATE INDEX invitations_spot_id_idx ON invitations(spot_id); |
| 124 | +CREATE INDEX invitations_user_id_idx ON invitations(user_id); |
| 125 | + |
| 126 | +-- Enable RLS |
| 127 | +ALTER TABLE invitations ENABLE ROW LEVEL SECURITY; |
| 128 | + |
| 129 | +-- Policy: Everyone can read invitations |
| 130 | +CREATE POLICY "Everyone can read invitations" ON invitations |
| 131 | + FOR SELECT USING (true); |
| 132 | + |
| 133 | +-- Policy: Users can create/update their own invitations |
| 134 | +CREATE POLICY "Users can manage own invitations" ON invitations |
| 135 | + FOR ALL USING (auth.uid() = user_id); |
| 136 | +``` |
| 137 | + |
| 138 | +### 4. `payments` table |
| 139 | + |
| 140 | +Stores payment information for spots. |
| 141 | + |
| 142 | +```sql |
| 143 | +CREATE TABLE payments ( |
| 144 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 145 | + spot_id UUID NOT NULL REFERENCES spots(id) ON DELETE CASCADE, |
| 146 | + user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, |
| 147 | + status TEXT NOT NULL DEFAULT 'not_paid' CHECK (status IN ('paid', 'not_paid')), |
| 148 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 149 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 150 | + UNIQUE(spot_id, user_id) |
| 151 | +); |
| 152 | + |
| 153 | +-- Create indexes for faster queries |
| 154 | +CREATE INDEX payments_spot_id_idx ON payments(spot_id); |
| 155 | +CREATE INDEX payments_user_id_idx ON payments(user_id); |
| 156 | + |
| 157 | +-- Enable RLS |
| 158 | +ALTER TABLE payments ENABLE ROW LEVEL SECURITY; |
| 159 | + |
| 160 | +-- Policy: Everyone can read payments |
| 161 | +CREATE POLICY "Everyone can read payments" ON payments |
| 162 | + FOR SELECT USING (true); |
| 163 | + |
| 164 | +-- Policy: Admins can update payments |
| 165 | +CREATE POLICY "Admins can update payments" ON payments |
| 166 | + FOR UPDATE USING ( |
| 167 | + EXISTS ( |
| 168 | + SELECT 1 FROM profiles |
| 169 | + WHERE profiles.id = auth.uid() |
| 170 | + AND profiles.role = 'admin' |
| 171 | + ) |
| 172 | + ); |
| 173 | + |
| 174 | +-- Policy: System can create payments (via service role) |
| 175 | +CREATE POLICY "System can create payments" ON payments |
| 176 | + FOR INSERT WITH CHECK (true); |
| 177 | +``` |
| 178 | + |
| 179 | +### 5. `chat_messages` table |
| 180 | + |
| 181 | +Stores chat messages. |
| 182 | + |
| 183 | +```sql |
| 184 | +CREATE TABLE chat_messages ( |
| 185 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 186 | + user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, |
| 187 | + content_text TEXT, |
| 188 | + content_image_urls TEXT[], |
| 189 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 190 | + reactions JSONB DEFAULT '{}'::jsonb |
| 191 | +); |
| 192 | + |
| 193 | +-- Create index on created_at for faster queries |
| 194 | +CREATE INDEX chat_messages_created_at_idx ON chat_messages(created_at); |
| 195 | + |
| 196 | +-- Enable RLS |
| 197 | +ALTER TABLE chat_messages ENABLE ROW LEVEL SECURITY; |
| 198 | + |
| 199 | +-- Policy: Everyone can read messages |
| 200 | +CREATE POLICY "Everyone can read messages" ON chat_messages |
| 201 | + FOR SELECT USING (true); |
| 202 | + |
| 203 | +-- Policy: Users can create their own messages |
| 204 | +CREATE POLICY "Users can create own messages" ON chat_messages |
| 205 | + FOR INSERT WITH CHECK (auth.uid() = user_id); |
| 206 | + |
| 207 | +-- Policy: Users can delete their own messages |
| 208 | +CREATE POLICY "Users can delete own messages" ON chat_messages |
| 209 | + FOR DELETE USING (auth.uid() = user_id); |
| 210 | +``` |
| 211 | + |
| 212 | +### 6. `moments` table |
| 213 | + |
| 214 | +Stores user moments/photos. |
| 215 | + |
| 216 | +```sql |
| 217 | +CREATE TABLE moments ( |
| 218 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 219 | + user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, |
| 220 | + image_url TEXT NOT NULL, |
| 221 | + caption TEXT, |
| 222 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 223 | +); |
| 224 | + |
| 225 | +-- Create index on user_id and created_at |
| 226 | +CREATE INDEX moments_user_id_idx ON moments(user_id); |
| 227 | +CREATE INDEX moments_created_at_idx ON moments(created_at DESC); |
| 228 | + |
| 229 | +-- Enable RLS |
| 230 | +ALTER TABLE moments ENABLE ROW LEVEL SECURITY; |
| 231 | + |
| 232 | +-- Policy: Everyone can read moments |
| 233 | +CREATE POLICY "Everyone can read moments" ON moments |
| 234 | + FOR SELECT USING (true); |
| 235 | + |
| 236 | +-- Policy: Users can create their own moments |
| 237 | +CREATE POLICY "Users can create own moments" ON moments |
| 238 | + FOR INSERT WITH CHECK (auth.uid() = user_id); |
| 239 | + |
| 240 | +-- Policy: Users can delete their own moments |
| 241 | +CREATE POLICY "Users can delete own moments" ON moments |
| 242 | + FOR DELETE USING (auth.uid() = user_id); |
| 243 | +``` |
| 244 | + |
| 245 | +## Real-time Subscriptions |
| 246 | + |
| 247 | +Enable real-time for the following tables in Supabase Dashboard: |
| 248 | +- `spots` - for spot updates |
| 249 | +- `invitations` - for RSVP updates |
| 250 | +- `payments` - for payment status updates |
| 251 | +- `chat_messages` - for chat updates |
| 252 | + |
| 253 | +## Initial Data |
| 254 | + |
| 255 | +After creating the tables, you can insert the admin user: |
| 256 | + |
| 257 | +```sql |
| 258 | +INSERT INTO profiles (id, name, username, email, phone, password, role, location, is_verified) |
| 259 | +VALUES ( |
| 260 | + 'admin', |
| 261 | + 'Ram', |
| 262 | + 'ramvj2005', |
| 263 | + 'ramvj2005@gmail.com', |
| 264 | + '7826821130', |
| 265 | + 'ramkumar', |
| 266 | + 'admin', |
| 267 | + 'Attibele', |
| 268 | + true |
| 269 | +); |
| 270 | +``` |
| 271 | + |
| 272 | +## Notes |
| 273 | + |
| 274 | +1. Make sure to enable Row Level Security (RLS) on all tables |
| 275 | +2. Adjust policies based on your security requirements |
| 276 | +3. The `username` field has a UNIQUE constraint to ensure uniqueness |
| 277 | +4. All timestamps use `TIMESTAMP WITH TIME ZONE` for proper timezone handling |
| 278 | +5. Foreign key constraints ensure data integrity |
0 commit comments