Skip to content

Commit 0c31ee6

Browse files
committed
chore: move db to mysql
1 parent 91033c0 commit 0c31ee6

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

api/drizzle.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { defineConfig } from "drizzle-kit";
22

33
export default defineConfig({
4-
dialect: "postgresql",
4+
dialect: "mysql",
55
schema: "./src/utils/db/schema.ts",
66
dbCredentials: {
7-
url: process.env.DATABASE_URL!,
7+
host: process.env.DATABASE_HOST!,
8+
port: parseInt(process.env.DATABASE_PORT!),
9+
user: process.env.DATABASE_USER!,
10+
password: process.env.DATABASE_PASSWORD!,
11+
database: process.env.DATABASE_NAME!,
812
},
913
verbose: true,
1014
});

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@trpc/server": "^10.45.2",
1414
"drizzle-orm": "^0.32.1",
1515
"hono": "^4.5.2",
16-
"postgres": "^3.4.4",
16+
"mysql2": "^3.11.0",
1717
"zod": "^3.23.8"
1818
},
1919
"devDependencies": {

api/src/router/channels.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,13 @@ export const channelsRouter = router({
5050
message: "Channel already exists",
5151
});
5252

53-
const createdChannels = await ctx.db
54-
.insert(channels)
55-
.values({
56-
id: input.channelId,
57-
guildId: input.guildId,
58-
name: input.name,
59-
count: 0,
60-
})
61-
.returning();
62-
return createdChannels[0];
53+
await ctx.db.insert(channels).values({
54+
id: input.channelId,
55+
guildId: input.guildId,
56+
name: input.name,
57+
count: 0,
58+
});
59+
return { success: true };
6360
}),
6461
setCount: procedure
6562
.input(

api/src/router/guilds.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ export const guildsRouter = router({
66
createGuild: procedure
77
.input(z.object({ id: z.string(), name: z.string() }))
88
.mutation(async ({ ctx, input }) => {
9-
const createdGuilds = await ctx.db
10-
.insert(guilds)
11-
.values({
12-
id: input.id,
13-
name: input.name,
14-
})
15-
.returning();
16-
return createdGuilds[0];
9+
await ctx.db.insert(guilds).values({
10+
id: input.id,
11+
name: input.name,
12+
});
13+
return { success: true };
1714
}),
1815
});

api/src/utils/db/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { drizzle } from "drizzle-orm/postgres-js";
2-
import postgres from "postgres";
1+
import { drizzle } from "drizzle-orm/mysql2";
2+
import mysql from "mysql2/promise";
33
import * as schema from "./schema";
44

5-
const connectionString = process.env.DATABASE_URL!;
6-
const client = postgres(connectionString);
5+
const poolConnection = mysql.createPool({
6+
host: process.env.DATABASE_HOST!,
7+
port: parseInt(process.env.DATABASE_PORT!),
8+
user: process.env.DATABASE_USER!,
9+
password: process.env.DATABASE_PASSWORD!,
10+
database: process.env.DATABASE_NAME!,
11+
});
712

8-
export const db = drizzle(client, { schema });
13+
export const db = drizzle(poolConnection, { mode: "default", schema });

api/src/utils/db/schema.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { integer, pgTable, text } from "drizzle-orm/pg-core";
1+
import { int, mysqlTable, text, varchar } from "drizzle-orm/mysql-core";
22

3-
export const guilds = pgTable("guilds", {
4-
id: text("id").primaryKey(),
3+
export const guilds = mysqlTable("guilds", {
4+
id: varchar("id", { length: 255 }).primaryKey(),
55
name: text("name"),
66
});
77

8-
export const channels = pgTable("channels", {
9-
id: text("id").primaryKey(),
8+
export const channels = mysqlTable("channels", {
9+
id: varchar("id", { length: 255 }).primaryKey(),
1010
name: text("name"),
11-
guildId: text("guild_id").references(() => guilds.id),
12-
count: integer("count").default(0),
11+
guildId: varchar("guild_id", { length: 255 }).references(() => guilds.id),
12+
count: int("count").default(0),
1313
lastUserId: text("last_user_id"),
1414
});

bot/src/commands/configuration/channels.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ export default {
5252
return interaction.followUp(
5353
`Added ${channel} as a counting channel.`,
5454
);
55-
} else {
56-
return interaction.followUp(err.message);
55+
} else if (err.message === "Channel already exists") {
56+
return interaction.followUp(
57+
`${channel} has already been added as a counting channel.`,
58+
);
5759
}
5860
}
61+
62+
console.error(err);
5963
}
6064
}
6165
}

bun.lockb

4.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)