The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Docs: postgres functions (#3589)
Copple68b21f3881a3
diff --git a/web/docs/guides/api.mdx b/web/docs/guides/api.mdxindex b612fcf0112..4c0f77340b5 100644--- a/web/docs/guides/api.mdx+++ b/web/docs/guides/api.mdx@@ -7,7 +7,6 @@ description: Auto-generating and Realtime APIs.import Tabs from '@theme/Tabs';import TabItem from '@theme/TabItem';-## OverviewSupabase generates APIs directly from your Database schema. The API is:@@ -30,6 +29,15 @@ It provides everything you need from a CRUD API:- Works with Postgres Functions- Works with the Postgres security model - including Row Level Security, Roles, and Grants.+<iframe+ className="w-full video-with-border"+ width="640"+ height="385"+ src="https://www.youtube-nocookie.com/embed/rPAJJFdtPw0"+ frameBorder="1"+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"+ allowFullScreen+></iframe>### Realtime APIdiff --git a/web/docs/guides/auth.mdx b/web/docs/guides/auth.mdxindex 38aa8a671b8..8b4e44af6a0 100644--- a/web/docs/guides/auth.mdx+++ b/web/docs/guides/auth.mdx@@ -9,6 +9,16 @@ import Tabs from '@theme/Tabs'import TabItem from '@theme/TabItem'import providers from '@site/src/data/authProviders'+<iframe+ className="w-full video-with-border"+ width="640"+ height="385"+ src="https://www.youtube-nocookie.com/embed/6ow_jW4epf8"+ frameBorder="1"+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"+ allowFullScreen+></iframe>+## User ManagementSupabase makes it simple to manage your users.diff --git a/web/docs/guides/auth/intro.mdx b/web/docs/guides/auth/intro.mdxindex 1d52730c4bf..cc53cbf71de 100644--- a/web/docs/guides/auth/intro.mdx+++ b/web/docs/guides/auth/intro.mdx@@ -11,10 +11,22 @@ import Tabs from '@theme/Tabs'import TabItem from '@theme/TabItem'import providers from '@site/src/data/authProviders'+<iframe+ className="w-full video-with-border"+ width="640"+ height="385"+ src="https://www.youtube-nocookie.com/embed/6ow_jW4epf8"+ frameBorder="1"+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"+ allowFullScreen+></iframe>++## Introduction+Supabase Auth is designed to work with Postgres. There are two parts to every Auth system:-- Authentication: should this person be allowed in? If yes, who are they?-- Authorization: once they are in, what are they allowed to do?+- **Authentication:** should this person be allowed in? If yes, who are they?+- **Authorization:** once they are in, what are they allowed to do?## Authenticationdiff --git a/web/docs/guides/database/full-text-search.mdx b/web/docs/guides/database/full-text-search.mdxindex a59a1a09967..9f43aa6c6ee 100644--- a/web/docs/guides/database/full-text-search.mdx+++ b/web/docs/guides/database/full-text-search.mdx@@ -20,8 +20,8 @@ For this guide we'll use the following example data:<TabsdefaultValue="Data"values={[- {label: 'SQL', value: 'SQL'},{label: 'Data', value: 'Data'},+ {label: 'SQL', value: 'SQL'},]}><TabItem value="SQL">diff --git a/web/docs/guides/database/functions.mdx b/web/docs/guides/database/functions.mdxnew file mode 100644index 00000000000..2cb4ff1f1ef--- /dev/null+++ b/web/docs/guides/database/functions.mdx@@ -0,0 +1,312 @@+---+id: functions+title: Functions+description: Creating and using Postgres functions.+---++import Tabs from '@theme/Tabs';+import TabItem from '@theme/TabItem';+++Postgres has built-in support for [SQL functions](https://www.postgresql.org/docs/current/sql-createfunction.html).+These functions live inside your database, and they can be [used with the API](/docs/reference/javascript/rpc).++## Quick demo++<iframe+ className="w-full video-with-border"+ width="640"+ height="385"+ src="https://www.youtube-nocookie.com/embed/MJZCCpCYEqk"+ frameBorder="1"+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"+ allowFullScreen+></iframe>++## Creating Functions++Supabase provides several options for creating database functions. You can use the Dashboard or create them directly using SQL.+We provide a SQL editor within the Dashboard, or you can [connect](/docs/guides/database/connecting/connecting-to-postgres) to your database+and run the SQL queries yourself.+++1. Go to the "SQL editor" section.+2. Click "New Query".+3. Enter the SQL to create or replace your Database function.+4. Click "Run" or cmd+enter (ctrl+enter).++## Examples++### Data set++For this guide we'll use the following example data:+++<Tabs+defaultValue="Data"+values={[+ {label: 'Data', value: 'Data'},+ {label: 'SQL', value: 'SQL'},+]}>++<TabItem value="SQL">++```sql+create table planets (+ id serial primary key,+ name text+);++insert into planets (id, name)+values+ (1, 'Tattoine'),+ (2, 'Alderaan'),+ (3, 'Kashyyyk');++create table people (+ id serial primary key,+ name text,+ planet_id bigint references planets+);++insert into people (id, name, planet_id)+values+ (1, 'Anakin Skywalker', 1),+ (2, 'Luke Skywalker', 1),+ (3, 'Princess Leia', 2),+ (4, 'Chewbacca', 3);+```++</TabItem>+<TabItem value="Data">++<h4>Planets</h4>++| id | name |+| ---- | ---- |+| 1 | Tattoine |+| 2 | Alderaan |+| 3 | Kashyyyk |++<h4>People</h4>++| id | name | planet_id |+| ---- | ---- | ---- |+| 1 | Anakin Skywalker | 1 |+| 2 | Luke Skywalker | 1 |+| 3 | Princess Leia | 2 |+| 4 | Chewbacca | 3 |++</TabItem>++</Tabs>++### Basic function++A basic example which returns a string "hello world".++```sql+create or replace function hello_world() -- 1+returns text -- 2+language sql -- 3+as $$ -- 4+ select 'hello world'; -- 5+$$; --6++```++#### Details++<details>+<summary>Show/Hide</summary>++At it's most basic a function has the following parts:++1. `create or replace function hello_world()`+- The function declaration, where `hello_world` is the name of the function.+- You can use either `create` when creating a new fucntion or `replace` when replacing an existing function. Or you can use `create or replace` together to handle either.+2. `returns text`+- The type of data that the function returns. If it returns nothing, you can `return void`.+3. `language sql`+- The language used inside the function body. This can also be a procedural language: `plpgsql`, `plv8`, `plpython`, etc.+4. `as $$`+- The function wrapper. Anything enclosed inside the `$$` symbols will be part of the function body.+5. `select 'hello world';`+- A simple function body. The final `select` statement inside a function body will be returned if there are no statements following it.+6. `$$;`+- The closing symbols of the funciton wrapper.++</details>++#### Execute++<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'JS', value: 'JS'},+ {label: 'Dart', value: 'Dart'},+]}>++<TabItem value="SQL">++```sql+select hello_world();+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .rpc('hello_world')+```++Reference: [rpc()](/docs/reference/javascript/rpc)++</TabItem>+<TabItem value="Dart">++```dart+final res = await supabase+ .rpc('hello_world')+ .execute();+```++Reference: [rpc()](/docs/reference/dart/rpc)++</TabItem>++</Tabs>+++### Return table sets++A basic example which return all the `planets` in our example data set.++```sql+create or replace function get_planets()+returns setof planets+language sql+as $$+ select * from planets;+$$;+```++#### Execute++Because this function returns a table set, we can also apply filters and selectors. For example, if we only wanted the first planet:++<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'JS', value: 'JS'},+ {label: 'Dart', value: 'Dart'},+]}>++<TabItem value="SQL">++```sql+select *+from get_planets()+where id = 1;+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .rpc('get_planets')+ .eq('id', 1)+```++Reference: [rpc()](/docs/reference/javascript/rpc)++</TabItem>+<TabItem value="Dart">++```dart+final res = await supabase+ .rpc('get_planets')+ .eq('id', 1)+ .execute();+```++Reference: [rpc()](/docs/reference/dart/rpc)++</TabItem>++</Tabs>+++### With parameters++A basic example which will insert a new planet into the `planets` and return the new ID. Note that this time we're using the `plpgsql` language.++```sql+create or replace function add_planet(name text)+returns bigint+language plpgsql+as $$+declare+ new_row bigint;+begin+ insert into planets(name)+ values (add_planet.name)+ returning id into new_row;++ return new_row;+end;+$$;+```++#### Execute++Because this function returns a table set, we can also apply filters and selectors. For exaple, if we only wanted the `name` of the first planet:++<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'JS', value: 'JS'},+ {label: 'Dart', value: 'Dart'},+]}>++<TabItem value="SQL">++```sql+select * from add_planet('Jakku');+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .rpc('add_planet', { name: 'Jakku' })+```++Reference: [rpc()](/docs/reference/javascript/rpc)++</TabItem>+<TabItem value="Dart">++```dart+final res = await supabase+ .rpc('add_planet', params: { 'name': 'Jakku' })+ .execute();+```++Reference: [rpc()](/docs/reference/dart/rpc)++</TabItem>++</Tabs>+++## Resources++- PostgreSQL Official Docs: [Chapter 9. Functions and Operators](https://www.postgresql.org/docs/current/functions.html)+- PostgreSQL Reference: [CREATE FUNCTION](https://www.postgresql.org/docs/9.1/sql-createfunction.html)\ No newline at end of filediff --git a/web/sidebars.js b/web/sidebars.jsindex 925ce56c7bb..bdeaad781d4 100755--- a/web/sidebars.js+++ b/web/sidebars.js@@ -94,6 +94,7 @@ module.exports = {items: ['guides/database/introduction','guides/database/tables',+ 'guides/database/functions','guides/database/full-text-search',// 'guides/database/json',// 'guides/database/arrays',diff --git a/web/src/css/custom.css b/web/src/css/custom.cssindex ce0ef0713ef..55eae960ded 100755--- a/web/src/css/custom.css+++ b/web/src/css/custom.css@@ -1315,4 +1315,8 @@ h4.method-list-item-label .method-list-item-validation {font-family: 'office code pro';border: 1px solid var(--custom-border-color);padding: 2px 6px;+}++details summary:hover {+ cursor: pointer;}\ No newline at end of file