Update userData to sql
This commit is contained in:
@@ -1,13 +1,58 @@
|
||||
import mongoose, { Schema } from 'mongoose';
|
||||
import { type ResultSetHeader, type RowDataPacket } from "mysql2"
|
||||
import pool from '../miscellaneous/db'
|
||||
|
||||
const userSchema = new Schema({
|
||||
'pass': String,
|
||||
'omdb_key': String,
|
||||
'twitch_client_id': String,
|
||||
'twitch_client_secret': String,
|
||||
'mac_address': String,
|
||||
});
|
||||
interface UserD extends RowDataPacket {
|
||||
name?: string;
|
||||
value?: string;
|
||||
}
|
||||
|
||||
const User = mongoose.model('user', userSchema);
|
||||
export enum values {
|
||||
pass = 1,
|
||||
omdb_key,
|
||||
twitch_client_id,
|
||||
twitch_client_secret,
|
||||
mac_address
|
||||
}
|
||||
|
||||
export default User;
|
||||
const namesOfValues: string[] = ["", "pass", "omdb_key", "twitch_client_id", "twitch_client_secret", "mac_address"];
|
||||
|
||||
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
|
||||
};
|
||||
|
Reference in New Issue
Block a user