11// To make it easier to work with the database, disable prettier for this file
22/* eslint-disable prettier/prettier */
3- import { pgTable , serial , text , boolean , integer , timestamp , date } from "drizzle-orm/pg-core" ;
3+ import { pgTable , serial , text , boolean , timestamp , decimal , unique , index , pgEnum , jsonb , integer } from "drizzle-orm/pg-core" ;
44
55export const dbDiscordTable = pgTable ( "discord" , {
66 guildId : text ( "guild_id" ) . primaryKey ( ) ,
77 allowedPublicSharing : boolean ( "allowed_public_sharing" ) . notNull ( ) . default ( false ) ,
88 feedrUpdatesChannelId : text ( "feedr_updates_channel_id" ) ,
9-
9+ isInServer : boolean ( "is_in_server" ) . notNull ( ) . default ( true ) ,
1010} ) ;
1111
1212export const dbBlueskyTable = pgTable ( "bluesky" , {
1313 blueskyUserId : text ( "bluesky_user_id" ) . primaryKey ( ) ,
1414 latestPostId : text ( "latest_post_id" ) ,
1515 latestReplyId : text ( "latest_reply_id" ) ,
16- } ) ;
16+ } , ( table ) => [
17+ index ( "idx_bluesky_user_id" ) . on ( table . blueskyUserId ) ,
18+ ] ) ;
1719
1820export const dbYouTubeTable = pgTable ( "youtube" , {
1921 youtubeChannelId : text ( "youtube_channel_id" ) . primaryKey ( ) ,
@@ -23,13 +25,18 @@ export const dbYouTubeTable = pgTable("youtube", {
2325 latestShortIdUpdated : timestamp ( "latest_short_id_updated" ) ,
2426 latestStreamId : text ( "latest_stream_id" ) ,
2527 latestStreamIdUpdated : timestamp ( "latest_stream_id_updated" ) ,
26- youtubeChannelIsLive : boolean ( "youtube_channel_is_live" ) ,
27- } ) ;
28+ youtubeChannelIsLive : boolean ( "youtube_channel_is_live" ) . notNull ( ) . default ( false ) ,
29+ youtubeLiveIds : text ( "youtube_live_ids" ) . array ( ) . notNull ( ) . default ( [ ] ) ,
30+ } , ( table ) => [
31+ index ( "idx_youtube_channel_id" ) . on ( table . youtubeChannelId ) ,
32+ ] ) ;
2833
2934export const dbTwitchTable = pgTable ( "twitch" , {
3035 twitchChannelId : text ( "twitch_channel_id" ) . primaryKey ( ) ,
31- twitchChannelIsLive : boolean ( "twitch_channel_is_live" ) . notNull ( ) ,
32- } ) ;
36+ twitchChannelIsLive : boolean ( "twitch_channel_is_live" ) . notNull ( ) . default ( false ) ,
37+ } , ( table ) => [
38+ index ( "idx_twitch_channel_id" ) . on ( table . twitchChannelId ) ,
39+ ] ) ;
3340
3441export const dbGuildBlueskySubscriptionsTable = pgTable ( "guild_bluesky_subscriptions" , {
3542 id : serial ( "id" ) . primaryKey ( ) ,
@@ -39,7 +46,9 @@ export const dbGuildBlueskySubscriptionsTable = pgTable("guild_bluesky_subscript
3946 notificationRoleId : text ( "notification_role_id" ) ,
4047 isDm : boolean ( "is_dm" ) . notNull ( ) . default ( false ) ,
4148 checkForReplies : boolean ( "check_for_replies" ) . notNull ( ) . default ( false ) ,
42- } ) ;
49+ } , ( table ) => [
50+ unique ( "guild_bluesky_subscription" ) . on ( table . guildId , table . blueskyUserId ) ,
51+ ] ) ;
4352
4453export const dbGuildYouTubeSubscriptionsTable = pgTable ( "guild_youtube_subscriptions" , {
4554 id : serial ( "id" ) . primaryKey ( ) ,
@@ -51,7 +60,9 @@ export const dbGuildYouTubeSubscriptionsTable = pgTable("guild_youtube_subscript
5160 trackVideos : boolean ( "track_videos" ) . notNull ( ) . default ( false ) ,
5261 trackShorts : boolean ( "track_shorts" ) . notNull ( ) . default ( false ) ,
5362 trackStreams : boolean ( "track_streams" ) . notNull ( ) . default ( false ) ,
54- } ) ;
63+ } , ( table ) => [
64+ unique ( "guild_youtube_subscription" ) . on ( table . guildId , table . youtubeChannelId ) ,
65+ ] ) ;
5566
5667export const dbGuildTwitchSubscriptionsTable = pgTable ( "guild_twitch_subscriptions" , {
5768 id : serial ( "id" ) . primaryKey ( ) ,
@@ -60,42 +71,64 @@ export const dbGuildTwitchSubscriptionsTable = pgTable("guild_twitch_subscriptio
6071 notificationChannelId : text ( "notification_channel_id" ) . notNull ( ) ,
6172 notificationRoleId : text ( "notification_role_id" ) ,
6273 isDm : boolean ( "is_dm" ) . notNull ( ) . default ( false ) ,
63- } ) ;
74+ latestMessageId : text ( "latest_message_id" ) ,
75+ } , ( table ) => [
76+ unique ( "guild_twitch_subscription" ) . on ( table . guildId , table . twitchChannelId ) ,
77+ ] ) ;
6478
6579export const dbBotInfoTable = pgTable ( "bot_info" , {
80+ timestamp : timestamp ( "timestamp" ) . notNull ( ) . defaultNow ( ) ,
6681 guildsTotal : integer ( "guilds_total" ) . notNull ( ) . default ( 0 ) ,
6782 channelsTracked : integer ( "channels_tracked" ) . notNull ( ) . default ( 0 ) ,
6883 totalMembers : integer ( "total_members" ) . notNull ( ) . default ( 0 ) ,
69- time : timestamp ( "time ") . notNull ( ) . defaultNow ( ) ,
84+ notificationsSent : integer ( "notifications_sent ") . notNull ( ) . default ( 0 ) ,
7085} ) ;
7186
7287export const dbBotInfoNotificationsTable = pgTable ( "bot_info_notifications" , {
73- date : date ( "date ") . notNull ( ) ,
74- totalYouTube : integer ( "total_youtube ") . notNull ( ) . default ( 0 ) ,
75- totalTwitch : integer ( "total_twitch" ) . notNull ( ) . default ( 0 ) ,
88+ timestamp : timestamp ( "timestamp ") . notNull ( ) ,
89+ service : text ( "service ") . notNull ( ) ,
90+ delay : decimal ( "delay" , { precision : 10 , scale : 2 } ) . notNull ( ) . default ( "0.0" ) ,
7691} ) ;
7792
78- export const dbBotInfoNotificationsTimingsTable = pgTable ( "bot_info_notifications_timings" , {
79- time : timestamp ( "time" ) . notNull ( ) ,
80- channelId : text ( "channel_id" ) . notNull ( ) . references ( ( ) => dbYouTubeTable . youtubeChannelId ) ,
81- timeMs : integer ( "time_ms" ) . notNull ( ) . default ( 0 ) ,
82- } ) ;
93+ // Deprecated, but kept for reference
94+ // export const dbBotInfoNotificationsTimingsTable = pgTable("bot_info_notifications_timings", {
95+ // time: timestamp("time").notNull(),
96+ // channelId: text("channel_id").notNull().references(() => dbYouTubeTable.youtubeChannelId),
97+ // timeMs: integer("time_ms").notNull().default(0),
98+ // });
8399
84- export const dbBotInfoTopChannelsTable = pgTable ( "bot_info_top_channels" , {
85- youtubeChannelId : text ( "youtube_channel_id" ) . primaryKey ( ) . references ( ( ) => dbYouTubeTable . youtubeChannelId ) ,
86- guildsTracking : integer ( "guilds_tracking" ) . notNull ( ) . default ( 0 ) ,
87- } ) ;
100+ // Once again, can be aggregated in the API
101+ // export const dbBotInfoTopChannelsTable = pgTable("bot_info_top_channels", {
102+ // youtubeChannelId: text("youtube_channel_id").primaryKey().references(() => dbYouTubeTable.youtubeChannelId),
103+ // guildsTracking: integer("guilds_tracking").notNull().default(0),
104+ // });
88105
89- export const dbBotInfoTopGuildsTable = pgTable ( "bot_info_top_guilds" , {
90- guildId : text ( "guild_id" ) . primaryKey ( ) . references ( ( ) => dbDiscordTable . guildId ) ,
91- members : integer ( "members" ) . notNull ( ) . default ( 0 ) ,
92- } ) ;
106+ // Once again, can be aggregated in the API
107+ // export const dbBotInfoTopGuildsTable = pgTable("bot_info_top_guilds", {
108+ // guildId: text("guild_id").primaryKey().references(() => dbDiscordTable.guildId),
109+ // members: integer("members").notNull().default(0),
110+ // });
111+
112+ export const dbAuditLogsEventTypeEnum = pgEnum ( "event_type" , [
113+ "subscription_created" ,
114+ "subscription_deleted" ,
115+ "notification_sent" ,
116+ "guild_joined" ,
117+ "guild_left" ,
118+ ] ) ;
119+
120+ export const dbAuditLogsSuccessTypeEnum = pgEnum ( "audit_log_success" , [
121+ "success" ,
122+ "failure" ,
123+ "info" ,
124+ "warning" ,
125+ ] ) ;
93126
94127export const dbAuditLogsTable = pgTable ( "audit_logs" , {
95128 id : serial ( "id" ) . primaryKey ( ) ,
96- eventType : text ( "event_type" ) . notNull ( ) ,
97129 guildId : text ( "guild_id" ) . notNull ( ) . references ( ( ) => dbDiscordTable . guildId ) ,
98- relatedId : text ( "related_id" ) . notNull ( ) ,
99- note : text ( "note" ) ,
130+ eventType : dbAuditLogsEventTypeEnum ( ) . notNull ( ) ,
131+ successType : dbAuditLogsSuccessTypeEnum ( ) . notNull ( ) ,
132+ data : jsonb ( "data" ) ,
100133 occurredAt : timestamp ( "occurred_at" ) . defaultNow ( ) ,
101134} ) ;
0 commit comments