Skip to content

Hyperdrive Getting Started guide should address connection clean-up #27109

@bprcode

Description

@bprcode

Existing documentation URL(s)

What changes are you suggesting?

The current Getting Started documentation for Hyperdrive is missing the step where the pg client should be disposed via .end(). This may lead to leaked database connections. The "Write a Worker" section does not mention how to correctly clean up after workers, which is not obvious to new users, since it relies on the execution context unique to Cloudflare Workers, unlike typical Node promise behavior where fire-and-forget promises could be used. In contrast, the code example exposed through the Cloudflare dashboard does address this step:

import { Client } from "pg";

export default {
  async fetch(request, env, ctx) {
    // Hyperdrive provides a unique generated connection string to connect to
    // your database via Hyperdrive that can be used with your existing tools
    const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
    await client.connect();

    try {
      // Sample SQL query
      const result = await client.query("SELECT * FROM pg_tables");

      // Close the client after the response is returned
      ctx.waitUntil(client.end());

      return Response.json({result: result.rows});
    } catch (e) {
      return Response.json({ error: e instanceof Error ? e.message : e }, { status: 500 });
    }
  },
}

I suggest adding the waitUntil step to the Getting Started documentation for Hyperdrive, or, if it is for some reason not necessary to call .end() on the client, then addressing the reason for this as part of the tutorial.

Additional information

No response

Metadata

Metadata

Labels

content:editRequest for content editsdocumentationDocumentation editsproduct:hyperdriveHyperdrive: https://developers.cloudflare.com/hyperdrive/

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions