Skip to content

Commit 03cb1cc

Browse files
committed
adjust per discord feedback
1 parent 0410c7b commit 03cb1cc

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

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

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

4545
| Driver | Documentation | Minimum Version Required | Notes |
4646
| ---------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
47-
| Postgres.js (**recommended**) | [Postgres.js documentation](https://github.com/porsager/postgres) | `[email protected]` | Supported in both Workers & Pages. |
47+
| Postgres.js | [Postgres.js documentation](https://github.com/porsager/postgres) | `[email protected]` | Supported in both Workers & Pages. |
4848
| 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`^ | |

src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ npm i -D drizzle-kit tsx @types/node
2929

3030
Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.jsonc` file:
3131

32-
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive"/>
32+
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive" />
3333

3434
## 2. Configure Drizzle
3535

@@ -66,7 +66,12 @@ export interface Env {
6666
export default {
6767
async fetch(request, env, ctx): Promise<Response> {
6868
// Create a database client with postgres.js driver connected via Hyperdrive
69-
const sql = postgres(env.HYPERDRIVE.connectionString);
69+
const sql = postgres(env.HYPERDRIVE.connectionString, {
70+
// Limit the connections for the Worker request to 5 due to Workers' limits on concurrent external connections
71+
max: 5,
72+
// If you are not using array types in your Postgres schema, disable `fetch_types` to avoid an additional round-trip (unnecessary latency)
73+
fetch_types: false,
74+
});
7075

7176
// Create the Drizzle client with the postgres.js connection
7277
const db = drizzle(sql);
@@ -82,6 +87,13 @@ export default {
8287
} satisfies ExportedHandler<Env>;
8388
```
8489

90+
:::note
91+
92+
You may use [node-postgres](https://orm.drizzle.team/docs/get-started-postgresql#node-postgres) or [Postgres.js](https://orm.drizzle.team/docs/get-started-postgresql#postgresjs)
93+
when using Drizzle ORM. Both are supported and compatible.
94+
95+
:::
96+
8597
### 2.3 Configure Drizzle-Kit for migrations (optional)
8698

8799
You can generate and run SQL migrations on your database based on your schema using Drizzle Kit CLI. Refer to [Drizzle ORM docs](https://orm.drizzle.team/docs/get-started/postgresql) for additional guidance.
@@ -95,22 +107,22 @@ You can generate and run SQL migrations on your database based on your schema us
95107

96108
2. Create a `drizzle.config.ts` file in the root of your project to configure Drizzle Kit and add the following content:
97109

98-
```ts
99-
import "dotenv/config";
100-
import { defineConfig } from "drizzle-kit";
101-
export default defineConfig({
102-
out: "./drizzle",
103-
schema: "./src/db/schema.ts",
104-
dialect: "postgresql",
105-
dbCredentials: {
106-
url: process.env.DATABASE_URL!,
107-
},
108-
});
109-
```
110+
```ts
111+
import "dotenv/config";
112+
import { defineConfig } from "drizzle-kit";
113+
export default defineConfig({
114+
out: "./drizzle",
115+
schema: "./src/db/schema.ts",
116+
dialect: "postgresql",
117+
dbCredentials: {
118+
url: process.env.DATABASE_URL!,
119+
},
120+
});
121+
```
110122

111123
3. Generate the migration file for your database according to your schema files and apply the migrations to your database.
112124

113-
```bash
114-
npx drizzle-kit generate
115-
npx drizzle-kit migrate
116-
```
125+
```bash
126+
npx drizzle-kit generate
127+
npx drizzle-kit migrate
128+
```

src/content/partials/hyperdrive/use-node-postgres-to-make-query.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Install the `node-postgres` driver:
88

99
```sh
1010
npm install pg
11+
# If using TypeScript
12+
npm i --save-dev @types/pg
1113
```
1214

1315
Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.jsonc` file:
@@ -48,6 +50,8 @@ export default {
4850
});
4951
} catch (error: any) {
5052
console.error("Database error:", error.message);
53+
54+
return Response.error();
5155
}
5256
},
5357
};

src/content/partials/hyperdrive/use-postgres-js-to-make-query.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export default {
4848
return Response.json({ success: true, result: result });
4949
} catch (e: any) {
5050
console.error("Database error:", e.message);
51+
52+
return Response.error();
5153
}
5254
},
5355
} satisfies ExportedHandler<Env>;

0 commit comments

Comments
 (0)