From 867ade541535d7917f4ef5f8ae73e5e5d4a26e12 Mon Sep 17 00:00:00 2001 From: Sam Aybar Date: Mon, 13 Feb 2023 10:01:23 -0500 Subject: [PATCH 1/2] chore: add postgres-on-the-edge example --- .../postgres-on-the-edge/.env.example | 2 + .../functions/postgres-on-the-edge/README.md | 18 ++++++++ .../functions/postgres-on-the-edge/index.ts | 43 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 examples/edge-functions/supabase/functions/postgres-on-the-edge/.env.example create mode 100644 examples/edge-functions/supabase/functions/postgres-on-the-edge/README.md create mode 100644 examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts diff --git a/examples/edge-functions/supabase/functions/postgres-on-the-edge/.env.example b/examples/edge-functions/supabase/functions/postgres-on-the-edge/.env.example new file mode 100644 index 00000000000..638351bc2c8 --- /dev/null +++ b/examples/edge-functions/supabase/functions/postgres-on-the-edge/.env.example @@ -0,0 +1,2 @@ +#Use PolyScale connection string with application_name parameter to get code-free database caching +DATABASE_URL="postgresql:/DB_USERNAME:DB_PASSWORD@psedge.global:5432/postgres?application_name=POLYSCALE_CACHE_ID" \ No newline at end of file diff --git a/examples/edge-functions/supabase/functions/postgres-on-the-edge/README.md b/examples/edge-functions/supabase/functions/postgres-on-the-edge/README.md new file mode 100644 index 00000000000..dc7369c5cfd --- /dev/null +++ b/examples/edge-functions/supabase/functions/postgres-on-the-edge/README.md @@ -0,0 +1,18 @@ +# postgres-on-the-edge + +This function allows you to access your Supabase database directly via TCP from an edge function. + +You can add a global cache to your database for fast access to your data, globally by simply connecting your Supabase database to [PolyScale](https://polyscale.ai). + +## Setup +1. Rename `.env.example` to `.env` +2. Insert Supabase database connection string for `DATABASE_URL` in `.env` file +3. (Optional) Create PolyScale cache - see [instructions](https://supabase.com/docs/guides/integrations/polyscale) for adding a PolyScale cache. +4. Replace `DATABASE_URL` with PolyScale connection string. + +## Deploy + +1. Run `supabase functions deploy --no-verify-jwt postgres-on-the-edge` +2. Run `supabase secrets set --env-file supabase/functions/postgres-on-the-edge/.env` + +Learn more about creating this function [here](https://www.youtube.com/watch?v=cl7EuF1-RsY). \ No newline at end of file diff --git a/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts b/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts new file mode 100644 index 00000000000..88724f203e4 --- /dev/null +++ b/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts @@ -0,0 +1,43 @@ +import * as postgres from "https://deno.land/x/postgres@v0.14.2/mod.ts"; +import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; + +// Get the connection string from the environment variable "DATABASE_URL" +const databaseUrl = Deno.env.get("DATABASE_URL")!; + +// Create a database pool with three connections that are lazily established +const pool = new postgres.Pool(databaseUrl, 3, true); + +serve(async (_req) => { + try { + // Grab a connection from the pool + const connection = await pool.connect(); + + try { + // Run a query + const result = await connection.queryObject`SELECT * FROM animals`; + const animals = result.rows; // [{ id: 1, name: "Lion" }, ...] + console.log(animals); + + // Encode the result as pretty printed JSON + const body = JSON.stringify( + animals, + (key, value) => (typeof value === "bigint" ? value.toString() : value), + 2 + ); + + // Return the response with the correct content type header + return new Response(body, { + status: 200, + headers: { + "Content-Type": "application/json; charset=utf-8", + }, + }); + } finally { + // Release the connection back into the pool + connection.release(); + } + } catch (err) { + console.error(err); + return new Response(String(err?.message ?? err), { status: 500 }); + } +}); \ No newline at end of file From 00a57d4d51a1155fabe2b98adc3c498b8720b05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thor=20=E9=9B=B7=E7=A5=9E=20Schaeff?= <5748289+thorwebdev@users.noreply.github.com> Date: Tue, 14 Feb 2023 11:31:32 +0800 Subject: [PATCH 2/2] chore: run prettier. --- .../functions/postgres-on-the-edge/index.ts | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts b/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts index 88724f203e4..6ce14f6990d 100644 --- a/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts +++ b/examples/edge-functions/supabase/functions/postgres-on-the-edge/index.ts @@ -1,43 +1,43 @@ -import * as postgres from "https://deno.land/x/postgres@v0.14.2/mod.ts"; -import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; +import * as postgres from 'https://deno.land/x/postgres@v0.14.2/mod.ts' +import { serve } from 'https://deno.land/std@0.168.0/http/server.ts' // Get the connection string from the environment variable "DATABASE_URL" -const databaseUrl = Deno.env.get("DATABASE_URL")!; +const databaseUrl = Deno.env.get('DATABASE_URL')! // Create a database pool with three connections that are lazily established -const pool = new postgres.Pool(databaseUrl, 3, true); +const pool = new postgres.Pool(databaseUrl, 3, true) serve(async (_req) => { try { // Grab a connection from the pool - const connection = await pool.connect(); + const connection = await pool.connect() try { // Run a query - const result = await connection.queryObject`SELECT * FROM animals`; - const animals = result.rows; // [{ id: 1, name: "Lion" }, ...] - console.log(animals); + const result = await connection.queryObject`SELECT * FROM animals` + const animals = result.rows // [{ id: 1, name: "Lion" }, ...] + console.log(animals) // Encode the result as pretty printed JSON const body = JSON.stringify( animals, - (key, value) => (typeof value === "bigint" ? value.toString() : value), + (_key, value) => (typeof value === 'bigint' ? value.toString() : value), 2 - ); + ) // Return the response with the correct content type header return new Response(body, { status: 200, headers: { - "Content-Type": "application/json; charset=utf-8", + 'Content-Type': 'application/json; charset=utf-8', }, - }); + }) } finally { // Release the connection back into the pool - connection.release(); + connection.release() } } catch (err) { - console.error(err); - return new Response(String(err?.message ?? err), { status: 500 }); + console.error(err) + return new Response(String(err?.message ?? err), { status: 500 }) } -}); \ No newline at end of file +})