Skip to content

Commit 3ab2b78

Browse files
authored
Add section in Pages Functions Bindings page describing how to use Hyperdrive bindings (#17386)
1 parent 4b0dbab commit 3ab2b78

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/content/docs/pages/functions/bindings.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,70 @@ export const onRequest: PagesFunction<Env> = async (context) => {
606606

607607
If using a queue producer binding with a Pages Function, you will be able to send events to a queue locally. However, it is not possible to consume events from a queue with a Pages Function. You will have to create a [separate consumer Worker](/queues/get-started/#5-create-your-consumer-worker) with a [queue consumer handler](/queues/configuration/javascript-apis/#consumer) to consume events from the queue. Wrangler does not yet support running separate producer Functions and consumer Workers bound to the same queue locally.
608608

609+
## Hyperdrive configs
610+
611+
:::note
612+
613+
PostgreSQL drivers like [`Postgres.js`](https://github.com/porsager/postgres) depend on Node.js APIs and as such Functions with Hyperdrive config bindings must be [deployed with Node.js compatibility](/workers/runtime-apis/nodejs).
614+
615+
:::
616+
617+
[Hyperdrive](/hyperdrive/) is a service for connecting to your existing databases from Cloudflare Workers and Pages Functions.
618+
619+
To bind your Hyperdrive config to your Pages Function, you can configure a Hyperdrive binding in [`wrangler.toml`](/pages/functions/wrangler-configuration/#hyperdrive) or the Cloudflare dashboard.
620+
621+
Below is an example of how to use Hyperdrive in your Function. In the following example, your Hyperdrive config is named `HYPERDRIVE` and you can access the binding in your Function code on `context.env`:
622+
623+
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
624+
625+
```js
626+
import postgres from "postgres";
627+
628+
export async function onRequest(context) {
629+
// create connection to postgres database
630+
const sql = postgres(context.env.HYPERDRIVE.connectionString);
631+
632+
try {
633+
const result = await sql`SELECT id, name, value FROM records`;
634+
635+
return Response.json({result: result})
636+
} catch (e) {
637+
return Response.json({error: e.message, {status: 500}});
638+
}
639+
}
640+
```
641+
642+
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
643+
644+
```ts
645+
import postgres from "postgres";
646+
647+
interface Env {
648+
HYPERDRIVE: Hyperdrive;
649+
}
650+
651+
type MyRecord = {
652+
id: number;
653+
name: string;
654+
value: string;
655+
};
656+
657+
export const onRequest: PagesFunction<Env> = async (context) => {
658+
// create connection to postgres database
659+
const sql = postgres(context.env.HYPERDRIVE.connectionString);
660+
661+
try {
662+
const result = await sql<MyRecord[]>`SELECT id, name, value FROM records`;
663+
664+
return Response.json({result: result})
665+
} catch (e) {
666+
return Response.json({error: e.message, {status: 500}});
667+
}
668+
};
669+
```
670+
671+
</TabItem> </Tabs>
672+
609673
## Analytics Engine
610674

611675
The [Analytics Engine](/analytics/analytics-engine/) binding enables you to write analytics within your Pages Function.

0 commit comments

Comments
 (0)