neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
362 stars 16 forks source link

neon is not reporting significant errors when linking to the supabse database #122

Open GmavionR opened 2 days ago

GmavionR commented 2 days ago

Steps to reproduce

Start by creating a supabase pg storage in vercel, note supabase instead of neon. Then use the following code to test, will report an error.

import { neon } from "@neondatabase/serverless";
import dotenv from "dotenv";
dotenv.config();
console.log("Hello CodeSandbox", process.env.POSTGRES_URL);

const sql = neon(process.env.POSTGRES_URL);

// const [post] = await sql`SELECT * FROM posts WHERE id = ${postId}`;
const data = await sql`SELECT * FROM revenue`;
console.log("data", data);
// `post` is now { id: 12, title: 'My post', ... } (or undefined)
 "dependencies": {
    "@neondatabase/serverless": "0.10.3",
    "dotenv": "16.4.5"
  }
                       ^

NeonDbError: Error connecting to database: fetch failed
    at execute (file:///project/workspace/node_modules/@neondatabase/serverless/index.mjs:1548:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///project/workspace/index.js:9:14 {
  severity: undefined,
  code: undefined,
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: undefined,
  line: undefined,
  routine: undefined,
  sourceError: TypeError: fetch failed
      at Object.fetch (node:internal/deps/undici/undici:11372:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async execute (file:///project/workspace/node_modules/@neondatabase/serverless/index.mjs:1547:21)
      at async file:///project/workspace/index.js:9:14 {
    cause: Error: getaddrinfo ENOTFOUND api.pooler.supabase.com
        at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26) {
      errno: -3008,
      code: 'ENOTFOUND',
      syscall: 'getaddrinfo',
      hostname: 'api.pooler.supabase.com'
    }
  }
}

The reason for the error is that we passed supabse's database url when using neon, which caused the query to error.

Why do you ask? Chapter 6 of the nextjs Learning demo mentions creating a database, but does not specify whether you should create neon or supabse. Although in theory @vercel/postgres blocks the difference between low-level neon and virtual.

However, there is a bug with @vercel/postgres that actually uses neon internally when sql is used directly in the following code, even though my.env configuration is configured with the supabase url

import { sql } from '@vercel/postgres';
export async function fetchLatestInvoices() {
  try {
    const data = await sql<LatestInvoiceRaw>`
      SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
      FROM invoices
      JOIN customers ON invoices.customer_id = customers.id
      ORDER BY invoices.date DESC
      LIMIT 5`;
    const latestInvoices = data.rows.map((invoice) => ({
      ...invoice,
      amount: formatCurrency(invoice.amount),
    }));
    return latestInvoices;
  } catch (error) {
    console.error('Database Error:', error);
    throw error;    throw new Error('Failed to fetch the latest invoices.');
  }

Image

Expected result

neon gives the correct indication when linking to the supabse.com url

Actual result

Now it's an error

Environment

Logs, links

danieltprice commented 2 days ago

@GmavionR can you point me to the Next.js learning demo you are using? @vercel/postgres is a wrapper around the Neon serverless driver. I believe it is solely intended for use with Neon. I would not expect it to support connecting to a Supabase database.