Skip to content

Commit 9593b92

Browse files
committed
chore(database): start move to drizzle
1 parent 8ca3d25 commit 9593b92

File tree

19 files changed

+272
-271
lines changed

19 files changed

+272
-271
lines changed

.env.example

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ CONFIG_UPDATE_INTERVAL_YOUTUBE='10'
1818
CONFIG_UPDATE_INTERVAL_TWITCH='2'
1919
CONFIG_DISCORD_LOGS_CHANNEL='YOUR_DISCORD_LOGS_CHANNEL'
2020

21-
# Postgres
22-
POSTGRES_HOST='YOUR_POSTGRES_HOST'
23-
POSTGRES_PORT='YOUR_POSTGRES_PORT'
24-
POSTGRES_USER='YOUR_POSTGRES_USER'
25-
POSTGRES_PASSWORD='YOUR_POSTGRES_PASSWORD'
26-
POSTGRES_DB='YOUR_POSTGRES_DB'
27-
28-
# Postgres Dev
29-
POSTGRES_DEV_HOST='YOUR_POSTGRES_DEV_HOST'
30-
POSTGRES_DEV_PORT='YOUR_POSTGRES_DEV_PORT'
31-
POSTGRES_DEV_USER='YOUR_POSTGRES_DEV_USER'
32-
POSTGRES_DEV_PASSWORD='YOUR_POSTGRES_DEV_PASSWORD'
33-
POSTGRES_DEV_DB='YOUR_POSTGRES_DEV_DB'
21+
# Postgres URLs
22+
POSTGRES_URL='postgresql://user:password@server:port/database'
23+
POSTGRES_DEV_URL='postgresql://user:password@server:port/database'

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ dist/
55
target/
66

77
# Database stuff
8-
/backups
98
*.db
109
*.sqlite
1110
*.sqlite3*
1211
*.sql
1312
dbtests.ts
14-
perftesting.ts
13+
perftesting.ts
14+
drizzle/

bun.lock

Lines changed: 124 additions & 0 deletions
Large diffs are not rendered by default.

drizzle.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from "drizzle-kit";
2+
3+
import { config } from "./src/config";
4+
5+
console.log("Using database URL:", config.databaseUrl);
6+
7+
export default defineConfig({
8+
out: "./drizzle",
9+
schema: "./src/db/schema.ts",
10+
dialect: "postgresql",
11+
dbCredentials: {
12+
url: config.databaseUrl!,
13+
},
14+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@typescript-eslint/eslint-plugin": "8.11.0",
1010
"@typescript-eslint/parser": "8.11.0",
1111
"concurrently": "^9.1.2",
12+
"drizzle-kit": "^0.31.4",
1213
"eslint": "^8.57.0",
1314
"eslint-config-prettier": "9.1.0",
1415
"eslint-plugin-import": "^2.26.0",
@@ -20,6 +21,7 @@
2021
"typescript": "^5.0.0"
2122
},
2223
"scripts": {
24+
"db:generate": "bun drizzle-kit generate",
2325
"dev": "concurrently --names \"WEB,API,BOT\" --prefix-colors \"blue,green,magenta\" \"cd web && bun dev\" \"cd api && cargo watch -x \\\"run -- --dev\\\"\" \"bun --watch . --dev\"",
2426
"dev:bot": "bun --watch . --dev",
2527
"test:jetstream": "bun --watch src/utils/bluesky/jetstream.ts",
@@ -35,6 +37,7 @@
3537
"dependencies": {
3638
"cron": "^4.3.0",
3739
"discord.js": "^14.19.1",
40+
"drizzle-orm": "^0.44.2",
3841
"hfksdjfskfhsjdfhkasfdhksf": "^1.0.5",
3942
"pg": "^8.15.6"
4043
}

src/commands.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ import { checkIfStreamerIsLive } from "./utils/twitch/checkIfStreamerIsLive";
2626
import {
2727
checkIfChannelIsAlreadyTracked,
2828
addNewChannelToTrack,
29-
} from "./utils/db/youtube";
29+
} from "./db/youtube";
3030
import search from "./utils/youtube/search";
3131
import {
3232
checkIfGuildIsTrackingUserAlready,
3333
discordAddGuildTrackingUser,
34-
} from "./utils/db/discord";
34+
} from "./db/discord";
3535
import { Platform, YouTubeContentType } from "./types/types.d";
3636
import searchTwitch from "./utils/twitch/searchTwitch";
3737
import { getStreamerName } from "./utils/twitch/getStreamerName";
3838
import {
3939
addNewStreamerToTrack,
4040
checkIfStreamerIsAlreadyTracked,
41-
} from "./utils/db/twitch";
41+
} from "./db/twitch";
4242

