The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add upstash redis example and docs.
thorwebdeve20bcce4d125
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex f2139f3a407..b48668e0ae7 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -498,6 +498,7 @@ export const functions = {{ name: 'Storage Caching', url: '/guides/functions/examples/storage-caching', items: [] },{ name: 'Stripe Webhooks', url: '/guides/functions/examples/stripe-webhooks', items: [] },{ name: 'Telegram Bot', url: '/guides/functions/examples/telegram-bot', items: [] },+ { name: 'Upstash Redis', url: '/guides/functions/examples/upstash-redis', items: [] },],},],diff --git a/apps/docs/pages/guides/functions/examples/upstash-redis.mdx b/apps/docs/pages/guides/functions/examples/upstash-redis.mdxnew file mode 100644index 00000000000..9ff8b218c65--- /dev/null+++ b/apps/docs/pages/guides/functions/examples/upstash-redis.mdx@@ -0,0 +1,91 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'examples-upstash-redis',+ title: 'Upstash Redis',+ description: 'Build an Edge Functions Counter with Upstash Redis.',+}++Redis counter example that stores a [hash](https://redis.io/commands/hincrby/) of function invocation count per region.++## Redis Database Setup++Create a Redis Database using [Upstash Console](https://console.upstash.com/) or [Upstash CLI](https://github.com/upstash/cli).++Select the `Global` type to minimize the latency from all edge locations. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to your .env file.++You'll find them under Details > REST API > .env++```bash+cp supabase/functions/upstash-redis-counter/.env.example supabase/functions/upstash-redis-counter/.env+```++## Code++Make sure you have the latest version of the [Supabase CLI installed](https://supabase.com/docs/guides/cli#installation).++Create a new function in your project:++```bash+supabase functions new upstash-redis-counter+```++And add the code to the `index.ts` file:++```ts index.ts+import { serve } from 'https://deno.land/[email protected]/http/server.ts'+import { Redis } from 'https://deno.land/x/[email protected]/mod.ts'++console.log(`Function "upstash-redis-counter" up and running!`)++serve(async (_req) => {+ try {+ const redis = new Redis({+ url: Deno.env.get('UPSTASH_REDIS_REST_URL')!,+ token: Deno.env.get('UPSTASH_REDIS_REST_TOKEN')!,+ })++ const deno_region = Deno.env.get('DENO_REGION')+ if (deno_region) {+ // Increment region counter+ await redis.hincrby('supa-edge-counter', deno_region, 1)+ } else {+ // Increment localhost counter+ await redis.hincrby('supa-edge-counter', 'localhost', 1)+ }++ // Get all values+ const counterHash: Record<string, number> | null = await redis.hgetall('supa-edge-counter')+ const counters = Object.entries(counterHash!)+ .sort(([, a], [, b]) => b - a) // sort desc+ .reduce((r, [k, v]) => ({ total: r.total + v, regions: { ...r.regions, [k]: v } }), {+ total: 0,+ regions: {},+ })++ return new Response(JSON.stringify({ counters }), { status: 200 })+ } catch (error) {+ return new Response(JSON.stringify({ error: error.message }), { status: 200 })+ }+})+```++## Run locally++```bash+supabase start+supabase functions serve upstash-redis-counter --no-verify-jwt --env-file supabase/functions/upstash-redis-counter/.env+```++Navigate to 'http://localhost:54321/functions/v1/upstash-redis-counter'.++## Deploy++```bash+supabase functions deploy upstash-redis-counter --no-verify-jwt+supabase secrets set --env-file supabase/functions/upstash-redis-counter/.env+```++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Pagediff --git a/examples/edge-functions/supabase/functions/upstash-redis-counter/.env.example b/examples/edge-functions/supabase/functions/upstash-redis-counter/.env.examplenew file mode 100644index 00000000000..d0785c19978--- /dev/null+++ b/examples/edge-functions/supabase/functions/upstash-redis-counter/.env.example@@ -0,0 +1,2 @@+UPSTASH_REDIS_REST_URL=+UPSTASH_REDIS_REST_TOKEN=\ No newline at end of filediff --git a/examples/edge-functions/supabase/functions/upstash-redis-counter/README.md b/examples/edge-functions/supabase/functions/upstash-redis-counter/README.mdnew file mode 100644index 00000000000..a382fef5a11--- /dev/null+++ b/examples/edge-functions/supabase/functions/upstash-redis-counter/README.md@@ -0,0 +1,31 @@+# Upstash Redis in Supabase Edge Functions++Redis counter example that stores a [hash](https://redis.io/commands/hincrby/) of function invocation count per region.++## Redis Database Setup++Create a Redis Database using [Upstash Console](https://console.upstash.com/) or [Upstash CLI](https://github.com/upstash/cli).++Select the `Global` type to minimize the latency from all edge locations. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to your .env file. You'll find them under Details > REST API > .env++```bash+cp supabase/functions/upstash-redis-counter/.env.example supabase/functions/upstash-redis-counter/.env+```++## Run locally++Make sure you have the latest version of the [Supabase CLI installed](https://supabase.com/docs/guides/cli#installation).++```bash+supabase start+supabase functions serve upstash-redis-counter --no-verify-jwt --env-file supabase/functions/upstash-redis-counter/.env+```++Navigate to 'http://localhost:54321/functions/v1/upstash-redis-counter'.++## Deploy++```bash+supabase functions deploy upstash-redis-counter --no-verify-jwt+supabase secrets set --env-file supabase/functions/upstash-redis-counter/.env+```diff --git a/examples/edge-functions/supabase/functions/upstash-redis-counter/index.ts b/examples/edge-functions/supabase/functions/upstash-redis-counter/index.tsnew file mode 100644index 00000000000..911424209c9--- /dev/null+++ b/examples/edge-functions/supabase/functions/upstash-redis-counter/index.ts@@ -0,0 +1,41 @@+// 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 { Redis } from 'https://deno.land/x/[email protected]/mod.ts'++console.log(`Function "upstash-redis-counter" up and running!`)++serve(async (_req) => {+ try {+ const redis = new Redis({+ url: Deno.env.get('UPSTASH_REDIS_REST_URL')!,+ token: Deno.env.get('UPSTASH_REDIS_REST_TOKEN')!,+ })++ const deno_region = Deno.env.get('DENO_REGION')+ if (deno_region) {+ // Increment region counter+ await redis.hincrby('supa-edge-counter', deno_region, 1)+ } else {+ // Increment localhost counter+ await redis.hincrby('supa-edge-counter', 'localhost', 1)+ }++ // Get all values+ const counterHash: Record<string, number> | null = await redis.hgetall('supa-edge-counter')+ const counters = Object.entries(counterHash!)+ .sort(([, a], [, b]) => b - a) // sort desc+ .reduce((r, [k, v]) => ({ total: r.total + v, regions: { ...r.regions, [k]: v } }), {+ total: 0,+ regions: {},+ })++ return new Response(JSON.stringify({ counters }), { status: 200 })+ } catch (error) {+ return new Response(JSON.stringify({ error: error.message }), { status: 200 })+ }+})++// To invoke, navigate to 'http://localhost:54321/functions/v1/upstash-redis-counter'.