Created
November 4, 2023 11:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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