Skip to content

Commit fc0b5d3

Browse files
committed
added pg pool
1 parent 1c47b7c commit fc0b5d3

File tree

3 files changed

+94
-26
lines changed

3 files changed

+94
-26
lines changed

src/utils/database.ts

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,130 @@
11
import type { dbDiscordTable, dbYouTube } from "../types/database";
22

3-
import path from "path";
3+
import { Pool } from "pg";
44

5-
import { Database } from "bun:sqlite";
5+
import { dbCredentials } from "../config";
66

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+
});
828

929
// #region Init Tables
1030
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
1534
);
1635
`;
1736

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
2547
);
2648
`;
2749

2850
const createTwitchTable = `
2951
CREATE TABLE IF NOT EXISTS twitch (
3052
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
3276
);
3377
`;
3478

3579
const createBotInfoTable = `
3680
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()
3984
);
4085
`;
4186

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+
`;
4597

46-
db.run(createDiscordTable);
98+
try {
99+
await pool.query(createDiscordTable);
47100
console.log("Discord table created");
48101

49-
db.run(createTwitchTable);
102+
await pool.query(createYouTubeTable);
103+
console.log("YouTube table created");
104+
105+
await pool.query(createTwitchTable);
50106
console.log("Twitch table created");
51107

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);
53115
console.log("Bot Info table created");
54116

117+
await pool.query(createAuditLogsTable);
118+
console.log("Audit Logs table created");
119+
55120
return true;
56121
} catch (err) {
57122
console.error("Error creating tables:", err);
58123

59124
return false;
60125
}
61126
}
127+
62128
// #endregion
63129

64130
// #region YouTube

src/utils/logging.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils/migratedb.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import path from "path";
2+
3+
import { Database } from "bun:sqlite";
4+
5+
const db = new Database(path.resolve(process.cwd(), "db.sqlite3"));

0 commit comments

Comments
 (0)