<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ali Asgar L. Laut</title>
    <description>The latest articles on Forem by Ali Asgar L. Laut (@sushialii).</description>
    <link>https://forem.com/sushialii</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F707236%2F9d298124-afb7-4690-90ef-5114feb1c46e.jpeg</url>
      <title>Forem: Ali Asgar L. Laut</title>
      <link>https://forem.com/sushialii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sushialii"/>
    <language>en</language>
    <item>
      <title>You should start creating a View in Supabase!!</title>
      <dc:creator>Ali Asgar L. Laut</dc:creator>
      <pubDate>Wed, 22 May 2024 19:24:43 +0000</pubDate>
      <link>https://forem.com/sushialii/you-should-start-creating-a-view-in-supabase-4ahj</link>
      <guid>https://forem.com/sushialii/you-should-start-creating-a-view-in-supabase-4ahj</guid>
      <description>&lt;h2&gt;
  
  
  What even are Views?
&lt;/h2&gt;

&lt;p&gt;If you are new to postgres or if you are a frontend heavy developer who is currently relying on &lt;a href="https://supabase.com/" rel="noopener noreferrer"&gt;supabase&lt;/a&gt; to have a magic backend appearing out of nowhere, or maybe you are just someone who likes to read stuff and in that case I have something to share with you!&lt;/p&gt;

&lt;p&gt;Start.👏 Using.👏 &lt;strong&gt;Views&lt;/strong&gt;.👏&lt;/p&gt;

&lt;p&gt;If you do not know what &lt;strong&gt;Views&lt;/strong&gt; are, then here is a link from &lt;a href="https://www.postgresql.org/docs/current/tutorial-views.html" rel="noopener noreferrer"&gt;postgres documentation&lt;/a&gt;. Well basically I like to describe Views as a mini table for your query. That means if you have a query with a long ass complex joins statements, and it is something that you know will be needed by multiple pages on your frontend. Then VOIILAAA! encapsulate it inside a View!&lt;/p&gt;

&lt;h2&gt;
  
  
  What even is the Syntax?
&lt;/h2&gt;

&lt;p&gt;Remember the &lt;strong&gt;SELECT&lt;/strong&gt; statement from Postgres?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq6tdjsbu94sw7cfa2dp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq6tdjsbu94sw7cfa2dp.png" alt="Image description" width="260" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well you can just add &lt;code&gt;CREATE VIEW view_name AS&lt;/code&gt; before the SELECT statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztwt5geejbfk1znjz3uj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztwt5geejbfk1znjz3uj.png" alt="Image description" width="311" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you go! you have now created a View!&lt;br&gt;
I am preaching this out for those people who are using or is planning to use Supabase since they made it very easy to use!&lt;br&gt;
Once you have a view ready, you will be able to query it on your frontend like how you query a table. Real Simple!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const { data, error } = await supabase.from('user_listings').select('*')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sweet right?&lt;br&gt;
Basically you just RAW SQL'd it with VIEW style!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait! There's more!
&lt;/h2&gt;

&lt;p&gt;Views are really powerful especially with Supabase Auth. You can combine using &lt;em&gt;auth.uid()&lt;/em&gt; on your query and do it like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feshutsew1krlof3rknxq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feshutsew1krlof3rknxq.png" alt="Image description" width="370" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, you don't have to worry about authentication. The user_profile VIEW above will query only the authenticated user's data if the user is authenticated. That means if the user is not authenticated then they get nothing. Adding an extra security layer to your backend which is great!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wait! There's more?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now if we create more Views inside Supabase and use it to fetch data into your front-end application then that would mean tables will no longer be relevant for READ actions.&lt;/p&gt;

&lt;p&gt;and the answer to that is YES! we want it this way!&lt;br&gt;
This is what I plan to preach anyway. :D&lt;/p&gt;

&lt;p&gt;If you want to create Views for fetching most of the data that you need in your frontend application. Then let's take one step further!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove all READ access to your tables using &lt;a href="https://supabase.com/docs/guides/database/postgres/row-level-security" rel="noopener noreferrer"&gt;Row Level Security&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Set READ access accordingly based on user roles on the Views that you created.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you have another layer of security!&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok Now I'll stop
&lt;/h2&gt;

&lt;p&gt;Hopefully I convinced you to create Views in Supabase!&lt;/p&gt;

&lt;p&gt;In conclusion, Views are great as you can store your SQL query inside it and will act as table for you to access in your frontend application. Combining with Supabase Auth, you will have a great time and will no longer worry about the user's authentication status when calling it from your frontend. Lastly, you can protect your tables by removing all READ access unto it and instead give the users access to the Views instead.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>supabase</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
