Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat: proper db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 committed Feb 12, 2024
1 parent f8e70a1 commit a5008d2
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 108 deletions.
33 changes: 3 additions & 30 deletions src/Schema/collections/channel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { pgTable, text, integer, boolean } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm/relations";
import { guilds } from "./guild.js";
import { voiceStates } from "./voice.js";

export const channels = pgTable("channels", {
id: text("id").primaryKey(),
Expand All @@ -23,40 +21,15 @@ export const channels = pgTable("channels", {
messageCount: integer("message_count"),
defaultAutoArchiveDuration: integer("default_auto_archive_duration"),
permissions: text("permissions"),
flags: integer("flags")
});
flags: integer("flags"),

export const guildsChannels = pgTable("guild_channels", {
id: text("id").primaryKey().references(() => channels.id, { onDelete: "cascade" }),
guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" })
guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }),

Check warning on line 26 in src/Schema/collections/channel.ts

View workflow job for this annotation

GitHub Actions / test / lint

Unexpected trailing comma

Check warning on line 26 in src/Schema/collections/channel.ts

View workflow job for this annotation

GitHub Actions / test / lint

Unexpected trailing comma
});

export const channelsOverwrite = pgTable("channels_overwrite", {
id: text("id").primaryKey().references(() => channels.id, { onDelete: "cascade" }),

type: integer("type"),
allow: text("allow"),
deny: text("deny")
});

export const channelsRelation = relations(channels, ({ many }) => ({
permission_overwrites: many(channelsOverwrite)
}));

export const guildChannelsRelations = relations(guildsChannels, ({ many, one }) => ({
states: many(voiceStates),
channel: one(channels, {
fields: [guildsChannels.id],
references: [channels.id]
}),
guild: one(guilds, {
fields: [guildsChannels.id],
references: [guilds.id]
})
}));

export const channelsOverwriteRelation = relations(channelsOverwrite, ({ one }) => ({
channel: one(channels, {
fields: [channelsOverwrite.id],
references: [channels.id]
})
}));
8 changes: 0 additions & 8 deletions src/Schema/collections/guild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { relations } from "drizzle-orm";
import { pgTable, text, boolean, integer } from "drizzle-orm/pg-core";
import { guildsRoles } from "./roles.js";
import { voiceStates } from "./voice.js";

