pkgs/db/src/utils/guild.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import { eq } from "drizzle-orm";
import db, { guilds } from "..";
export async function upsertGuild(guild: { discordId: string; name: string; icon: string | null }) {
const [upsertedGuild] = await db
.insert(guilds)
.values({
discordId: guild.discordId,
name: guild.name,
icon: guild.icon,
createdAt: new Date(),
updatedAt: new Date(),
})
.onConflictDoUpdate({
target: guilds.discordId,
set: {
name: guild.name,
icon: guild.icon,
updatedAt: new Date(),
},
})
.returning();
return upsertedGuild;
}
export async function getGuild(id: number) {
const [result] = await db.select().from(guilds).where(eq(guilds.id, id));
return result;
}
export async function getGuildByDiscordId(discordId: string) {
const [result] = await db.select().from(guilds).where(eq(guilds.discordId, discordId));
return result;
}
export async function updateGuild(id: number, guild: Partial<typeof guilds.$inferInsert>) {
const [updatedGuild] = await db
.update(guilds)
.set({ ...guild, updatedAt: new Date() })
.where(eq(guilds.id, id))
.returning();
return updatedGuild;
}
|