The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add function examples
thorwebdevdd1bbdb03968
diff --git a/examples/edge-functions/README.md b/examples/edge-functions/README.mdnew file mode 100644index 00000000000..c29d45ed849--- /dev/null+++ b/examples/edge-functions/README.md@@ -0,0 +1,40 @@+# Supabase Edge Function Examples++## What are Supabase Edge Functions?++[Supabase Edge Functions](https://supabase.com/edge-functions) are written in TypeScript, run via Deno, and deployed with the Supabase CLI. Please [download](https://github.com/supabase/cli#install-the-cli) the latest version of the Supabase CLI, or [upgrade](https://github.com/supabase/cli#install-the-cli) it if you have it already installed.++## Example Functions++The function examples are located in [`./supabase/functions`](./supabase/functions):++- Retrieve data from an authenticated user via RLS: [`select-from-table-with-auth-rls`](./supabase/functions/select-from-table-with-auth-rls/index.ts)++## Develop locally++- Run `supabase start` (make sure your Docker daemon is running.)+- Run `supabase functions serve your-function-name`+- Run the CURL command in the example function, or use the [invoke method](https://supabase.com/docs/reference/javascript/invoke) on the Supabase client.++## Deploy++- Generate access token and log in to CLI+ - Navigate to https://app.supabase.io/account/tokens+ - Click "Generate New Token"+ - Copy newly created token+ - run `supabase login`+ - Input your token when prompted+- Link your project+ - Within your project root run `supabase link --project-ref your-project-ref`+- Set up your secrets+ - Run `supabase secrets set --from-stdin < .env` to set the env vars from your `.env` file.+ - You can run `supabase secrets list` to check that it worked and also to see what other env vars are set by default.+- Deploy the function+ - Within your project root run `supabase functions deploy payment-sheet`+- In youre [`./app/.env`](./app/.env) file remove the `SUPA_FUNCTION_LOCALHOST` variable and restart your Expo app.++## 👁⚡️👁++\o/ That's it, you can now invoke your Supabase Function via the [`supabase-js`](https://supabase.com/docs/reference/javascript/invoke) and [`supabase-dart`](https://supabase.com/docs/reference/dart/invoke) client libraries. (More client libraries coming soon. Check the [supabase-community](https://github.com/supabase-community#client-libraries) org for details).++For more info on Supabase Functions, check out the [docs](https://supabase.com/docs/guides/functions) and the [examples](https://github.com/supabase/supabase/tree/master/examples/edge-functions).diff --git a/examples/edge-functions/supabase/config.toml b/examples/edge-functions/supabase/config.tomlnew file mode 100644index 00000000000..efec0060d27--- /dev/null+++ b/examples/edge-functions/supabase/config.toml@@ -0,0 +1,60 @@+# A string used to distinguish different Supabase projects on the same host. Defaults to the working+# directory name when running `supabase init`.+project_id = "edge-functions"++[api]+# Port to use for the API URL.+port = 54321+# Schemas to expose in your API. Tables, views and stored procedures in this schema will get API+# endpoints. public and storage are always included.+schemas = []+# Extra schemas to add to the search_path of every request.+extra_search_path = ["extensions"]+# The maximum number of rows returns from a view, table, or stored procedure. Limits payload size+# for accidental or malicious requests.+max_rows = 1000++[db]+# Port to use for the local database URL.+port = 54322+# The database major version to use. This has to be the same as your remote database's. Run `SHOW+# server_version;` on the remote database to check.+major_version = 14++[studio]+# Port to use for Supabase Studio.+port = 54323++# Email testing server. Emails sent with the local dev setup are not actually sent - rather, they+# are monitored, and you can view the emails that would have been sent from the web interface.+[inbucket]+# Port to use for the email testing server web interface.+port = 54324++[auth]+# The base URL of your website. Used as an allow-list for redirects and for constructing URLs used+# in emails.+site_url = "http://localhost:3000"+# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.+additional_redirect_urls = ["https://localhost:3000"]+# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 seconds (one+# week).+jwt_expiry = 3600+# Allow/disallow new user signups to your project.+enable_signup = true++[auth.email]+# Allow/disallow new user signups via email to your project.+enable_signup = true+# If enabled, a user will be required to confirm any email change on both the old, and new email+# addresses. If disabled, only the new email is required to confirm.+double_confirm_changes = true+# If enabled, users need to confirm their email address before signing in.+enable_confirmations = false++# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`,+# `discord`, `facebook`, `github`, `gitlab`, `google`, `twitch`, `twitter`, `slack`, `spotify`.+[auth.external.apple]+enabled = false+client_id = ""+secret = ""diff --git a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.tsnew file mode 100644index 00000000000..1fcfdb4603e--- /dev/null+++ b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts@@ -0,0 +1,33 @@+// Follow this setup guide to integrate the Deno language server with your editor:+// https://deno.land/manual/getting_started/setup_your_environment+// This enables autocomplete, go to definition, etc.++import { serve } from 'https://deno.land/[email protected]/http/server.ts'+import { createClient } from 'https://esm.sh/@supabase/supabase-js@^1.33.2'++console.log(`Function "select-from-table-with-auth-rls" up and running!`)++serve(async (req: Request) => {+ const supabase = createClient(+ // Supabase API URL - env var exported by default when deployed.+ Deno.env.get('SUPABASE_URL') ?? '',+ // Supabase API ANON KEY - env var exported by default when deployed.+ Deno.env.get('SUPABASE_ANON_KEY') ?? '',+ // Create client with Auth context of the user that called the function.+ // This way your row-level-security (RLS) policies are applied.+ { headers: { Authorization: req.headers.get('Authorization') ?? '' } }+ )++ const { data, error } = await supabase.from('users').select('*')+ console.log({ data, error })++ return new Response(JSON.stringify({ data, error }), {+ headers: { 'Content-Type': 'application/json' },+ })+})++// To invoke:+// curl -i --location --request POST 'http://localhost:54321/functions/v1/select-from-table-with-auth-rls' \+// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \+// --header 'Content-Type: application/json' \+// --data '{"name":"Functions"}'diff --git a/examples/edge-functions/supabase/migrations/20220331105910_init.sql b/examples/edge-functions/supabase/migrations/20220331105910_init.sqlnew file mode 100644index 00000000000..1c1fd933055--- /dev/null+++ b/examples/edge-functions/supabase/migrations/20220331105910_init.sql@@ -0,0 +1,28 @@+/**+* USERS+* Note: This table contains user data. Users should only be able to view and update their own data.+*/+create table users (+ -- UUID from auth.users+ id uuid references auth.users not null primary key,+ full_name text,+ avatar_url text+);+alter table users enable row level security;+create policy "Can view own user data." on users for select using (auth.uid() = id);+create policy "Can update own user data." on users for update using (auth.uid() = id);++/**+* This trigger automatically creates a user entry when a new user signs up via Supabase Auth.+*/+create function public.handle_new_user()+returns trigger as $$+begin+ insert into public.users (id, full_name, avatar_url)+ values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');+ return new;+end;+$$ language plpgsql security definer;+create trigger on_auth_user_created+ after insert on auth.users+ for each row execute procedure public.handle_new_user();\ No newline at end of file