personal_website/models/userModel.ts
2024-09-23 23:24:59 +02:00

58 lines
1.2 KiB
TypeScript

import { type ResultSetHeader, type RowDataPacket } from "mysql2"
import pool from 'miscellaneous/db'
interface UserD extends RowDataPacket {
name?: string;
value?: string;
}
export enum values {
pass = 1,
omdb_key,
twitch_client_id,
twitch_client_secret,
}
const namesOfValues: string[] = ["", "pass", "omdb_key", "twitch_client_id", "twitch_client_secret"];
async function getValue(name: values): Promise<string | undefined> {
try {
const [rows, fields] = await pool.query<UserD[]>("SELECT name, value FROM userData where id = ?;", [name]);
if (rows.length > 0)
return rows[0].value;
}
catch (err) {
console.log(err);
}
return;
}
async function updateValue(name: string, value: string): Promise<number> {
try {
const [result, fields] = await pool.query<ResultSetHeader>("UPDATE userData SET value = ? WHERE name = ?", [value, name]);
return result.affectedRows;
}
catch (err) {
console.log(err);
}
return 0;
}
async function getAll(): Promise<UserD[]> {
try {
const [rows, fields] = await pool.query<UserD[]>("SELECT name, value FROM userData;");
return rows;
}
catch (err) {
console.log(err);
}
return [];
}
export default {
getValue,
updateValue,
getAll,
namesOfValues
};