mirror of
https://gitlab.com/qwerinope/qweribot.git
synced 2026-02-04 09:26:58 +01:00
moving to postgres, part 1
This commit is contained in:
@@ -1,58 +1,47 @@
|
||||
import pocketbase, { type userRecord } from "db/connection";
|
||||
import { emptyInventory, itemarray } from "items";
|
||||
import db from "db/connection";
|
||||
import { users } from "db/schema";
|
||||
import { itemarray, type inventory } from "items";
|
||||
import type User from "user";
|
||||
import logger from "lib/logger";
|
||||
|
||||
const pb = pocketbase.collection('users');
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
/** Use this function to both ensure existance and to retreive data */
|
||||
export async function getUserRecord(user: User): Promise<userRecord> {
|
||||
try {
|
||||
const data = await pb.getOne(user.id);
|
||||
export async function getUserRecord(user: User) {
|
||||
const data = await db.query.users.findFirst({ where: eq(users.id, parseInt(user.id)) });
|
||||
if (!data) return createUserRecord(user);
|
||||
|
||||
if (Object.keys(data.inventory).sort().toString() !== itemarray.sort().toString()) { // If the items in the user inventory are missing an item.
|
||||
itemarray.forEach(key => {
|
||||
if (!(key in data.inventory)) data.inventory[key] = 0;
|
||||
});
|
||||
};
|
||||
|
||||
return data;
|
||||
} catch (err) {
|
||||
// This gets triggered if the user doesn't exist in the database
|
||||
return await createUserRecord(user);
|
||||
if (Object.keys(data.inventory).sort().toString() !== itemarray.sort().toString()) { // If the items in the user inventory are missing an item.
|
||||
itemarray.forEach(key => {
|
||||
if (!(key in data.inventory)) data.inventory[key] = 0;
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
export async function getAllUserRecords(): Promise<userRecord[]> {
|
||||
return await pb.getFullList();
|
||||
};
|
||||
|
||||
async function createUserRecord(user: User): Promise<userRecord> {
|
||||
const data = await pb.create({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
balance: 0,
|
||||
inventory: emptyInventory,
|
||||
lastlootbox: new Date(0).toISOString()
|
||||
});
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export async function updateUserRecord(user: User, newData: userRecord): Promise<boolean> {
|
||||
try {
|
||||
await pb.update(user.id, newData);
|
||||
return true;
|
||||
} catch (err) {
|
||||
logger.err(err as string);
|
||||
return false;
|
||||
};
|
||||
export async function getAllUserRecords() {
|
||||
return await db.select().from(users);
|
||||
};
|
||||
|
||||
async function createUserRecord(user: User) {
|
||||
return await db.insert(users).values({
|
||||
id: parseInt(user.id),
|
||||
username: user.username
|
||||
}).returning().then(a => {
|
||||
if (!a[0]) throw Error('Something went horribly wrong');
|
||||
return a[0]
|
||||
});
|
||||
};
|
||||
|
||||
export type balanceUpdate = { balance: number; };
|
||||
export type inventoryUpdate = { inventory: inventory; };
|
||||
type updateUser = balanceUpdate | inventoryUpdate;
|
||||
|
||||
export async function updateUserRecord(user: User, newData: updateUser) {
|
||||
await db.update(users).set(newData).where(eq(users.id, parseInt(user.id)));
|
||||
return true;
|
||||
};
|
||||
|
||||
export async function getBalanceLeaderboard() {
|
||||
try {
|
||||
return await pb.getList(1, 10, { sort: '-balance,id' }).then(a => a.items);
|
||||
} catch (err) {
|
||||
logger.err(err as string);
|
||||
};
|
||||
return await db.select().from(users).orderBy(desc(users.balance)).limit(10);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user