4343
import client from ".";
4444

@@ -681,9 +681,9 @@ const commands: Record<string, Command> = {
681681
const query =
682682
platform === "youtube"
683683
? (interaction.options.get("channel_id")
684-
?.value as string)
684+
?.value as string)
685685
: (interaction.options.get("streamer_id")
686-
?.value as string);
686+
?.value as string);
687687

688688
// If the query is empty or not a string, return an empty array
689689
if (!query || typeof query !== "string") {

src/config.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const runningInDevMode: boolean = process.argv.includes("--dev");
33
export interface Config {
44
updateIntervalYouTube: number;
55
updateIntervalTwitch: number;
6+
databaseUrl: string | undefined;
67
}
78

89
export const config: Config = {
@@ -12,6 +13,9 @@ export const config: Config = {
1213
updateIntervalTwitch: process.env?.CONFIG_UPDATE_INTERVAL_TWITCH
1314
? parseInt(process.env?.CONFIG_UPDATE_INTERVAL_TWITCH) * 1000
1415
: 60_000,
16+
databaseUrl: runningInDevMode
17+
? process.env?.POSTGRES_DEV_URL
18+
: process.env?.POSTGRES_URL,
1519
};
1620

1721
interface Env {
@@ -29,29 +33,3 @@ export const env: Env = {
2933
twitchClientId: process.env?.TWITCH_CLIENT_ID,
3034
twitchClientSecret: process.env?.TWITCH_CLIENT_SECRET,
3135
};
32-
33-
interface DatabaseConfig {
34-
host: string | undefined;
35-
port: string | undefined;
36-
user: string | undefined;
37-
password: string | undefined;
38-
database: string | undefined;
39-
}
40-
41-
export const dbCredentials: DatabaseConfig = {
42-
host: runningInDevMode
43-
? process.env?.POSTGRES_DEV_HOST
44-
: process.env?.POSTGRES_HOST,
45-
port: runningInDevMode
46-
? process.env?.POSTGRES_DEV_PORT
47-
: process.env?.POSTGRES_PORT,
48-
user: runningInDevMode
49-
? process.env?.POSTGRES_DEV_USER
50-
: process.env?.POSTGRES_USER,
51-
password: runningInDevMode
52-
? process.env?.POSTGRES_DEV_PASSWORD
53-
: process.env?.POSTGRES_PASSWORD,
54-
database: runningInDevMode
55-
? process.env?.POSTGRES_DEV_DB
56-
: process.env?.POSTGRES_DB,
57-
};
File renamed without changes.

src/utils/db/botinfo.ts renamed to src/db/botinfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PoolClient, QueryResult } from "pg";
2-
import type { dbBotInfo } from "../../types/database";
2+
import type { dbBotInfo } from "../types/database";
33

4-
import { pool } from "../database";
4+
import { pool } from "../utils/database";
55

66
export async function updateBotInfo(
77
guilds_total: number = 0,

src/utils/db/cron.ts renamed to src/db/cron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pool } from "../database";
1+
import { pool } from "../utils/database";
22

33
export async function cronUpdateTopChannels(): Promise<void> {
44
const query = `

0 commit comments

Comments
 (0)