Skip to content

Instantly share code, notes, and snippets.

@pntrivedy
Created November 4, 2023 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pntrivedy/957dcefcf361a258ef731936869cba7d to your computer and use it in GitHub Desktop.
Save pntrivedy/957dcefcf361a258ef731936869cba7d to your computer and use it in GitHub Desktop.
Get data from supabase from current date to specific days, weeks, months in future.
import supbase, dayjs and apiService
function
----
const result = await apiService.fetchDataWithDate("TABLE NAME", {
field: "d", // options: d for days, w for week, M for month
value: 7, // number value. Given example will fetch rows within 7 days from current date
});
----
async fetchDataWithDate(
endpoint: string,
filter: { field: string; value: number }
): Promise<any[]> {
// Handle array of strings as filter
try {
const startDate = dayjs().format("YYYY-MM-DD HH:mm:ss");
const endDate = dayjs()
.add(filter.value, filter.field)
.format("YYYY-MM-DDTHH:mm:ss");
const { data, error } = await supabase
.from(endpoint)
.select("*")
.gt("due_at", startDate) // Update 'due_at' with column name with date value
.lt("due_at", endDate); // Update 'due_at' with column name with date value
if (error) {
throw error;
}
return data;
} catch (error: any) {
console.error("Error fetching data with date:", error.message);
throw error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment