From c070c5e74a97eb3c362c39dabb9610ec94c82823 Mon Sep 17 00:00:00 2001 From: William Man Date: Tue, 17 Dec 2024 11:26:04 +0000 Subject: [PATCH] feat: updated and added crud fcuntions for databse --- src/functions/databaseFunctions.tsx | 82 +++++++++++++++++++++++++++++ src/functions/outfit_items.tsx | 15 ------ 2 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 src/functions/databaseFunctions.tsx delete mode 100644 src/functions/outfit_items.tsx diff --git a/src/functions/databaseFunctions.tsx b/src/functions/databaseFunctions.tsx new file mode 100644 index 0000000..4e839c9 --- /dev/null +++ b/src/functions/databaseFunctions.tsx @@ -0,0 +1,82 @@ +import supabase from "../utils/supbaseClient"; + +const fetchItems = async ( + table: string, + columns: string[] = [], + orderBy: string = "id", + ascend: boolean = true +) => { + try { + const { data, status, statusText, error } = await supabase + .from(table) + .select(columns.join(",")) + .order(orderBy, { ascending: ascend }); + if (error) { + throw new Error(`There was an error fetching the data from: ${table}`); + } + return { data, status, statusText }; + } catch (err) { + return err.message; + } +}; + +const insertItems = async (table: string, newData: Record) => { + try { + const { status, statusText, error } = await supabase + .from(table) + .insert(newData); + if (status !== 201 && error) { + throw new Error( + `There was an error inserting new data into the table: ${table}` + ); + } + return { status, statusText }; + } catch (err) { + return err.message; + } +}; + +const updateItems = async ( + table: string, + newData: Record, + where: string, + whereValue: string | number +) => { + try { + const { status, statusText, error } = await supabase + .from(table) + .update(newData) + .eq(where, whereValue); + if (status !== 204 && error) { + throw new Error( + `There was an error updating the row where ${where} is ${whereValue}, in table ${table}` + ); + } + return { status, statusText }; + } catch (err) { + return err.message; + } +}; + +const deleteItems = async ( + table: string, + where: string, + whereValue: string | number +) => { + try { + const { status, statusText, error } = await supabase + .from(table) + .delete() + .eq(where, whereValue); + if (status !== 204 && error) { + throw new Error( + `There was an error deleting the row from table ${table}, where ${where} is ${whereValue}` + ); + } + return { status, statusText }; + } catch (err) { + return err.message; + } +}; + +export { fetchItems, insertItems, updateItems, deleteItems }; diff --git a/src/functions/outfit_items.tsx b/src/functions/outfit_items.tsx deleted file mode 100644 index c08cdfa..0000000 --- a/src/functions/outfit_items.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import supabase from "../utils/supbaseClient"; - -const fetchAllOutfitItems = async () => { - try { - const { data, error } = await supabase.from("outfit_items").select(); - if (error) { - throw new Error(error.message); - } - return data; - } catch (err) { - return "There was an error fetching outfit items: " + err.message; - } -}; - -export { fetchAllOutfitItems };