import { type ResultSetHeader, type RowDataPacket, type QueryOptions } from "mysql2" import pool from 'miscellaneous/db' interface Media extends RowDataPacket { id?: number; code?: number; title?: string; released?: string; webImg?: string; } export enum Table { movies = "movies", series = "series", games = "games", } async function save(table: Table, code: number, title: string, released: string, webImg: string): Promise { try { const options: QueryOptions = { sql: "INSERT INTO " + table + " (code, title, released, webImg) VALUES (?,?,?,?)", values: [code, title, released, webImg] }; const [result, fields] = await pool.query(options); return result.affectedRows; } catch (err) { console.log(err); } return 0; } async function findOneAndDelete(table: Table, code: number): Promise { try { const [result, fields] = await pool.query("DELETE FROM " + table + " WHERE code = ?;", [code]); return result.affectedRows; } catch (err) { console.log(err); } return 0; } async function findOne(table: Table, code: number): Promise { try { const [rows, fields] = await pool.query("SELECT * FROM " + table + " WHERE code = ?;", [code]); return rows; } catch (err) { console.log(err); } return []; } async function find(table: Table): Promise { try { const [rows, fields] = await pool.query("SELECT * FROM " + table + ";"); return rows; } catch (err) { console.log(err); } return []; } export default { save, findOneAndDelete, findOne, find };