|
| 1 | +--- |
| 2 | +title: Database Actions |
| 3 | +toc_max_heading_level: 5 |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +# Supabase Database Actions |
| 8 | + |
| 9 | +The Supabase Database Actions allow you to **Insert, Update**, or **Delete a Row** from a Supabase table. |
| 10 | + |
| 11 | +:::note[Prerequisites] |
| 12 | +Before getting started with this section, ensure you have, |
| 13 | + |
| 14 | +1. Completed all steps in the [**Supabase setup**](..%2F..%2Fsupabase%2Fsupabase-setup.md) |
| 15 | +2. Ensure you have a table created for adding, updating, and deleting data. |
| 16 | +::: |
| 17 | + |
| 18 | + |
| 19 | +## Types of Supabase Database Actions |
| 20 | + |
| 21 | +Following are the types of actions you can perform on a Supabase table. |
| 22 | + |
| 23 | +- [**Insert Row**](#insert-row-action): Adds a new row in a table. |
| 24 | +- [**Update Row**](#update-row-action)**:** Updates a row with the specified values. |
| 25 | +- [**Delete Row**](#delete-row-action)**:** Deletes a row from a table. |
| 26 | +- [**Query Rows**](#query-rows-action): Retrieves |
| 27 | + rows from a table based on specific criteria or conditions. |
| 28 | + |
| 29 | +### Insert Row [Action] |
| 30 | + |
| 31 | +1. Select the **Widget** (e.g., Button) on which you want to define the action. |
| 32 | +5. Select **Actions** from the Properties Panel (the right menu), and click **Open**. This will |
| 33 | + open an **Action Flow Editor** in a new popup window. |
| 34 | + |
| 35 | + 1. Click on **+ Add Action**. |
| 36 | + 5. On the right side, search and select the **Supabase** > **Insert Row** action. |
| 37 | + 8. Set the **Table** to your table name (e.g., assignments). |
| 38 | + 11. Under the **Set Fields** section, click on the **+ Add Field** button. |
| 39 | + 14. Click on the Field name. |
| 40 | + |
| 41 | + 1. Scroll down to find the **Value Source** dropdown and change it to **From Variable**. |
| 42 | + 5. Click on **UNSET** and select **Widget State > Name** of the TextField. |
| 43 | + 17. Similarly, add the field for the other UI elements. |
| 44 | + |
| 45 | +:::tip[Pro Tip] |
| 46 | +While adding this action, you can leave the **id** (if marked as *Primary*) and |
| 47 | + **created_at** (if default value is `now()`) fields. Supabase will automatically add values for these fields. |
| 48 | +::: |
| 49 | + |
| 50 | +<div class="video-container"><iframe src="https://www.loom. |
| 51 | +com/embed/f2a75f9b0e144f6aab06cc1f0965541b?sid=6f57ff8a-ca2a-4c47-833d-03fa928b8301" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +### Update Row [Action] |
| 56 | + |
| 57 | +1. Select the **Widget** (e.g., Button) on which you want to define the action. |
| 58 | +5. Select **Actions** from the Properties Panel (the right menu), and click **Open**. This will |
| 59 | + open an **Action Flow Editor** in a new popup window. |
| 60 | + |
| 61 | + 1. Click on **+ Add Action**. |
| 62 | + 5. On the right side, search and select the **Supabase** > **Update Row** action. |
| 63 | + 8. Set the **Table** to your table name (e.g., assignments). |
| 64 | + 11. Optional: If you want to get the rows after the update is finished, enable the **Return Matching Rows** option. |
| 65 | + 14. Now, you must set the row you want to update. Usually, this is done by finding a row in a table that matches the current row ID. To do so, click **+ Add Filter** button inside the **Matching Rows** section. |
| 66 | + |
| 67 | + 1. Set the **Field Name** to the field that contains the IDs. Typically, this is the |
| 68 | + **id** column. |
| 69 | + 5. Set the **Relation** to **Equal To** because you want to find a row with the exact id. |
| 70 | + 8. Into the **Value Source**, you can select the **From Variable** and provide the id of the row for which you just updated values in the UI. |
| 71 | + 17. Under the **Set Fields** section, click on the **+ Add Field** button. |
| 72 | + 20. Click on the Field Name. |
| 73 | + |
| 74 | + 1. Scroll down to find the **Value Source** dropdown and change it to **From Variable**. |
| 75 | + 5. Click on **UNSET** and select **Widget State > Name** of the TextField. |
| 76 | + 23. Similarly, add the field for the other UI elements. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +:::tip[How to & Tips] |
| 83 | + |
| 84 | +<div class="video-container"><iframe src="https://www.loom. |
| 85 | +com/embed/08cb8a851350428bbc226f2e7ce9d2b3?sid=b3d097cf-6f84-4f69-893a-3a363cbf7143" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 86 | + |
| 87 | +<p></p> |
| 88 | +If you have a flow like this, *HomePage* -> *AssignmentDetailsPage* -> |
| 89 | +*UpdateAssignmentPage*, you can enable the **Replace Route** option (see point no. 5 [here](/actions/actions/navigation/navigate)) when you navigate from *AssignmentDetailsPage* to *UpdateAssignmentPage*. And then chain the [Navigate Back](/actions/actions/navigation/navigate-back#adding-navigate-back-action) action after the update action. This will directly open the *HomePage* after the row is updated. |
| 90 | +::: |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### Delete Row [Action] |
| 95 | + |
| 96 | +Go to your project page on FlutterFlow and follow the steps below to define the Action to any widget. |
| 97 | + |
| 98 | +1. Select the **Widget** (e.g., Button) on which you want to define the action. |
| 99 | +5. Select **Actions** from the Properties panel (the right menu), and click **Open**. This will open an **Action Flow Editor** in a new popup window. |
| 100 | + |
| 101 | + 1. Click on **+ Add Action**. |
| 102 | + 5. On the right side, search and select the **Supabase** -> **Delete Row** action. |
| 103 | + 8. Set the **Table** to your table name (e.g., assignments). |
| 104 | + 11. Optional: If you want to know which rows were deleted from a table, enable the **Return |
| 105 | + Matching Rows** option. |
| 106 | + 14. Now, you must set the row you want to delete. Usually, this is done by finding a row in a table that matches the current row ID. To do so, click **+ Add Filter** button inside the **Matching Rows** section. |
| 107 | + |
| 108 | + 1. Set the **Field Name** to the field that contains the IDs. Typically, this is the |
| 109 | + **id** column. |
| 110 | + 5. Set the **Relation** to **Equal To** because you want to find a row with the exact id. |
| 111 | + 8. Into the **Value Source**, you can select the **From Variable** and provide the id of the row you want to delete. |
| 112 | + |
| 113 | +<div class="video-container"><iframe src="https://www.loom. |
| 114 | +com/embed/309e0e40832146df8909c2e533e9b11e?sid=34f4ba89-cc53-47c3-8b8d-8859c03a0f6f" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 115 | + |
| 116 | +<p></p> |
| 117 | +:::tip |
| 118 | + |
| 119 | +You can chain the [**Refresh Database Request**](../refresh-db-request.md) action after this action to remove the |
| 120 | +deleted items from the list. |
| 121 | + |
| 122 | +::: |
| 123 | + |
| 124 | +### Query Rows [Action] |
| 125 | + |
| 126 | +There are certain scenarios where you may want to query a Supabase table manually. For example, you might want to only fetch data in response to a specific user action, such as clicking on a button. |
| 127 | + |
| 128 | +Additionally, if your app fetches different data under different conditions, you might find it more convenient to manually call queries. For example, you might fetch different tasks for admin and team members. |
| 129 | + |
| 130 | +To manually query a Supabase table, follow the steps below to define this action to any widget: |
| 131 | + |
| 132 | +1. Select the **Widget** (e.g., Button) on which you want to define the action. |
| 133 | + |
| 134 | +5. Select **Actions** from the Properties panel (the right menu), and click **Open**. This will open an **Action Flow Editor** in a new popup window. |
| 135 | +8. Click on **+ Add Action**. |
| 136 | +11. On the right side, search and select the **Supabase** > **Query Rows** action. |
| 137 | +14. Select the **Table** you want to query. |
| 138 | +17. You can also [Filter](#filtering-table-data) and [Order](#ordering-table-data) the query results. |
| 139 | +20. Provide the **Action Output Variable Name**. This will be used to store the query result. |
| 140 | + |
| 141 | +<p></p> |
| 142 | + |
| 143 | +<div class="video-container"><iframe src="https://www.loom. |
| 144 | +com/embed/99f47689b5b147678ed197c6ea0facf6?sid=615f2ecf-c8d0-4124-b2fa-ca9798893c92" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 145 | + |
| 146 | +<p></p> |
| 147 | + |
| 148 | + |
| 149 | +8. Now, you can use the **Action Output Variable Name** provided in the previous step to generate |
| 150 | +children from a variable on [ListView](/widgets-and-components/widgets/layout-elements/listview). |
| 151 | +5. Finally, you can display data in a [Text](/widgets-and-components/widgets/base-elements/text) widget. To do so, select the **Text widget > Properties Panel > Text > Set from Variable menu > ***[children\_from\_variable\_name]*** item > Get Row Field > select the row field** you want to display. |
| 152 | + |
| 153 | +<div class="video-container"><iframe src="https://www.loom. |
| 154 | +com/embed/d30acf8e4451459689b0d295b87bb5f9?sid=9a4f5aec-59ef-4fa3-9e76-0de72e75f278" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 155 | + |
| 156 | + |
| 157 | +#### Filtering table data |
| 158 | +Sometimes you might want to filter a list based on a condition. For example, showing only |
| 159 | +completed assignments. You can do so by adding the Filter while you query a Supabase table. |
| 160 | + |
| 161 | +Let's see how to filter the Supabase table to display only desired items: |
| 162 | + |
| 163 | +- In your **Action properties** of Query Rows action, scroll down and click on the **+ Add Filter** |
| 164 | + button at the bottom. |
| 165 | + |
| 166 | +- Find the **Field Name**, click on the Unset, and select a column on which you would like to apply |
| 167 | + the filter. |
| 168 | +- Find the **Relation** dropdown, click on the Unset, and choose the relation amongst the list. |
| 169 | +- Find the **Value** property and set it to an appropriate value and click Confirm. |
| 170 | + |
| 171 | +:::tip |
| 172 | +You could choose a Filter relation based on your requirements. For example, to show only completed assignments, set the Field Name to the column that holds completed status, e.g., is_done, set the Relation to Equal To, and set the Value to True. Here's another example. For showing only users older than 30, create a column called Age, set the Relation to Greater Than, and set the Value to 30. |
| 173 | +::: |
| 174 | + |
| 175 | +<div class="video-container"><iframe src="https://www.loom. |
| 176 | +com/embed/b5a62eac1f5d4d9698d30b064c395326?sid=4e8c4972-7fac-4c9b-b495-c9f0cb783717" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 177 | + |
| 178 | + |
| 179 | +#### Ordering table data |
| 180 | +You might want to show a list from the Supabase table in a specific order. For example, showing assignments in order of the due date. |
| 181 | + |
| 182 | +To specify the order: |
| 183 | + |
| 184 | +- In your **Action properties** of Query Rows action, scroll down and click on the **+ Add Order** |
| 185 | +button at the bottom. |
| 186 | +- Set the **Table Field Name** to the column you would like to choose for ordering. |
| 187 | +- Find **Order** dropdown, click on the Unset and choose the order either **Increasing** or **Decreasing** and |
| 188 | + click **Confirm**. |
| 189 | + |
| 190 | +:::tip |
| 191 | +You could choose the order based on your requirements. For example, to show assignments in order of due date, set Table Field Name to due_date and Order to Increasing. |
| 192 | +::: |
| 193 | + |
| 194 | +<div class="video-container"><iframe src="https://www.loom. |
| 195 | +com/embed/244eea2a4f694120b08524b7b8cf67de?sid=13d47ef5-5d48-4986-88da-d335ce23bd06" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
0 commit comments