export const guilds = pgTable("guilds", {
id: text("id").primaryKey(),
Expand Down Expand Up @@ -42,8 +39,3 @@ export const guilds = pgTable("guilds", {
premiumProgressBarEnabled: boolean("premium_progress_bar_enabled"),
safetyAlertChannelId: text("safety_alert_channel_id")
});

export const guildsRelations = relations(guilds, ({ many }) => ({
roles: many(guildsRoles),
voiceStates: many(voiceStates)
}));
13 changes: 3 additions & 10 deletions src/Schema/collections/member.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { relations } from "drizzle-orm";
import { pgTable, integer, text, boolean } from "drizzle-orm/pg-core";
import { memberRoles } from "./roles.js";
import { users } from "./user.js";
import { guilds } from "./guild.js";

Check failure on line 3 in src/Schema/collections/member.ts

View workflow job for this annotation

GitHub Actions / test / lint

`./guild.js` import should occur before import of `./user.js`

Check failure on line 3 in src/Schema/collections/member.ts

View workflow job for this annotation

GitHub Actions / test / lint

`./guild.js` import should occur before import of `./user.js`

export const members = pgTable("members", {
id: text("id").primaryKey().references(() => users.id, { onDelete: "cascade" }),
guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }),

nick: text("nick"),
avatar: text("avatar"),
flags: integer("flags"),
Expand All @@ -16,11 +17,3 @@ export const members = pgTable("members", {
permissions: integer("permissions"),
communicationDisabledUntil: text("communication_disabled_until")
});

export const membersRelations = relations(members, ({ many, one }) => ({
roles: many(memberRoles),
user: one(users, {
fields: [members.id],
references: [users.id]
})
}));
13 changes: 2 additions & 11 deletions src/Schema/collections/message.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { relations } from "drizzle-orm";
import { pgTable, text, boolean, integer } from "drizzle-orm/pg-core";
import { channels } from "./channel.js";
import { users } from "./user.js";

export const messages = pgTable("messages", {
id: text("id").primaryKey(),

channelId: text("channel_id").references(() => channels.id, { onDelete: "cascade" }),
authorId: text("author_id").references(() => users.id, { onDelete: "set null" }),

content: text("content"),
timestamp: text("timestamp"),
editedTimestamp: text("edited_timestamp"),
Expand All @@ -21,14 +22,4 @@ export const messages = pgTable("messages", {
position: integer("position")
});

export const messagesRelation = relations(messages, ({ one }) => ({
channel: one(channels, {
fields: [messages.channelId],
references: [channels.id]
}),
author: one(users, {
fields: [messages.authorId],
references: [users.id]
})
}));

Check warning on line 25 in src/Schema/collections/message.ts

View workflow job for this annotation

GitHub Actions / test / lint

Too many blank lines at the end of file. Max of 1 allowed

Check warning on line 25 in src/Schema/collections/message.ts

View workflow job for this annotation

GitHub Actions / test / lint

Too many blank lines at the end of file. Max of 1 allowed
25 changes: 0 additions & 25 deletions src/Schema/collections/roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { relations } from "drizzle-orm";
import { boolean, integer, pgTable, text } from "drizzle-orm/pg-core";
import { guilds } from "./guild.js";
import { members } from "./member.js";
Expand All @@ -14,34 +13,10 @@ export const roles = pgTable("roles", {

export const memberRoles = pgTable("member_roles", {
id: text("id").primaryKey().references(() => members.id, { onDelete: "cascade" }),

roleId: text("role_id").references(() => roles.id, { onDelete: "cascade" })
});

export const guildsRoles = pgTable("guild_roles", {
id: text("id").primaryKey().references(() => guilds.id, { onDelete: "cascade" }),
roleId: text("role_id").references(() => roles.id, { onDelete: "cascade" })
});

export const memberRolesRelation = relations(memberRoles, ({ one }) => ({
role: one(roles, {
fields: [memberRoles.roleId],
references: [roles.id]
}),
member: one(members, {
fields: [memberRoles.id],
references: [members.id]
})
}));

export const guildsRolesRelation = relations(guildsRoles, ({ one }) => ({
role: one(roles, {
fields: [guildsRoles.roleId],
references: [roles.id]
}),
guild: one(guilds, {
fields: [guildsRoles.id],
references: [guilds.id]
})
}));

3 changes: 1 addition & 2 deletions src/Schema/collections/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { integer, pgTable, text } from "drizzle-orm/pg-core";

export const sessions = pgTable("sessions", {
id: integer("id").primaryKey(),

resumeURL: text("resume_url").notNull(),
sequence: integer("sequence").notNull(),
sessionId: text("session_id").notNull(),
sessionId: text("session_id").notNull().unique(),
shardCount: integer("shardCount").notNull()
});
29 changes: 7 additions & 22 deletions src/Schema/collections/voice.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { relations } from "drizzle-orm";
import { pgTable, text, boolean } from "drizzle-orm/pg-core";
import { guildsChannels } from "./channel.js";
import { pgTable, text, boolean, integer } from "drizzle-orm/pg-core";

Check failure on line 1 in src/Schema/collections/voice.ts

View workflow job for this annotation

GitHub Actions / test / lint

'integer' is defined but never used

Check failure on line 1 in src/Schema/collections/voice.ts

View workflow job for this annotation

GitHub Actions / test / lint

'integer' is defined but never used
import { guilds } from "./guild.js";
import { members } from "./member.js";
import { channels } from "./channel.js";

Check failure on line 4 in src/Schema/collections/voice.ts

View workflow job for this annotation

GitHub Actions / test / lint

`./channel.js` import should occur before import of `./guild.js`

Check failure on line 4 in src/Schema/collections/voice.ts

View workflow job for this annotation

GitHub Actions / test / lint

`./channel.js` import should occur before import of `./guild.js`
import { sessions } from "./session.js";

export const voiceStates = pgTable("voice_states", {
channelId: text("channel_id").references(() => guildsChannels.id, { onDelete: "cascade" }),
guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }),
memberId: text("member_id").primaryKey().references(() => members.id, { onDelete: "cascade" }),
sessionId: text("session_id"),
guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }),
channelId: text("channel_id").references(() => channels.id, { onDelete: "cascade" }),
sessionId: text("session_id").references(() => sessions.sessionId, { onDelete: "cascade" }),

deaf: boolean("deaf"),
mute: boolean("mute"),
selfDeaf: boolean("self_deaf"),
Expand All @@ -18,19 +19,3 @@ export const voiceStates = pgTable("voice_states", {
suppress: boolean("suppress"),
requestToSpeakTimestamp: text("request_to_speak_timestamp")
});

export const voiceStatesRelation = relations(voiceStates, ({ one }) => ({
channel: one(guildsChannels, {
fields: [voiceStates.channelId],
references: [guildsChannels.id]
}),
guild: one(guilds, {
fields: [voiceStates.guildId],
references: [guilds.id]
}),
member: one(members, {
fields: [voiceStates.memberId],
references: [members.id]
})
}));

0 comments on commit a5008d2

Please sign in to comment.