Using 'pg' in an Astro API route

Hi. I think you can use Node's pg with Cloudflare workers according to https://developers.cloudflare.com/workers/tutorials/postgres I am trying to deploy an Astro API route like the following but the build is failing The package "net" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. It's not clear to me where I am supposed to specify the platform.
import { Client } from "pg";

export async function GET({ params, request }) {
const DATABASE_URL = new URL(request.url).searchParams.get("database_url");
const client = new Client({ DATABASE_URL });
await client.connect();
await client.query("SELECT pg_sleep(1);");
await client.end();
return new Response(
JSON.stringify({
DATABASE_URL: DATABASE_URL,
})
);
}
import { Client } from "pg";

export async function GET({ params, request }) {
const DATABASE_URL = new URL(request.url).searchParams.get("database_url");
const client = new Client({ DATABASE_URL });
await client.connect();
await client.query("SELECT pg_sleep(1);");
await client.end();
return new Response(
JSON.stringify({
DATABASE_URL: DATABASE_URL,
})
);
}
Connect to a PostgreSQL database with Cloudflare Workers · Cloudfla...
In this tutorial, you will learn how to create a Cloudflare Workers application and connect it to a PostgreSQL database using TCP Sockets. The Workers …
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server