functions-js icon indicating copy to clipboard operation
functions-js copied to clipboard

Edge functions/Deno not supporting scheme 'postgres'

Open zzulanas opened this issue 2 years ago • 4 comments

Bug report

  • [ ✅ ] I confirm this is a bug with Supabase, not with my own application.
  • [ ✅ ] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I'm unable to use the supabase-js client to access my database in an edge function using Deno.

To Reproduce

To reproduce, create a new Edge function, this is the function I was using below:

import { Configuration, OpenAIApi } from "openai";
import { createClient } from "@supabase/supabase-js";

const supabaseUrl = Deno.env.get("SUPABASE_DB_URL");
const supabaseKey = Deno.env.get("SUPABASE_ANON_KEY");
export const supabaseClient = createClient(supabaseUrl, supabaseKey);


const configuration = new Configuration({ apiKey:  Deno.env.get("OPEN_AI_KEY" });
const openAi = new OpenAIApi(configuration);

const documents = [
  "Zach loves long walks on the beach",
  "Zach loves ice cream, especially vanilla ice cream",
  "In Zach's free time he likes to rock climb, the highest grade he can do is around a V5, he's still learning!",
];

// Assuming each document is a string
for (const document of documents) {
  // OpenAI recommends replacing newlines with spaces for best results
  const input = document.replace(/\n/g, " ");

  const embeddingResponse = await openAi.createEmbedding({
    model: "text-embedding-ada-002",
    input,
  });

  const [{ embedding }] = embeddingResponse.data.data;

  // In production we should handle possible errors
  await supabaseClient.from("documents").insert({
    content: document,
    embedding,
  });
}

You can use this function or some other function that just inserts data into any table, but I'm not able to add any data through the edge functions.

Try deploying this as a Supabase edge function and invoking it. When I invoke it I get this error:

{
  message: "TypeError: scheme 'postgres' not supported",
  details: "TypeError: scheme 'postgres' not supported\n" +
    "    at opFetch (ext:deno_fetch/26_fetch.js:73:14)\n" +
    "    at "... 504 more characters,
  hint: "",
  code: ""
} error

Expected behavior

I expected the edge function to insert the data correctly, but no data was inserted into the DB, and the function errored out on the first document. I'm not sure if I'm doing this correctly, but this was following a guide to create embeddings in a normal Typescript file, I wanted to extend that to making an Edge function to do that. If that isn't possible then I was unaware and I can work with it some other way.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: MacOS
  • Browser (if applies) N/A
  • Version of supabase-js: 2.25.0
  • Version of Deno: 1.34.3

Additional context

Add any other context about the problem here.

zzulanas avatar Jun 19 '23 19:06 zzulanas

Getting the same error with similar setup.

malewis5 avatar Jul 16 '23 16:07 malewis5

I encountered the same error with basically the same setup as well. Using the SUPABASE_SERVICE_ROLE_KEY instead of the ANON_KEY does seem to solve the problem. However, that is not an ideal solution. Another workaround would be to connect to the Postgres Client directly as stated in the documentation.

doeringi avatar Jan 27 '24 11:01 doeringi

I came across this issue, apparently is an issue with confused naming on supabase secrets. SUPABASE_DB_URL is the Postgres connection URL, something like postgres://... SUPABASE_URL is the supabase API url, something like https://<your_project_unique_key... SUPABASE_URL is what you should use when creating a supabase client with supabasejs

WawinyEdwin avatar Jan 30 '24 04:01 WawinyEdwin

I came across this issue, apparently is an issue with confused naming on supabase secrets. SUPABASE_DB_URL is the Postgres connection URL, something like postgres://... SUPABASE_URL is the supabase API url, something like https://<your_project_unique_key... SUPABASE_URL is what you should use when creating a supabase client with supabasejs

The reason why you get the mentioned error is because deno's fetch API only supports an https:// scheme for a URL.

WawinyEdwin avatar Jan 30 '24 04:01 WawinyEdwin