Skip to content

Commit

Permalink
chore: bite me
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed Jun 14, 2024
1 parent e104b74 commit 564d0e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
29 changes: 11 additions & 18 deletions src/lib/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import type { Member } from '@prisma/client';
import { container } from '@sapphire/framework';

interface RawMember {
guild_id: string;
regular_expressions: string[] | null;
user_id: string;
words: string[] | null;
}

interface RawIgnored {
ignored_channels: null[] | string[] | null;
ignored_users: null[] | string[] | null;
}

export interface FullMember extends Member {
ignoredChannels: string[];
ignoredUsers: string[];
Expand All @@ -21,16 +9,21 @@ export interface FullMember extends Member {
export async function getDatabaseMember(guildId: string, userId: string): Promise<FullMember> {
const [_, [rawMember], [rawIgnored]] = await container.prisma.$transaction([
container.prisma.$queryRaw`INSERT INTO users (id) VALUES (${userId}) ON CONFLICT (id) DO NOTHING`,
// eslint-disable-next-line @ts-safeql/check-sql
container.prisma.$queryRaw<[RawMember]>`
container.prisma.$queryRaw<
{
guild_id: string | null;
regular_expressions: string[] | null;
user_id: string | null;
words: string[] | null;
}[]
>`
INSERT INTO members (guild_id, user_id)
VALUES (${guildId}, ${userId})
ON CONFLICT (guild_id, user_id) DO
UPDATE SET user_id = ${userId}
RETURNING *
`,
// eslint-disable-next-line @ts-safeql/check-sql
container.prisma.$queryRaw<[RawIgnored]>`
container.prisma.$queryRaw<{ ignored_channels: string[] | null; ignored_users: (string | null)[] | null }[]>`
SELECT
array_agg(guild_ignored_channels.ignored_channel_id) as ignored_channels,
array_agg(guild_ignored_users.ignored_user_id) as ignored_users
Expand All @@ -56,8 +49,8 @@ export async function getDatabaseMember(guildId: string, userId: string): Promis
}

return {
guildId: rawMember.guild_id,
userId: rawMember.user_id,
guildId: rawMember.guild_id!,
userId: rawMember.user_id!,
words: rawMember.words ?? [],
regularExpressions: rawMember.regular_expressions ?? [],
ignoredUsers,
Expand Down
2 changes: 1 addition & 1 deletion src/listeners/miscEvents/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ClientReadyListener extends Listener<typeof Events.ClientReady> {
private async ensureAllGuildsAreInDatabase() {
const { prisma, client } = this.container;

// eslint-disable-next-line @ts-safeql/check-sql -- This is validated to work but its SO JANK
// eslint-disable-next-line -- This is validated to work but its SO JANK
await prisma.$executeRaw`INSERT INTO guilds (guild_id) VALUES ${Prisma.join(
[...client.guilds.cache.keys()],
'), (',
Expand Down

0 comments on commit 564d0e5

Please sign in to comment.