|
| 1 | +# PosgreSQl table setup |
| 2 | + |
| 3 | +#### Points to remember:- |
| 4 | + |
| 5 | +- There are `7` tables in **public** schema. |
| 6 | +- Make sure your RLS policy is Disabled for all tables. |
| 7 | +- You have to create both enums and tables. |
| 8 | +- Running the provided codes may create problem in case sensitiveness. Make sure you correct those cases. |
| 9 | +- To run the SQL code go to [Supabase Dashboard](https://supabase.com/dashboard/project) -> `YourProject` -> `SQL Editor` |
| 10 | +- Make sure you create the ENUM first then the Tables sequentially. |
| 11 | +- `If your SQL code does not work then please try to create by seeing the images.` |
| 12 | + |
| 13 | +| Table Name | No. of Columns | |
| 14 | +| ------------- | -------------- | |
| 15 | +| Profile | 7 | |
| 16 | +| Server | 7 | |
| 17 | +| Channel | 7 | |
| 18 | +| Member | 6 | |
| 19 | +| Conversation | 4 | |
| 20 | +| Message | 8 | |
| 21 | +| DirectMessage | 8 | |
| 22 | + |
| 23 | +## Enums --> |
| 24 | + |
| 25 | +| Enum name | Values | |
| 26 | +| ----------- | --------------------------- | |
| 27 | +| ChannelType | [ TEXT, AUDIO, VIDEO ] | |
| 28 | +| MemberRole | [ ADMIN, MODERATOR, GUEST ] | |
| 29 | + |
| 30 | +Code to create the enums -> |
| 31 | + |
| 32 | +```sql |
| 33 | +CREATE TYPE channeltype AS ENUM ('TEXT', 'AUDIO', 'VIDEO'); |
| 34 | +CREATE TYPE memberrole AS ENUM ('MODERATOR', 'GUEST', 'ADMIN'); |
| 35 | +``` |
| 36 | + |
| 37 | +## 1. Table - 'Profile': |
| 38 | + |
| 39 | +| Column Name | Data Type | Description | |
| 40 | +| ----------- | --------- | ------------------------- | |
| 41 | +| id | text | Primary key | |
| 42 | +| name | text | Username of the user | |
| 43 | +| userId | text | | |
| 44 | +| imgUrl | text | profile image of the user | |
| 45 | +| email | text | Email of the user | |
| 46 | +| createdAt | timestamp | Time of creation | |
| 47 | +| updatedAt | timestamp | Last updated time | |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### SQL Code to Create Table |
| 52 | + |
| 53 | +```sql |
| 54 | +CREATE TABLE |
| 55 | + public.Profile ( |
| 56 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 57 | + userId TEXT, |
| 58 | + name TEXT, |
| 59 | + imgUrl TEXT, |
| 60 | + email TEXT, |
| 61 | + createdAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 62 | + updatedAt TIMESTAMP WITH TIME ZONE DEFAULT now() |
| 63 | + ); |
| 64 | +``` |
| 65 | + |
| 66 | +## 2. Table - 'Server': |
| 67 | + |
| 68 | +| Column Name | Data Type | Description | |
| 69 | +| ----------- | --------- | ------------------------------------------------------ | |
| 70 | +| id | text | Primary key | |
| 71 | +| name | text | Name of the server | |
| 72 | +| imgUrl | text | Thumbnail | |
| 73 | +| profileId | text | Owner of the server- It is a forign key of Profile(id) | |
| 74 | +| inviteCode | text | Invite link for the server | |
| 75 | +| createdAt | timestamp | Time of creation | |
| 76 | +| updatedAt | timestamp | Last updated time | |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +### SQL code to create table |
| 81 | + |
| 82 | +```sql |
| 83 | +CREATE TABLE |
| 84 | + public.Server ( |
| 85 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 86 | + name TEXT, |
| 87 | + imgUrl TEXT, |
| 88 | + inviteCode TEXT, |
| 89 | + profileId TEXT, |
| 90 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 91 | + updatedAt TIMESTAMP WITHOUT TIME ZONE default now(), |
| 92 | + FOREIGN KEY (profileId) REFERENCES public.Profile (id) |
| 93 | + ); |
| 94 | +``` |
| 95 | + |
| 96 | +## 3. Table - 'Channel': |
| 97 | + |
| 98 | +| Column Name | Data Type | Description | |
| 99 | +| ----------- | ----------- | ------------------------------------------- | |
| 100 | +| id | text | Primary key | |
| 101 | +| name | text | Name of the channel | |
| 102 | +| type | channelType | It is a enum of type channel type | |
| 103 | +| serverId | text | Server id- It is a forign key of Server(id) | |
| 104 | +| profileId | text | Profile id of admin - It is a forign key | |
| 105 | +| createdAt | timestamp | Time of creation | |
| 106 | +| updatedAt | timestamp | Last updated time | |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +### SQL code to create table |
| 111 | + |
| 112 | +```sql |
| 113 | +CREATE TABLE |
| 114 | + Channel ( |
| 115 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 116 | + name text, |
| 117 | + type channeltype, |
| 118 | + serverId text, |
| 119 | + profileId text, |
| 120 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 121 | + updatedAt TIMESTAMP WITHOUT TIME ZONE default now(), |
| 122 | + FOREIGN KEY (profileId) REFERENCES public.Profile (id), |
| 123 | + FOREIGN KEY (serverId) REFERENCES public.Server (id) |
| 124 | + ); |
| 125 | +``` |
| 126 | + |
| 127 | +## 3. Table - 'Member': |
| 128 | + |
| 129 | +| Column Name | Data Type | Description | |
| 130 | +| ----------- | ---------- | ------------------------------------------- | |
| 131 | +| id | text | Primary key | |
| 132 | +| role | memberrole | It is a enum of type member role | |
| 133 | +| serverId | text | Server id- It is a forign key of Server(id) | |
| 134 | +| profileId | text | Profile id of admin - It is a forign key | |
| 135 | +| createdAt | timestamp | Time of creation | |
| 136 | +| updatedAt | timestamp | Last updated time | |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +### SQL code to create table |
| 141 | + |
| 142 | +```sql |
| 143 | +CREATE TABLE |
| 144 | + Member ( |
| 145 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 146 | + role memberrole, |
| 147 | + serverId text, |
| 148 | + profileId text, |
| 149 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 150 | + updatedAt TIMESTAMP WITHOUT TIME ZONE default now(), |
| 151 | + FOREIGN KEY (profileId) REFERENCES public.Profile (id), |
| 152 | + FOREIGN KEY (serverId) REFERENCES public.Server (id) |
| 153 | + ); |
| 154 | +``` |
| 155 | + |
| 156 | +## 3. Table - 'Message': |
| 157 | + |
| 158 | +| Column Name | Data Type | Description | |
| 159 | +| ----------- | --------- | ----------------------------------------------- | |
| 160 | +| id | text | Primary key | |
| 161 | +| content | text | Content of the message | |
| 162 | +| fileUrl | text | Attachment url (nullable) | |
| 163 | +| memberId | text | Member id - It is a foreign key for Member(id) | |
| 164 | +| channelId | text | Channel id - It is a forign key for Channel(id) | |
| 165 | +| deleted | boolean | A boolean value to check if message is deleted | |
| 166 | +| createdAt | timestamp | Time of creation | |
| 167 | +| updatedAt | timestamp | Last updated time | |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +### SQL code to create table |
| 172 | + |
| 173 | +```sql |
| 174 | +CREATE TABLE |
| 175 | + Message ( |
| 176 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 177 | + CONTENT text, |
| 178 | + fileUrl TEXT, |
| 179 | + memberId TEXT , |
| 180 | + channelId TEXT, |
| 181 | + deleted BOOLEAN, |
| 182 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 183 | + updatedAt TIMESTAMP WITHOUT TIME ZONE default now(), |
| 184 | + FOREIGN KEY (channelId) REFERENCES public.Channel (id), |
| 185 | + FOREIGN KEY (memberId) REFERENCES public.Member (id) |
| 186 | + ); |
| 187 | +``` |
| 188 | + |
| 189 | +## 4. Table - 'Conversation': |
| 190 | + |
| 191 | +| Column Name | Data Type | Description | |
| 192 | +| ----------- | --------- | ----------------------------------------------- | |
| 193 | +| id | text | Primary key | |
| 194 | +| memberOneId | text | Sender Member Id - Foreign key for Member(id) | |
| 195 | +| memberTwoId | text | Receiver Member Id - Foreign key for Member(id) | |
| 196 | +| createdAt | timestamp | Time of creation | |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +### SQL code to create Table |
| 201 | + |
| 202 | +```sql |
| 203 | +CREATE TABLE |
| 204 | + Conversation ( |
| 205 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 206 | + memberOneId TEXT , |
| 207 | + memberTwoId TEXT, |
| 208 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 209 | + FOREIGN KEY (memberTwoId) REFERENCES public.Member (id), |
| 210 | + FOREIGN KEY (memberOneId) REFERENCES public.Member (id) |
| 211 | + ); |
| 212 | +``` |
| 213 | + |
| 214 | +## 5. Table - 'DirectMessage': |
| 215 | + |
| 216 | +| Column Name | Data Type | Description | |
| 217 | +| -------------- | --------- | -------------------------------------------------- | |
| 218 | +| id | text | Primary key | |
| 219 | +| content | text | Content of the message | |
| 220 | +| fileUrl | text | Attachment url (nullable) | |
| 221 | +| memberId | text | Sender Id - foreign keys for Member(id) | |
| 222 | +| conversationId | text | Converastion Id - Foreign key for Conversation(id) | |
| 223 | +| deleted | boolean | A boolean value to check if message is deleted | |
| 224 | +| createdAt | timestamp | Time of creation | |
| 225 | +| updatedAt | timestamp | Last updated time | |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +```sql |
| 230 | +CREATE TABLE |
| 231 | + DirectMessage ( |
| 232 | + id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), |
| 233 | + CONTENT TEXT, |
| 234 | + fileUrl TEXT, |
| 235 | + memberId TEXT, |
| 236 | + conversationId TEXT, |
| 237 | + deleted BOOLEAN, |
| 238 | + createdAt TIMESTAMP WITHOUT TIME ZONE default current_timestamp, |
| 239 | + updatedAt TIMESTAMP WITHOUT TIME ZONE default now(), |
| 240 | + FOREIGN KEY (memberId) REFERENCES Member (id), |
| 241 | + FOREIGN KEY (conversationId) REFERENCES Conversation (id) |
| 242 | + ); |
| 243 | +``` |
0 commit comments