Im making a recipe website with nextjs and supabase. I have an occasions table which has a foreign key to many recipes. I can easily query the occasions for all recipe in that occasion however I want to paginate so I need a count and a range. How would I go about doing this or would I have to make an RPC instead?
Here is my current query that returns all recipes in an occasion – comments in the query is what I want to put a count/range on
.from("occasions")
.select(
`*,
recipes_occasions(
recipes(
// I WANT TO GET EXACT COUNT OF ALL RECIPES IN AN OCCASION HERE
recipeId, title, slug, totalTime, yield, ingredients,
recipes_categories (
categories(title, slug)
),
recipes_diets (
diets(title, slug)
),
recipes_cuisines (
cuisines(title, slug, code)
)
),
)`
)
.eq("slug", slug )
.range(0,4)
// I WANT TO TAKE SPECIFIC RANGE HERE (e.g. range(12, 24) or range(200, 212)
2
Answers
I managed to get the range working by first getting the occasionID from the slug and then another query to get the recipes with that occasion ID like so. Added count to destructure also.
Result in my terminal
There are multiple ways you could solve this problem. Splitting it up to two separate queries is certainly one option.
Another option might be to flip the query and get the recipes along with it’s associated occasions.
count: 'exact'
will get the number of recipes so that you can use it to paginate.The
!inner
keyword used afteroccasions
allows you to filter the results ofrecipes
selected based on values ofoccasions
.— Edit above fixed recipe typo