Skip to content

Commit 8a4fd58

Browse files
committed
feat(schema.prisma): switch database provider from sqlite to postgresql for better scalability and performance
chore(schema.prisma): comment out old postgresql datasource configuration for future reference refactor(schema.prisma): update database schema to better represent Discord data - Replace 'roles' model with 'Users' and 'Roles' models to separate user and role data - Add 'UserRoles' model to represent many-to-many relationship between users and roles - Replace 'notes' model with 'Infractions' and 'Snippets' models to separate infraction and snippet data - Remove 'moderators', 'settings', 'logs', 'guilds', and 'messages' models as they are not needed in the new schema - Add 'InfractionType' enum to represent different types of infractions - Update comments to better explain the purpose of each model and field These changes were made to better align the database schema with the data provided by Discord and to simplify the data model. feat: add new models for logs, user_roles, notes, snippets, guilds, infractions to extend database schema WHY: To support new features such as logging, user roles, moderator notes, text snippets, guilds, and user infractions.
1 parent a25748b commit 8a4fd58

File tree

1 file changed

+235
-132
lines changed

1 file changed

+235
-132
lines changed

prisma/schema.prisma

+235-132
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// datasource db {
2-
// provider = "postgresql"
3-
// url = env("DATABASE_URL")
4-
// }
1+
// // datasource db {
2+
// // provider = "postgresql"
3+
// // url = env("DATABASE_URL")
4+
// // }
55

66
datasource db {
77
// SQLite provider
8-
provider = "sqlite"
8+
provider = "postgresql"
99
// Database local file
1010
url = "file:database.db"
1111
}
@@ -19,149 +19,252 @@ generator client {
1919
recursive_type_depth = -1
2020
}
2121

