0.4.0b1 add database and /userinfo (incomplete)
This commit is contained in:
parent
3f13fe1ec0
commit
5b98a2e98a
18 changed files with 1194 additions and 243 deletions
37
src/db/users.ts
Normal file
37
src/db/users.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { eq } from "drizzle-orm";
|
||||
import { db } from "./database";
|
||||
import { users } from "./schema";
|
||||
|
||||
|
||||
export type UserUpdate = {
|
||||
id: string | bigint,
|
||||
username: string
|
||||
globalName: string
|
||||
};
|
||||
|
||||
export type NewUser = typeof users.$inferInsert;
|
||||
|
||||
export async function findUser (userData: UserUpdate) {
|
||||
let user = await db.query.users.findFirst({
|
||||
where: () => eq(users.discordId, BigInt(userData.id))
|
||||
}) as NewUser;
|
||||
|
||||
if (!user) {
|
||||
// upsert the user
|
||||
user = await db.insert(users).values({
|
||||
discordId: BigInt(userData.id),
|
||||
username: userData.username,
|
||||
displayName: userData.globalName,
|
||||
} as NewUser).returning() as NewUser;
|
||||
} else if (
|
||||
user.username !== userData.username ||
|
||||
user.displayName !== userData.globalName
|
||||
) {
|
||||
await db.update(users).set({
|
||||
username: userData.username,
|
||||
displayName: userData.globalName
|
||||
}).where(eq(users.discordId, BigInt(userData.id)))
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue