Skip to content

Commit 0703181

Browse files
harshil1712sdnts
authored andcommitted
Update to use pg (cloudflare#22982)
1 parent 348e015 commit 0703181

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/content/docs/hyperdrive/examples/connect-to-postgres/index.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ Hyperdrive uses Workers [TCP socket support](/workers/runtime-apis/tcp-sockets/#
4444

4545
| Driver | Documentation | Minimum Version Required | Notes |
4646
| ---------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
47+
| node-postgres - `pg` (recommended) | [node-postgres - `pg` documentation](https://node-postgres.com/) | `[email protected]` | `8.11.4` introduced a bug with URL parsing and will not work. `8.11.5` fixes this. Requires `compatibility_flags = ["nodejs_compat"]` and `compatibility_date = "2024-09-23"` - refer to [Node.js compatibility](/workers/runtime-apis/nodejs). Requires wrangler `3.78.7` or later. |
4748
| Postgres.js | [Postgres.js documentation](https://github.com/porsager/postgres) | `[email protected]` | Supported in both Workers & Pages. |
48-
| node-postgres - `pg` | [node-postgres - `pg` documentation](https://node-postgres.com/) | `[email protected]` | `8.11.4` introduced a bug with URL parsing and will not work. `8.11.5` fixes this. Requires `compatibility_flags = ["nodejs_compat"]` and `compatibility_date = "2024-09-23"` - refer to [Node.js compatibility](/workers/runtime-apis/nodejs). Requires wrangler `3.78.7` or later. |
4949
| Drizzle | [Drizzle documentation](https://orm.drizzle.team/) | `0.26.2`^ | |
5050
| Kysely | [Kysely documentation](https://kysely.dev/) | `0.26.3`^ | |
5151
| [rust-postgres](https://github.com/sfackler/rust-postgres) | [rust-postgres documentation](https://docs.rs/postgres/latest/postgres/) | `v0.19.8` | Use the [`query_typed`](https://docs.rs/postgres/latest/postgres/struct.Client.html#method.query_typed) method for best performance. |
@@ -68,18 +68,18 @@ The following examples show you how to:
6868
2. Pass the Hyperdrive connection string and connect to the database.
6969
3. Query your database via Hyperdrive.
7070

71-
### Postgres.js
72-
73-
The following Workers code shows you how to use [Postgres.js](https://github.com/porsager/postgres) with Hyperdrive.
74-
75-
<Render file="use-postgres-js-to-make-query" product="hyperdrive" />
76-
7771
### node-postgres / pg
7872

7973
Install the `node-postgres` driver:
8074

8175
<Render file="use-node-postgres-to-make-query" product="hyperdrive" />
8276

77+
### Postgres.js
78+
79+
The following Workers code shows you how to use [Postgres.js](https://github.com/porsager/postgres) with Hyperdrive.
80+
81+
<Render file="use-postgres-js-to-make-query" product="hyperdrive" />
82+
8383
## Identify connections from Hyperdrive
8484

8585
To identify active connections to your Postgres database server from Hyperdrive:

src/content/docs/hyperdrive/get-started.mdx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,15 @@ Once you have created a Hyperdrive configuration and bound it to your Worker, yo
182182
<Tabs>
183183
<TabItem label="PostgreSQL">
184184

185-
To connect to your database, you will need a database driver which allows you to authenticate and query your database. For this tutorial, you will use [Postgres.js](https://github.com/porsager/postgres), one of the most widely used PostgreSQL drivers.
185+
To connect to your database, you will need a database driver which allows you to authenticate and query your database. For this tutorial, you will use [node-postgres (pg)](https://node-postgres.com/), one of the most widely used PostgreSQL drivers.
186186

187-
To install `postgres`, ensure you are in the `hyperdrive-tutorial` directory. Open your terminal and run the following command:
187+
To install `pg`, ensure you are in the `hyperdrive-tutorial` directory. Open your terminal and run the following command:
188188

189-
<PackageManagers pkg="postgres" comment="This should install v3.4.5 or later" />
189+
<PackageManagers pkg="pg" comment="This should install v8.13.0 or later" />
190+
191+
If you are using TypeScript, you should also install the type definitions for `pg`:
192+
193+
<PackageManagers pkg="@types/pg" dev comment="This should install v8.13.0 or later" />
190194

191195
With the driver installed, you can now create a Worker script that queries your database.
192196

@@ -217,8 +221,8 @@ The `index.ts` file is where you configure your Worker's interactions with Hyper
217221
Populate your `index.ts` file with the following code:
218222

219223
```typescript
220-
// Postgres.js 3.4.5 or later is recommended
221-
import postgres from "postgres";
224+
// pg 8.13.0 or later is recommended
225+
import { Client } from "pg";
222226

223227
export interface Env {
224228
// If you set another name in the Wrangler config file as the value for 'binding',
@@ -228,27 +232,24 @@ export interface Env {
228232

229233
export default {
230234
async fetch(request, env, ctx): Promise<Response> {
231-
// Create a connection using the Postgres.js driver (or any supported driver, ORM or query builder)
232-
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
233-
const sql = postgres(env.HYPERDRIVE.connectionString, {
234-
// Workers limit the number of concurrent external connections, so be sure to limit
235-
// the size of the local connection pool that postgres.js may establish.
236-
max: 5,
237-
238-
// If you are not using array types in your Postgres schema,
239-
// disabling this will save you an extra round-trip every time you connect.
240-
fetch_types: false,
241-
});
235+
// Create a client using the pg driver (or any supported driver, ORM or query builder)
236+
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
237+
const sql = new Client({
238+
connectionString: env.HYPERDRIVE.connectionString,
239+
});
242240

243241
try {
242+
// Connect to the database
243+
await sql.connect();
244+
244245
// Sample query
245-
const results = await sql`SELECT * FROM pg_tables`;
246+
const results = await sql.query(`SELECT * FROM pg_tables`);
246247

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

250251
// Return result rows as JSON
251-
return Response.json(results);
252+
return Response.json(results.rows);
252253
} catch (e) {
253254
console.error(e);
254255
return Response.json(
@@ -263,7 +264,7 @@ export default {
263264
Upon receiving a request, the code above does the following:
264265

265266
1. Creates a new database client configured to connect to your database via Hyperdrive, using the Hyperdrive connection string.
266-
2. Initiates a query via `await sql` that outputs all tables (user and system created) in the database (as an example query).
267+
2. Initiates a query via `await sql.query()` that outputs all tables (user and system created) in the database (as an example query).
267268
3. Returns the response as JSON to the client.
268269

269270
</TabItem>

0 commit comments

Comments
 (0)