|
1 | 1 | import type { dbDiscordTable, dbYouTube } from "../types/database"; |
2 | 2 |
|
3 | | -import path from "path"; |
| 3 | +import { Pool } from "pg"; |
4 | 4 |
|
5 | | -import { Database } from "bun:sqlite"; |
| 5 | +import { dbCredentials } from "../config"; |
6 | 6 |
|
7 | | -const db = new Database(path.resolve(process.cwd(), "db.sqlite3")); |
| 7 | +// import path from "path"; |
| 8 | +// import { Database } from "bun:sqlite"; |
| 9 | +// const db = new Database(path.resolve(process.cwd(), "db.sqlite3")); |
| 10 | + |
| 11 | +if ( |
| 12 | + !dbCredentials.host || |
| 13 | + !dbCredentials.port || |
| 14 | + !dbCredentials.user || |
| 15 | + !dbCredentials.password || |
| 16 | + !dbCredentials.database |
| 17 | +) { |
| 18 | + throw new Error("Database credentials are not set"); |
| 19 | +} |
| 20 | + |
| 21 | +export const pool: Pool = new Pool({ |
| 22 | + host: dbCredentials.host, |
| 23 | + port: parseInt(dbCredentials.port), |
| 24 | + user: dbCredentials.user, |
| 25 | + password: dbCredentials.password, |
| 26 | + database: dbCredentials.database, |
| 27 | +}); |
8 | 28 |
|
9 | 29 | // #region Init Tables |
10 | 30 | export async function initTables(): Promise<boolean> { |
11 | | - const createYouTubeTable = ` |
12 | | - CREATE TABLE IF NOT EXISTS youtube ( |
13 | | - youtube_channel_id TEXT PRIMARY KEY, |
14 | | - latest_video_id TEXT UNIQUE |
| 31 | + const createDiscordTable = ` |
| 32 | + CREATE TABLE IF NOT EXISTS discord ( |
| 33 | + guild_id TEXT PRIMARY KEY |
15 | 34 | ); |
16 | 35 | `; |
17 | 36 |
|
18 | | - const createDiscordTable = ` |
19 | | - CREATE TABLE IF NOT EXISTS discord ( |
20 | | - guild_id TEXT, |
21 | | - guild_channel_id TEXT NOT NULL, |
22 | | - guild_platform TEXT NOT NULL, |
23 | | - platform_user_id TEXT NOT NULL, |
24 | | - guild_ping_role TEXT |
| 37 | + const createYouTubeTable = ` |
| 38 | + CREATE TABLE IF NOT EXISTS youtube ( |
| 39 | + youtube_channel_id TEXT PRIMARY KEY, |
| 40 | + latest_video_id TEXT, |
| 41 | + latest_video_id_updated TIMESTAMP, |
| 42 | + latest_short_id TEXT, |
| 43 | + latest_short_id_updated TIMESTAMP, |
| 44 | + latest_stream_id TEXT, |
| 45 | + latest_stream_id_updated TIMESTAMP, |
| 46 | + youtube_channel_is_live BOOLEAN |
25 | 47 | ); |
26 | 48 | `; |
27 | 49 |
|
28 | 50 | const createTwitchTable = ` |
29 | 51 | CREATE TABLE IF NOT EXISTS twitch ( |
30 | 52 | twitch_channel_id TEXT PRIMARY KEY, |
31 | | - is_live BOOLEAN |
| 53 | + twitch_channel_is_live BOOLEAN NOT NULL |
| 54 | + ); |
| 55 | + `; |
| 56 | + |
| 57 | + const createGuildYouTubeSubscriptionsTable = ` |
| 58 | + CREATE TABLE IF NOT EXISTS guild_youtube_subscriptions ( |
| 59 | + id SERIAL PRIMARY KEY, |
| 60 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 61 | + youtube_channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id), |
| 62 | + notification_channel_id TEXT NOT NULL, |
| 63 | + notification_role_id TEXT, |
| 64 | + is_dm BOOLEAN DEFAULT FALSE |
| 65 | + ); |
| 66 | + `; |
| 67 | + |
| 68 | + const createGuildTwitchSubscriptionsTable = ` |
| 69 | + CREATE TABLE IF NOT EXISTS guild_twitch_subscriptions ( |
| 70 | + id SERIAL PRIMARY KEY, |
| 71 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 72 | + twitch_channel_id TEXT NOT NULL REFERENCES twitch(twitch_channel_id), |
| 73 | + notification_channel_id TEXT NOT NULL, |
| 74 | + notification_role_id TEXT, |
| 75 | + is_dm BOOLEAN DEFAULT FALSE |
32 | 76 | ); |
33 | 77 | `; |
34 | 78 |
|
35 | 79 | const createBotInfoTable = ` |
36 | 80 | CREATE TABLE IF NOT EXISTS bot_info ( |
37 | | - total_servers INTEGER NOT NULL DEFAULT 1, |
38 | | - total_members INTEGER NOT NULL DEFAULT 1 |
| 81 | + guilds_total INTEGER NOT NULL, |
| 82 | + channels_tracked INTEGER NOT NULL, |
| 83 | + updated_at TIMESTAMP DEFAULT now() |
39 | 84 | ); |
40 | 85 | `; |
41 | 86 |
|
42 | | - try { |
43 | | - db.run(createYouTubeTable); |
44 | | - console.log("YouTube table created"); |
| 87 | + const createAuditLogsTable = ` |
| 88 | + CREATE TABLE IF NOT EXISTS audit_logs ( |
| 89 | + id SERIAL PRIMARY KEY, |
| 90 | + event_type TEXT NOT NULL, |
| 91 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 92 | + related_id TEXT NOT NULL, |
| 93 | + note TEXT, |
| 94 | + occurred_at TIMESTAMP DEFAULT now() |
| 95 | + ); |
| 96 | + `; |
45 | 97 |
|
46 | | - db.run(createDiscordTable); |
| 98 | + try { |
| 99 | + await pool.query(createDiscordTable); |
47 | 100 | console.log("Discord table created"); |
48 | 101 |
|
49 | | - db.run(createTwitchTable); |
| 102 | + await pool.query(createYouTubeTable); |
| 103 | + console.log("YouTube table created"); |
| 104 | + |
| 105 | + await pool.query(createTwitchTable); |
50 | 106 | console.log("Twitch table created"); |
51 | 107 |
|
52 | | - db.run(createBotInfoTable); |
| 108 | + await pool.query(createGuildYouTubeSubscriptionsTable); |
| 109 | + console.log("Guild YouTube Subscriptions table created"); |
| 110 | + |
| 111 | + await pool.query(createGuildTwitchSubscriptionsTable); |
| 112 | + console.log("Guild Twitch Subscriptions table created"); |
| 113 | + |
| 114 | + await pool.query(createBotInfoTable); |
53 | 115 | console.log("Bot Info table created"); |
54 | 116 |
|
| 117 | + await pool.query(createAuditLogsTable); |
| 118 | + console.log("Audit Logs table created"); |
| 119 | + |
55 | 120 | return true; |
56 | 121 | } catch (err) { |
57 | 122 | console.error("Error creating tables:", err); |
58 | 123 |
|
59 | 124 | return false; |
60 | 125 | } |
61 | 126 | } |
| 127 | + |
62 | 128 | // #endregion |
63 | 129 |
|
64 | 130 | // #region YouTube |
|
0 commit comments