pkgs/db/src/utils/index.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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import { eq } from "drizzle-orm";
import db, { actors, sessions } from "..";
export async function getUser(id: number) {
const [result] = await db.select().from(actors).where(eq(actors.id, id));
return result;
}
export async function getUserByDiscordId(discordId: string) {
const [result] = await db.select().from(actors).where(eq(actors.discordId, discordId));
return result;
}
export async function getUserSessions(id: number) {
const result = await db.select().from(sessions).where(eq(sessions.userId, id));
return result;
}
export async function getSession(id: string) {
const [result] = await db.select().from(sessions).where(eq(sessions.id, id));
return result;
}
export async function upsertUser(user: {
username: string;
displayName: string | null;
id: string;
avatar: string | null;
}) {
const [upsertedUser] = await db
.insert(actors)
.values({
discordId: user.id,
username: user.username,
displayName: user.displayName,
avatar: user.avatar,
createdAt: new Date(),
updatedAt: new Date(),
})
.onConflictDoUpdate({
target: actors.discordId,
set: {
username: user.username,
displayName: user.displayName,
avatar: user.avatar,
updatedAt: new Date(),
},
})
.returning();
return upsertedUser;
}
export async function anonymiseUser(id: number) {
const result = await db
.update(actors)
.set({
username: `Anonymised User`,
displayName: null,
avatar: null,
updatedAt: new Date(),
})
.where(eq(actors.id, id))
.returning();
return result;
}
|