Hi guys, I think that I’m going crazy, there is so...
# javascript
g
Hi guys, I think that I’m going crazy, there is something about the doc that I must not understand correctly. How would you convert this query
Copy code
SELECT urls.id, urls.name, array_agg(tags) as tags
    from urls
    join urls_tags on urls.id = urls_tags.url_id
    join tags on tags.id = urls_tags.tag_id
    group by urls.id
To a supabase query ? I came up with that but it’s not complete:
Copy code
await db.from(`urls_tags`).select(`urls:url_id ( id, name ), tags:tag_id ( id, name )`)
s
You can create a view from your query and query it the same as you would a table
g
Thank you !
Do you have an example by any chance ?
j
you need to put your SQL query into a function
Copy code
CREATE FUNCTION my_function ...
<your query here>
then
supabase.rpc('my_function')
s
For what you are after here a view is probably a better option
Copy code
sql
CREATE VIEW public.url_tags_grouped AS
SELECT urls.id, urls.name, array_agg(tags) as tags
    from urls
    join urls_tags on urls.id = urls_tags.url_id
    join tags on tags.id = urls_tags.tag_id
    group by urls.id
Now you can call it using
Copy code
js
await supabase.from('url_tags_grouped').select('*')
g
Thanks guys