porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.42k stars 271 forks source link

Encountering CONNECTION_CLOSED error when using query parameters in Hyperdrive connection in Cloudflare Worker #721

Closed allemonta closed 11 months ago

allemonta commented 11 months ago

I followed the guide provided at https://developers.cloudflare.com/hyperdrive/get-started/ to set up a Hyperdrive connection to the Neon database. I conducted the same test on a Vercel database with identical results.

Currently, the connection works when executing queries without any parameters. However, when I attempt to execute a query with parameters, it results in a CONNECTION_CLOSED error.

Example code snippet:

import Postgres from "postgres"

export interface Env {
    HYPERDRIVE: Hyperdrive
}

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
        try {
            const sql = Postgres(env.HYPERDRIVE.connectionString)
                         // Also tried with { prepare: false, fetch_types: false } options

            const result = await sql`
                SELECT ${42}
            `

            return new Response(JSON.stringify(result))
        } catch (err) {
            return new Response(JSON.stringify({
                message: err.message,
                code: err.code
            }))
        }
    }
};

Example response:

{
    "message": "write CONNECTION_CLOSED xxxxx.hyperdrive.local:5432",
    "code": "CONNECTION_CLOSED"
}
anirudhsama commented 11 months ago

I'm also facing this issue with version 3.4.3. Earlier issue with Hyperdrive was related to connection timeout which was fixed with 3.4.1 - https://github.com/porsager/postgres/issues/702. This is a different issue.

anirudhsama commented 11 months ago

Just tired this out and it seems to be working fine now.

porsager commented 11 months ago

Thanks for the feedback @anirudhsama