22-
// Table representing roles in the system
23-
model roles {
24-
// Role identifier
25-
role_id BigInt @id
26-
// Name of the role
27-
role_name String
28-
// Link to users associated with this role
29-
user_roles user_roles[]
30-
// Link to moderators associated with this role
31-
moderators moderators[]
32-
}
22+
model Users {
23+
// The user’s unique ID (via Discord)
24+
id BigInt @id
25+
// The user’s username.
26+
name String
27+
// The user’s global nickname, taking precedence over the username in display.
28+
global_name String?
29+
// Returns the user’s display name. For regular users this is just their global name or their username, but if they have a guild specific nickname then that is returned instead.
30+
display_name String
31+
// Returns a string that allows you to mention the given user.
32+
mention String
33+
// Specifies if the user is a bot account.
34+
bot Boolean
35+
// Returns the user’s creation time in UTC. This is when the user’s Discord account was created.
36+
created_at DateTime
37+
// True if user is a member of a guild (not a discord.py attribute)
38+
is_member Boolean @default(true)
39+
// The guild specific nickname of the user. Takes precedence over the global name.
40+
nick String?
41+
// An aware datetime object that specifies the date and time in UTC that the member joined the guild. If the member left and rejoined the guild, this will be the latest date. In certain cases, this can be None.
42+
joined_at DateTime?
3343
34-
// General settings for the system
35-
model settings {
36-
// Key of the setting
37-
setting_key String @id
38-
// Value of the setting
39-
setting_value String
40-
// Optional description of what the setting does
41-
setting_description String?
42-
}
44+
// This is a relation field and is a list of roles that the user has, linking to the `UserRoles` table. If you fetch a user from the database and include this field, you will get all the roles associated with that user.
45+
roles UserRoles[]
4346
44-
// Table representing users in the system
45-
model users {
46-
// User identifier (Discord ID)
47-
user_id BigInt @id
48-
// Indicator if the user is a bot
49-
user_is_bot Boolean?
50-
// Optional User's username
51-
user_username String?
52-
// Optional User's global name
53-
user_global_name String?
54-
// Timestamp when user was created
55-
user_created_at DateTime
56-
// Link to roles associated with this user
57-
user_roles user_roles[]
58-
// Link to notes associated with this user
59-
notes notes[]
60-
// Link to infractions associated with this user
61-
infractions infractions[]
62-
}
47+
// This represents all the infractions that this user has given out when acting as a moderator. It has a `relation` annotation to make clear that for these infractions, this user is referred to in the `moderator` field of the `Infractions` table.
48+
infractions_given Infractions[] @relation("Moderator")
49+
50+
// This is all the infractions that this user has received. It has a `relation` annotation to make clear that for these infractions, this user is referred to in the `user` field of the `Infractions` table.
51+
infractions_received Infractions[] @relation("User")
6352
64-
// Table representing moderators in the system
65-
model moderators {
66-
// Moderator identifier (Discord ID)
67-
moderator_id BigInt @id
68-
// Associated role identifier
69-
role_id BigInt?
70-
// Link to role associated with this moderator
71-
role roles? @relation(fields: [role_id], references: [role_id])
72-
// Link to notes created by this moderator
73-
notes notes[]
74-
// Link to infractions created by this moderator
75-
infractions infractions[]
53+
snippets Snippets[]
7654
}
7755

78-
// Table for storing logs
79-
model logs {
80-
// Unique log identifier
81-
log_id String @id @default(uuid())
82-
// Timestamp when log was created
83-
log_created_at DateTime @default(now())
84-
// Level of the log
85-
log_level String?
86-
// Content/text of the log
87-
log_content String?
56+
model Roles {
57+
// The ID for the role (via Discord)
58+
id BigInt @id
59+
// The name of the role
60+
name String
61+
// Indicates if the role will be displayed separately from other members.
62+
hoist Boolean
63+
// Indicates if the role is managed by the guild through some form of integrations such as Twitch.
64+
managed Boolean @default(false)
65+
// Indicates if the role is mentionable.
66+
mentionable Boolean @default(false)
67+
// The role’s creation time in UTC.
68+
created_at DateTime
69+
// Returns a string that allows you to mention a role.
70+
mention String @default("")
71+
72+
// This field links a role to the users that have it. It references the `UserRoles` junction table. If you fetch a role from the database and include this field, you will get a list of UserRoles entries and from there you can find all the users that have this role.
73+
users UserRoles[]
74+
75+
// This is a Boolean field indicating if the role is a moderator role. This is not an attribute coming from Discord but an extra field you have defined to distinguish normal roles from moderator roles. It defaults to false, meaning if you don't specify it when creating a new role, it will be assumed to be a non-moderator role.
76+
is_mod Boolean @default(false)
8877
}
8978

90-
// Relationship table between a user and their roles
91-
model user_roles {
92-
// User identifier
79+
model UserRoles {
80+
id BigInt @id @default(autoincrement())
81+
82+
// These refer to the `Users` model. `user_id` is the ID of a user from the `Users` table. The line `user Users @relation(fields: [user_id], references: [id])` implies that `user` establishes a relation with `Users` model based on the `user_id` in this `UserRoles` model and the `id` in the `Users` model.
83+
user Users @relation(fields: [user_id], references: [id])
9384
user_id BigInt
94-
// Role identifier
85+
86+
// These refer to the `Roles` model. `role_id` is the ID of a role from the `Roles` table. The line `role Roles @relation(fields: [role_id], references: [id])` implies that `role` establishes a relation with `Roles` model based on the `role_id` in this `UserRoles` model and the `id` in the `Roles` model.
87+
role Roles @relation(fields: [role_id], references: [id])
9588
role_id BigInt
96-
// Link to the user
97-
users users @relation(fields: [user_id], references: [user_id])
98-
// Link to the role
99-
roles roles @relation(fields: [role_id], references: [role_id])
10089
101-
// Composite primary key consisting of user_id and role_id
102-
@@id([user_id, role_id])
90+
// This specifies a composite unique constraint on `user_id` and `role_id`, meaning each combination of a user id and role id must be unique. It prevents a single user from having the same role multiple times. This constraint helps to maintain data integrity.
91+
@@unique([user_id, role_id])
10392
}
10493

105-
// Table for storing notes/moderator reports on users
106-
model notes {
107-
// Unique identifier for the note
108-
note_id String @id @default(uuid())
109-
// Content of the note
110-
note_content String
111-
// Moderator who created the note
112-
moderator_id BigInt?
113-
// User who the note is about
114-
user_id BigInt?
115-
// When the note was created
116-
note_created_at DateTime @default(now())
117-
// Link to the moderator
118-
moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
119-
// Link to the user
120-
user users? @relation(fields: [user_id], references: [user_id])
94+
enum InfractionType {
95+
BAN
96+
KICK
97+
WARN
98+
TIMEOUT
12199
}
122100

123-
// Table for storing one-to-many snippets of text
124-
model snippets {
125-
// Name of the snippet
126-
snippet_name String @id
127-
// Content of the snippet
128-
snippet_content String
129-
}
101+
model Infractions {
102+
id BigInt @id
103+
infraction_type InfractionType
104+
infraction_reason String?
105+
created_at DateTime
106+
expires_at DateTime?
130107
131-
// Table representing guilds/servers in the system
132-
model guilds {
133-
// Unique identifier for the guild (Discord ID)
134-
guild_id BigInt @id
135-
// Name of the guild
136-
guild_name String
137-
// ID of the owner of the guild
138-
guild_owner_id BigInt
139-
}
108+
// These fields establish a relationship with the `Users` model. `moderator_id` is the ID of the user who gave the infraction. The line `moderator Users? @relation("Moderator", fields: [moderator_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the moderator) is associated with this infraction.
109+
moderator Users? @relation("Moderator", fields: [moderator_id], references: [id])
110+
moderator_id BigInt?
140111
141-
// Table representing infractions/punishments on users
142-
model infractions {
143-
// Unique identifier for the infraction
144-
infraction_id String @id @default(uuid())
145-
// ID of the moderator who gave the infraction
146-
moderator_id BigInt?
147-
// ID of the user who received the infraction
148-
user_id BigInt?
149-
// Type of the infraction (ban, mute, etc)
150-
infraction_type String
151-
// Optional reason for the infraction
152-
infraction_reason String?
153-
// When the infraction was given
154-
infraction_created_at DateTime @default(now())
155-
// When the infraction expires, if applicable
156-
infraction_expires_at DateTime?
157-
// Link to the moderator who gave the infraction
158-
moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
159-
// Link to the user who received the infraction
160-
user users? @relation(fields: [user_id], references: [user_id])
112+
// These fields establish another relationship with the `Users` model. `user_id` is the ID of the user who received the infraction. The line `user Users @relation("User", fields: [user_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the user who received the infraction) is associated with this infraction.
113+
user Users @relation("User", fields: [user_id], references: [id])
114+
user_id BigInt
161115
}
162116

163-
model messages {
164-
message_id BigInt @id
165-
message_text String
166-
message_time DateTime
117+
model Snippets {
118+
// The name of the snippet
119+
name String @id
120+
// The content of the snippet
121+
content String
122+
// The creation time of the snippet
123+
created_at DateTime @default(now())
124+
author Users @relation(fields: [author_id], references: [id])
125+
author_id BigInt
167126
}
127+
128+
// Old stuff
129+
130+
// // Table representing users in the system
131+
// model users {
132+
// // user identifier (Discord ID)
133+
// user_id BigInt @id
134+
// // Indicator if the user is a bot
135+
// user_is_bot Boolean?
136+
// // Optional user's username
137+
// user_username String?
138+
// // Optional user's global name
139+
// user_global_name String?
140+
// // Timestamp when user was created
141+
// user_created_at DateTime
142+
// // Link to roles associated with this user
143+
// user_roles user_roles[]
144+
// // Link to notes associated with this user
145+
// notes notes[]
146+
// // Link to infractions associated with this user
147+
// infractions infractions[]
148+
// //
149+
// }
150+
151+
// // Table representing roles in the system
152+
// model roles {
153+
// // Role identifier
154+
// role_id BigInt @id
155+
// // Name of the role
156+
// role_name String
157+
// // Link to users associated with this role
158+
// user_roles user_roles[]
159+
// // Link to moderators associated with this role
160+
// moderators moderators[]
161+
// }
162+
163+
// // General settings for the system
164+
// model settings {
165+
// // Key of the setting
166+
// setting_key String @id
167+
// // Value of the setting
168+
// setting_value String
169+
// // Optional description of what the setting does
170+
// setting_description String?
171+
// }
172+
173+
// // Table representing moderators in the system
174+
// model moderators {
175+
// // Moderator identifier (Discord ID)
176+
// moderator_id BigInt @id
177+
// // Associated role identifier
178+
// role_id BigInt?
179+
// // Link to role associated with this moderator
180+
// role roles? @relation(fields: [role_id], references: [role_id])
181+
// // Link to notes created by this moderator
182+
// notes notes[]
183+
// // Link to infractions created by this moderator
184+
// infractions infractions[]
185+
// }
186+
187+
// // Table for storing logs
188+
// model logs {
189+
// // Unique log identifier
190+
// log_id String @id @default(uuid())
191+
// // Timestamp when log was created
192+
// log_created_at DateTime @default(now())
193+
// // Level of the log
194+
// log_level String?
195+
// // Content/text of the log
196+
// log_content String?
197+
// }
198+
199+
// // Relationship table between a user and their roles
200+
// model user_roles {
201+
// // User identifier
202+
// user_id BigInt
203+
// // Role identifier
204+
// role_id BigInt
205+
// // Link to the user
206+
// users users @relation(fields: [user_id], references: [user_id])
207+
// // Link to the role
208+
// roles roles @relation(fields: [role_id], references: [role_id])
209+
210+
// // Composite primary key consisting of user_id and role_id
211+
// @@id([user_id, role_id])
212+
// }
213+
214+
// // Table for storing notes/moderator reports on users
215+
// model notes {
216+
// // Unique identifier for the note
217+
// note_id String @id @default(uuid())
218+
// // Content of the note
219+
// note_content String
220+
// // Moderator who created the note
221+
// moderator_id BigInt?
222+
// // User who the note is about
223+
// user_id BigInt?
224+
// // When the note was created
225+
// note_created_at DateTime @default(now())
226+
// // Link to the moderator
227+
// moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
228+
// // Link to the user
229+
// user users? @relation(fields: [user_id], references: [user_id])
230+
// }
231+
232+
// // Table for storing one-to-many snippets of text
233+
// model snippets {
234+
// // Name of the snippet
235+
// snippet_name String @id
236+
// // Content of the snippet
237+
// snippet_content String
238+
// }
239+
240+
// // Table representing guilds/servers in the system
241+
// model guilds {
242+
// // Unique identifier for the guild (Discord ID)
243+
// guild_id BigInt @id
244+
// // Name of the guild
245+
// guild_name String
246+
// // ID of the owner of the guild
247+
// guild_owner_id BigInt
248+
// }
249+
250+
// // Table representing infractions/punishments on users
251+
// model infractions {
252+
// // Unique identifier for the infraction
253+
// infraction_id String @id @default(uuid())
254+
// // ID of the moderator who gave the infraction
255+
// moderator_id BigInt?
256+
// // ID of the user who received the infraction
257+
// user_id BigInt?
258+
// // Type of the infraction (ban, mute, etc)
259+
// infraction_type String
260+
// // Optional reason for the infraction
261+
// infraction_reason String?
262+
// // When the infraction was given
263+
// infraction_created_at DateTime @default(now())
264+
// // When the infraction expires, if applicable
265+
// infraction_expires_at DateTime?
266+
// // Link to the moderator who gave the infraction
267+
// moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
268+
// // Link to the user who received the infraction
269+
// user users? @relation(fields: [user_id], references: [user_id])
270+
// }

0 commit comments

Comments
 (0)