la fusew
02/15/2023, 4:18 PMlildripka
02/15/2023, 4:55 PMcreate or replace function test()
returns setof table("country" countries.country_name, "location" locations.street_address, "postal_code" locations.postal_code)
language sql
as $$
select
countries.country_name,
locations.street_address,
locations.postal_code
from locations
join countries using (country_id)
where countries.region_id = 1
order by countries.country_name asc;
$$;
I also tried to execute it without brackets after table, but it didn't help. So, how to define return type correctly (ideally with custom column names)?DYELbrah
02/15/2023, 5:22 PMfast
02/15/2023, 5:32 PMconst session = await supabase.auth.getSession();
const accessToken = session.data.session.access_token;
⬆️ This is how i get the session and the access token.
I then put the accessToken into a normal fetch request to the backend
Backend:
// getUserFromAccessToken, validates if the given access token is valid
async function getUserFromAccessToken(accessToken) {
try {
const supabase_response = await supabase.auth.getUser(accessToken);
if(supabase_response.error) {
sendError();
return;
}
return supabase_response.data.user;
} catch(e) {
sendError();
return;
}
}
⬆️ This is how I check the given access token (ofc this happens on a server not on client side)
I then take the sub ID to perform changes for the user
Also I wanted to know if I should provide any configuration to the createClient function on the server side cause I saw it sometimes on blog posts?
For example ⤵️
{
auth: {
autoRefreshToken: false,
persistSession: false
}
}
Let me know if that's secureandrevenancio
02/15/2023, 5:49 PMconst { data, error } = await supabase
.from('videos')
.select(
`
id,
name,
thumbnail,
likes:likes(user_id, video_id)
`
)
.order('created_at', { ascending: false });
if (error) throw new Error(error.message);
But this returns me ALL the users that liked the video and not if I liked it.8bitdad
02/15/2023, 5:58 PMHugos
02/15/2023, 6:04 PMmohnish
02/15/2023, 6:11 PMrchrdnsh
02/15/2023, 6:40 PMjs
let imageUrl;
const downloadImage = async (path: string) => {
try {
const { data, error } = await supabase.storage.from('ads').download(path)
if (error) {
throw error
}
const url = URL.createObjectURL(data)
imageUrl = url
} catch (error) {
if (error instanceof Error) {
console.log('Error downloading image: ', error.message)
}
}
}
..and it works, but I can't seem to figure out how to make function reusable. The imageUrl variable cannot seem to be swapped out, like so:
js
const downloadImage = async (path: string) => {
try {
const { data, error } = await supabase.storage.from('ads').download(path)
let image;
if (error) {
throw error
}
const url = URL.createObjectURL(data)
image = url
return image
} catch (error) {
if (error instanceof Error) {
console.log('Error downloading image: ', error.message)
}
}
}
I get error:mohnish
02/15/2023, 7:15 PMfrom supabase import create_client, Client
from supabase.lib.client_options import ClientOptions
client_options = ClientOptions(timeout=60)
supabase = create_client(SUPABASE_URL, SUPABASE_ANON_KEY, options=client_options)
However, the function still times out after 5 seconds (the extra 1 second is from the first query run). I am also just doing a simple query querying 5-6 columns from a table.
Any idea how I can fix this?Atrox
02/15/2023, 7:26 PMSELECT name FROM countries LIMIT 1 OFFSET 0
nateland
02/15/2023, 8:08 PMKen
02/15/2023, 8:42 PMRuzelmania
02/15/2023, 9:06 PMinvalid input syntax for type bigint: "id"
devx101
02/15/2023, 9:42 PMCREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'test.example.com', dbname 'testdb', port '5432');
However this command fails with the postgresql error:
Query 1 ERROR: ERROR: permission denied for foreign-data wrapper postgres_fdw
I'm connected to the supabase db with the postgres user, but it appears the postgres_fdw extension is assigned to the supabase_admin
user. Is this why I cannot create a foreign data wrapper server? If so, how can I fix this?AlanK
02/15/2023, 10:05 PMRasta
02/15/2023, 10:34 PM<script>
import {page} from '$app/stores'
import { onMount } from 'svelte'
import { supabaseClient } from '$lib/supabase';
let found = false;
const getProfile = async () => {
try {
const {data, error} = await supabaseClient
.from('profiles')
.select('username')
.eq('username', $page.params.username )
.single()
if (data) {
console.log(data);
found = true
}
if (error) {
console.log(error);
}
} catch (error) {
if (error instanceof Error) {
alert(error.message)
}
}
}
getProfile()
</script>
{#if found}
<h1>User found</h1>
{:else}
<h1>User NOT found</h1>
{/if}
this works but not sure if is the best way to do it, the page shows the 'User NOT found' message for a few ms before is confirmed the username exists.
is there a proper guide for checking the username and slugs in general or this is a good approach to it?user8923
02/15/2023, 10:53 PMRake
02/15/2023, 11:04 PMSolemensis
02/15/2023, 11:08 PMAtrox
02/15/2023, 11:49 PMsql
create or replace function authorize_by_domain(profile_id uuid, workspace_id uuid) returns boolean as $$
declare
user_email text;
domain text;
workspace_domain text;
domain_enabled boolean;
begin
select email from profiles where id = $1 into user_email;
select substring(user_email from position('@' in user_email) + 1) into domain;
select email_domain, email_domain_enabled into workspace_domain, domain_enabled from workspaces where id = $2;
return domain_enabled and (domain = workspace_domain);
end;
$$ language plpgsql;
I have the function above to enable joining a workspace based on a user's email and am using this in an RLS policy like authorize_by_domain(auth.uid(), id)
but it's causing a stack overflow and I can't figure out why.JustPatrick
02/16/2023, 1:14 AMUnknown Member
02/16/2023, 2:35 AMuser_requests_1
table. Is this table persisting, and will this policy cause problems? Hope to have any advice. thanks! ❤️ Supabase ❤️
((auth.uid() = user_id) AND (NOT (listing_id IN ( SELECT user_requests_1.listing_id
FROM user_requests user_requests_1
WHERE ((user_requests_1.user_id = auth.uid()) AND (user_requests_1.request_status = ANY (ARRAY[0, 1, 2])))))))
Ken
02/16/2023, 2:49 AMKenneth J Hughes
02/16/2023, 3:07 AMUncaught SyntaxError: The requested module './../../../../cross-fetch/dist/node-ponyfill.js' does not provide an export named 'default' (at PostgrestBuilder.ts:1:8)
Any ideas what I might be doing wrong?rlee128
02/16/2023, 3:10 AMredlumxn
02/16/2023, 4:42 AMALL
events.
FYI - when RLS is disabled on the table, I do get all the events.
Our current setup involves the minting of custom JWT with additional claims (see below). We are using Supabase Project JWT Secret
to sign it.
json
{
"sub": "google-apps|user@googleworkspace.com",
"role": "authenticated",
"aud": "authenticated",
"userId": "google-apps|user@googleworkspace.com",
"iat": 1676519049,
"exp": 1676522649
}
The RLS policy looks like this:
sql
CREATE POLICY "ALL"
ON public.test_table
AS PERMISSIVE
FOR ALL
TO public
USING ((auth.user_id() = user_id));
and finally the auth.user_id
function looks like this
sql
CREATE OR REPLACE FUNCTION auth.user_id(
)
RETURNS text
LANGUAGE 'sql'
COST 100
STABLE PARALLEL UNSAFE
AS $BODY$
select nullif(current_setting('request.jwt.claims', true)::json->>'userId', '')::text;
$BODY$;
ALTER FUNCTION auth.user_id()
OWNER TO postgres;
And the actual subscription on the client side:
typescript
sbClient
.channel("test_channel")
.on(
"postgres_changes",
{
event: "*",
schema: "public",
table: "test_table",
},
(payload) => {
console.log("Receive table event...");
console.log(payload);
}
)
.subscribe((status: string, error: Error) =>
console.log({ status, error })
);
However, I get no other event ie INSERT
or UPDATE
for example. Any pointers?
P.S. I can see records being created in the realtime.subscription
table (see screenshot) and Replication
is enabled for the table (see other screenshot).Ho3einvb
02/16/2023, 5:33 AMDYELbrah
02/16/2023, 5:38 AMLA
02/16/2023, 7:18 AMnpm install
and I ran npm run dev
. I opened the studio project. It is showing connect to default project
for a long time. Did I miss any step?