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 @@ -87,9 +87,6 @@ export default {
// Sample query to get all users
const allUsers = await db.select().from(users);

// Clean up the connection
ctx.waitUntil(sql.end());

return Response.json(allUsers);
},
} satisfies ExportedHandler<Env>;
Expand Down Expand Up @@ -169,4 +166,4 @@ Deploy your Worker.
npx wrangler deploy
```

<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />
11 changes: 4 additions & 7 deletions src/content/docs/hyperdrive/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ export interface Env {
export default {
async fetch(request, env, ctx): Promise<Response> {
// Create a client using the pg driver (or any supported driver, ORM or query builder)
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
const sql = new Client({
connectionString: env.HYPERDRIVE.connectionString,
});
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
const sql = new Client({
connectionString: env.HYPERDRIVE.connectionString,
});

try {
// Connect to the database
Expand All @@ -245,9 +245,6 @@ export default {
// Sample query
const results = await sql.query(`SELECT * FROM pg_tables`);

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

// Return result rows as JSON
return Response.json(results.rows);
} catch (e) {
Expand Down
4 changes: 0 additions & 4 deletions src/content/docs/hyperdrive/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ export default {
try {
// Sample SQL query
const results = await sql`SELECT * FROM pg_tables`;

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

return Response.json(results);
} catch (e) {
return Response.json({ error: e instanceof Error ? e.message : e }, { status: 500 });
Expand Down
110 changes: 50 additions & 60 deletions src/content/docs/workers/tutorials/postgres/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,18 @@ export default {
const sql = new Client({
connectionString: env.DB_URL,
});
try {
// Connect to the PostgreSQL database
await sql.connect();

// Query the products table
const result = await sql.query("SELECT * FROM products");

// Return the result as JSON
return new Response(JSON.stringify(result.rows), {
headers: {
"Content-Type": "application/json",
},
});
} finally {
// Clean up the client connection
await sql.end();
}
// Connect to the PostgreSQL database
await sql.connect();

// Query the products table
const result = await sql.query("SELECT * FROM products");

// Return the result as JSON
return new Response(JSON.stringify(result.rows), {
headers: {
"Content-Type": "application/json",
},
});
},
} satisfies ExportedHandler<Env>;
```
Expand Down Expand Up @@ -260,49 +255,44 @@ export default {
const sql = new Client({
connectionString: env.DB_URL,
});
try {
// Connect to the PostgreSQL database
await sql.connect();

const url = new URL(request.url);
if (request.method === "POST" && url.pathname === "/products") {
// Parse the request's JSON payload
const productData = (await request.json()) as {
name: string;
description: string;
price: number;
};

const name = productData.name,
description = productData.description,
price = productData.price;

// Insert the new product into the products table
const insertResult = await sql.query(
`INSERT INTO products(name, description, price) VALUES($1, $2, $3)
RETURNING *`,
[name, description, price],
);

// Return the inserted row as JSON
return new Response(JSON.stringify(insertResult.rows), {
headers: { "Content-Type": "application/json" },
});
}

// Query the products table
const result = await sql.query("SELECT * FROM products");

// Return the result as JSON
return new Response(JSON.stringify(result.rows), {
headers: {
"Content-Type": "application/json",
},
});
} finally {
// Clean up the client connection
await sql.end();
}
// Connect to the PostgreSQL database
await sql.connect();

const url = new URL(request.url);
if (request.method === "POST" && url.pathname === "/products") {
// Parse the request's JSON payload
const productData = (await request.json()) as {
name: string;
description: string;
price: number;
};

const name = productData.name,
description = productData.description,
price = productData.price;

// Insert the new product into the products table
const insertResult = await sql.query(
`INSERT INTO products(name, description, price) VALUES($1, $2, $3)
RETURNING *`,
[name, description, price],
);

// Return the inserted row as JSON
return new Response(JSON.stringify(insertResult.rows), {
headers: { "Content-Type": "application/json" },
});
}

// Query the products table
const result = await sql.query("SELECT * FROM products");

// Return the result as JSON
return new Response(JSON.stringify(result.rows), {
headers: {
"Content-Type": "application/json",
},
});
},
} satisfies ExportedHandler<Env>;
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export default {
// A very simple test query
const result = await sql`select * from pg_tables`;

// Clean up the client, ensuring we don't kill the worker before that is
// completed.
ctx.waitUntil(sql.end());

// Return result rows as JSON
return Response.json({ success: true, result: result });
} catch (e: any) {
Expand Down
4 changes: 0 additions & 4 deletions src/content/partials/prompts/base-prompt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,6 @@ const sql = postgres(env.HYPERDRIVE.connectionString)
// Test query
const results = await sql`SELECT * FROM pg_tables`;

// Clean up the client, ensuring we don't kill the worker before that is
// completed.
ctx.waitUntil(sql.end());

// Return result rows as JSON
return Response.json(results);
} catch (e) {
Expand Down
Loading