Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ export default {
headers: { "Content-Type": "application/json" },
});

ctx.waitUntil(client.end());
return resp;

// Create a route for querying within a time-frame
Expand All @@ -221,7 +220,6 @@ export default {
headers: { "Content-Type": "application/json" },
});

ctx.waitUntil(client.end());
return resp;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ To connect to Neon using `@neondatabase/serverless`, follow these steps:
const client = new Client(env.DATABASE_URL);
await client.connect();
const { rows } = await client.query("SELECT * FROM elements");
ctx.waitUntil(client.end()); // this doesn’t hold up the response

return new Response(JSON.stringify(rows));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ Create a new `Client` instance and pass the Hyperdrive `connectionString`:
import { Client } from "pg";

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
// Create a new client instance for each request.
const client = new Client({
connectionString: env.HYPERDRIVE.connectionString,
Expand All @@ -43,17 +47,14 @@ export default {
// Perform a simple query
const result = await client.query("SELECT * FROM pg_tables");

// Clean up the client after the response is returned, before the Worker is killed
ctx.waitUntil(client.end());

return Response.json({
success: true,
result: result.rows,
});
} catch (error: any) {
console.error("Database error:", error.message);

new Response('Internal error occurred', { status: 500 });
new Response("Internal error occurred", { status: 500 });
}
},
};
Expand Down