From ebb481e129438d9f5b84a4cbe01f186ee01b0ea9 Mon Sep 17 00:00:00 2001 From: Alex Wang Date: Fri, 11 Oct 2024 15:26:46 -0700 Subject: [PATCH 1/3] basic supabase queries to add and remove trees --- src/supabase/queries.ts | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/supabase/queries.ts diff --git a/src/supabase/queries.ts b/src/supabase/queries.ts new file mode 100644 index 0000000..ddaa37b --- /dev/null +++ b/src/supabase/queries.ts @@ -0,0 +1,52 @@ +import { supabase } from './client'; + +// Function to add a single tree +export async function addTree(species: string) { + const { data, error } = await supabase.rpc('add_tree', { species }); + + if (error) { + throw new Error(`Error adding tree: ${error.message}`); + } + + return data; +} + +// Function to add multiple trees +export async function addMultipleTrees(species: string, quantity: number) { + const { data, error } = await supabase.rpc('add_multiple_trees', { + species, + quantity, + }); + + if (error) { + throw new Error(`Error adding multiple trees: ${error.message}`); + } + + return data; +} + +// Function to remove a single tree by UUID +export async function removeTree(treeId: string) { + const { data, error } = await supabase.rpc('remove_tree', { + tree_id: treeId, + }); + + if (error) { + throw new Error(`Error removing tree: ${error.message}`); + } + + return data; +} + +// Function to remove multiple trees by a list of UUIDs +export async function removeMultipleTrees(treeIds: string[]) { + const { data, error } = await supabase.rpc('remove_multiple_trees', { + tree_ids: treeIds, + }); + + if (error) { + throw new Error(`Error removing multiple trees: ${error.message}`); + } + + return data; +} \ No newline at end of file From bea151a2d8ac7b45e60084a19ee4388601fe6845 Mon Sep 17 00:00:00 2001 From: Alex Wang Date: Fri, 11 Oct 2024 19:53:14 -0700 Subject: [PATCH 2/3] complete testing of query functions, change uuid handling of multiple trees --- src/supabase/queries.ts | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/supabase/queries.ts b/src/supabase/queries.ts index ddaa37b..57b5896 100644 --- a/src/supabase/queries.ts +++ b/src/supabase/queries.ts @@ -2,51 +2,44 @@ import { supabase } from './client'; // Function to add a single tree export async function addTree(species: string) { - const { data, error } = await supabase.rpc('add_tree', { species }); + const { error } = await supabase.rpc('add_tree', { species }); if (error) { throw new Error(`Error adding tree: ${error.message}`); } - - return data; } // Function to add multiple trees -export async function addMultipleTrees(species: string, quantity: number) { - const { data, error } = await supabase.rpc('add_multiple_trees', { - species, - quantity, +export async function addMultipleTrees( + trees: { species: string; quantity: number }[], +) { + const { error } = await supabase.rpc('add_multiple_trees', { + trees: JSON.stringify(trees), }); if (error) { throw new Error(`Error adding multiple trees: ${error.message}`); } - - return data; } // Function to remove a single tree by UUID export async function removeTree(treeId: string) { - const { data, error } = await supabase.rpc('remove_tree', { - tree_id: treeId, + const { error } = await supabase.rpc('remove_tree', { + tree_uuid: treeId, }); if (error) { throw new Error(`Error removing tree: ${error.message}`); } - - return data; } // Function to remove multiple trees by a list of UUIDs export async function removeMultipleTrees(treeIds: string[]) { - const { data, error } = await supabase.rpc('remove_multiple_trees', { - tree_ids: treeIds, + const { error } = await supabase.rpc('remove_multiple_trees', { + p_tree_ids: treeIds, }); if (error) { throw new Error(`Error removing multiple trees: ${error.message}`); } - - return data; -} \ No newline at end of file +} From 31352e09883c43b25b48f1124eb151b4378a87c5 Mon Sep 17 00:00:00 2001 From: Alex Wang Date: Sat, 19 Oct 2024 12:18:36 -0700 Subject: [PATCH 3/3] refactor arguments for add_multiple_trees, rename fields --- src/supabase/queries.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/supabase/queries.ts b/src/supabase/queries.ts index 57b5896..3f44ceb 100644 --- a/src/supabase/queries.ts +++ b/src/supabase/queries.ts @@ -10,11 +10,10 @@ export async function addTree(species: string) { } // Function to add multiple trees -export async function addMultipleTrees( - trees: { species: string; quantity: number }[], -) { +export async function addMultipleTrees(species: string, quantity: number) { const { error } = await supabase.rpc('add_multiple_trees', { - trees: JSON.stringify(trees), + species: species, + quantity: quantity, }); if (error) { @@ -36,7 +35,7 @@ export async function removeTree(treeId: string) { // Function to remove multiple trees by a list of UUIDs export async function removeMultipleTrees(treeIds: string[]) { const { error } = await supabase.rpc('remove_multiple_trees', { - p_tree_ids: treeIds, + tree_uuids: treeIds, }); if (error) {