Skip to content

Commit

Permalink
Merge pull request #20 from fac30/database-crud
Browse files Browse the repository at this point in the history
feat: updated and added crud fcuntions for databse
  • Loading branch information
Gaajia authored Dec 17, 2024
2 parents 1c2109c + c070c5e commit 98ca449
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
82 changes: 82 additions & 0 deletions src/functions/databaseFunctions.tsx
Original file line number Diff line number Diff line change
@@ -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<string, number>) => {
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<string, number>,
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 };
15 changes: 0 additions & 15 deletions src/functions/outfit_items.tsx

This file was deleted.

0 comments on commit 98ca449

Please sign in to comment.