Does supabase have any triggers on storage ?
# off-topic
d
Does supabase have any triggers on storage ?
s
Thanks @User
You can create a database function to do something when an insert happens on the
storage.objects
table
d
Where can I find that table?
s
Scroll down on that page you posted the screenshot of
a
um sorry bit off-topic why i don't have function hooks? options 🤔
s
I'm not sure, maybe log out and log back into the dashboard and see if it shows
d
Yes but I don't have the table you mentioned
only 2 options
a
nope, didn't work
s
Ok so you would setup the trigger based on those tables then
d
Does that mean I upload image to storage and also add a row in db to triggers something ?
s
Whenever you upload to storage a row is automatically created in the db
Well in the storage.objects table
d
I wonder I can't see that table....
s
Its in a schema not exposed to the public
d
now there's no option to type that table name
only the dropdown menu
s
One second, I have a trigger setup on one of my project for this, let me dig it out
d
Thanks
s
So you will be using the
images
table to keep a reference of your files in
storage
right?
d
yes after I upload them in
images
bucket
My first project had
auth_logs
table too which was never added in this project..
s
So I created a function with raw SQL using the SQL editor in the dashboard
Copy code
sql
-- inserts a row into public.images
create function public.handle_add_storage()
returns trigger as $$
begin
    insert into public.images (id, name)
    values (new.id, new.name);
    return new;
end;
$$ language plpgsql security definer;
Then every time an object gets uploaded I call this trigger
Copy code
sql
-- trigger the function every time a file is uploaded
create trigger on_file_created
    after insert on storage.objects
    for each row execute procedure public.handle_add_storage();
All of these can be created through the SQL editor
d
Hmmm, new to SQL so I'll have to try them
Thanks for the help
s
So the second one is the one that triggers when something happens in the
storage.objects
table, this table is updated each time a new storage upload happens
d
Thanks, I'll try creating one with the UI first before SQL editor
s
I don't think you will have access to the
storage.objects
table from the UI as it's not in the
public
schema
d
tbh my first project had default tables namely storage, auth_logs and something else. I wonder by this project didn't had one
s
It has them, they are just not exposed in the dashboard
d
Okay.... still curios why they were in first 😅
I also deleted them and wasn't able to do anything then
so I created new project
s
Please don't delete such tables, this is probably why they have been hidden to prevent users from easily doing this
d
Okay 🤔 they updated is within a day lol xD
e
Btw, hooks are only rolled out to new projects as of now. Over the next few weeks, we will roll it out to existing projects
which is why you might only see two options in that section
d
I just created that project yesterday
e
oh just catching up on the messages in this thread..i see that you are able to access the section, but you aren't able to see the storage.objects table..yup we only show public schema tables here ..for security reasons we don't show auth and storage..i haven't spent much time thinking about how to expose storage triggers, but this is definitely something we will launch
d
Can you confirm if
auth_logs
can be seen? I was able to see that in my first project and there was
storage
table too. In new project there were not any default tables.
e
these tables shouldn't be visible in the dashboard..there are still present, you can use a postgres client to connect to the db directly to view those tables. Some postgres clients are mentioned here - https://supabase.io/docs/guides/self-hosting#before-you-begin
but as @User mentioned, please don't modify these tables, since if you do, functionality and upgrades will break.
d
Yes, thanks!
z
Hey @silentworks, I follow your advices here but I came across an issue: I have a "bayesgames" table. I want to trigger add a row for each .json file I upload, but I can't seem to get the values to work. here is my function:
Copy code
sql
create function public.handle_add_storage()
returns trigger as $$
begin
    insert into public.bayesgames (id, name, type, size, last_modified, data, inserted_at)
    values (new.id, new.name, new.type, new.size, new.last_modified, new.data, new.inserted_at);
    return new;
end;
$$ language plpgsql security definer;
and here is my trigger:
Copy code
sql
create trigger on_file_created
    after insert on storage.objects
    for each row execute procedure public.handle_add_storage();
My error is this one:
s
I think the error is stating the issue here, it says there is no size property on the object you are storing. You will need to first figure out what properties are available.
z
It is a json file, what are the properties available for this type of files and is it possible for me to get the data inside of it ? Thanks for your help !
s
I'm not sure as I have never uploaded a json file before.
z
Alright I will check then, hopefully I can get the data of the json into a column thanks 🙏
s
You won't be able to get the content of the file as that is just a blog that is uploaded to storage. You can only get access to properties that are available on the file itself.
z
Basically I want to map through json data on my React App after Uploading a json file on my site, is there a way for me to access that data from the front of my app if this is not possible ?
s
Can you ask the question in #1006358244786196510 while providing as much information about what you are trying to accomplish there as others might be able to help too. This thread is old and only people who are in it will be seeing your question.
z
will do thanks