Skip to content

Commit 253c9cf

Browse files
thomasgauvinOxyjun
authored andcommitted
[Hyperdrive] add drivers and orms docs (#22125)
* [Hyperdrive] drafting custom cert support for Hyperdrive * corrections * wip * add changelog * adjust per comments * add note on wrangler version * add troubleshooting guides for ssl certs * change changelog entry date * Update src/content/docs/hyperdrive/configuration/tls-ssl-certificates-for-hyperdrive.mdx * Update src/content/docs/hyperdrive/configuration/tls-ssl-certificates-for-hyperdrive.mdx * PCX review * add drivers and orms to hyperdrive docs * Apply suggestions from code review * Apply suggestions from code review * Specifying product when rendering partials * Importing Render components * Fixing invalid links * adjust per discord feedback * more nits * Pushing changes to Postgres Drizzle ORM tutorial * Editing MySQL version of the tutorial * Adding note about when to run the optional migration step. * Removing outdated redirects to avoid breaking * Adding missing redirects. --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 0defd49 commit 253c9cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+767
-234
lines changed

public/__redirects

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,26 +1712,45 @@
17121712
/hyperdrive/learning/how-hyperdrive-works/ /hyperdrive/configuration/how-hyperdrive-works/ 301
17131713
/hyperdrive/learning/connect-to-postgres/ /hyperdrive/examples/connect-to-postgres/ 301
17141714
/hyperdrive/learning/query-caching/ /hyperdrive/configuration/query-caching/ 301
1715-
/hyperdrive/learning/troubleshooting/ /hyperdrive/reference/troubleshooting/ 301
1715+
/hyperdrive/learning/troubleshooting/ /hyperdrive/observability/troubleshooting/ 301
17161716
/hyperdrive/changelog/ /hyperdrive/platform/release-notes/ 301
17171717
/hyperdrive/platform/changelog/ /hyperdrive/platform/release-notes/ 301
17181718
/hyperdrive/learning/ /hyperdrive/configuration/ 301
17191719
/hyperdrive/reference/metrics/ /hyperdrive/observability/metrics/ 301
17201720
/hyperdrive/reference/troubleshooting/ /hyperdrive/observability/troubleshooting/ 301
1721-
/hyperdrive/examples/aws-rds-aurora/ /hyperdrive/examples/connect-to-postgres/aws-rds-aurora/ 301
1722-
/hyperdrive/examples/azure/ /hyperdrive/examples/connect-to-postgres/azure/ 301
1723-
/hyperdrive/examples/cockroachdb/ /hyperdrive/examples/connect-to-postgres/cockroachdb/ 301
1724-
/hyperdrive/examples/digital-ocean/ /hyperdrive/examples/connect-to-postgres/digital-ocean/ 301
1725-
/hyperdrive/examples/google-cloud-sql/ /hyperdrive/examples/connect-to-postgres/google-cloud-sql/ 301
1726-
/hyperdrive/examples/materialize/ /hyperdrive/examples/connect-to-postgres/materialize/ 301
1727-
/hyperdrive/examples/neon/ /hyperdrive/examples/connect-to-postgres/neon/ 301
1728-
/hyperdrive/examples/nile/ /hyperdrive/examples/connect-to-postgres/nile/ 301
1729-
/hyperdrive/examples/pgedge/ /hyperdrive/examples/connect-to-postgres/pgedge/ 301
1730-
/hyperdrive/examples/supabase/ /hyperdrive/examples/connect-to-postgres/supabase/ 301
1731-
/hyperdrive/examples/timescale/ /hyperdrive/examples/connect-to-postgres/timescale/ 301
1732-
/hyperdrive/examples/xata/ /hyperdrive/examples/connect-to-postgres/xata/ 301
1733-
/hyperdrive/examples/fly/ /hyperdrive/examples/connect-to-postgres/fly/ 301
17341721

1722+
/hyperdrive/examples/aws-rds-aurora/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/aws-rds-aurora/ 301
1723+
/hyperdrive/examples/azure/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/azure/ 301
1724+
/hyperdrive/examples/cockroachdb/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/cockroachdb/ 301
1725+
/hyperdrive/examples/digital-ocean/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/digital-ocean/ 301
1726+
/hyperdrive/examples/google-cloud-sql/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/google-cloud-sql/ 301
1727+
/hyperdrive/examples/materialize/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/materialize/ 301
1728+
/hyperdrive/examples/neon/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/ 301
1729+
/hyperdrive/examples/nile/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/nile/ 301
1730+
/hyperdrive/examples/pgedge/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/pgedge/ 301
1731+
/hyperdrive/examples/supabase/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/ 301
1732+
/hyperdrive/examples/timescale/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/timescale/ 301
1733+
/hyperdrive/examples/xata/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/xata/ 301
1734+
/hyperdrive/examples/fly/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/fly/ 301
1735+
1736+
/hyperdrive/examples/connect-to-postgres/aws-rds-aurora/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/aws-rds-aurora/ 301
1737+
/hyperdrive/examples/connect-to-postgres/azure/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/azure/ 301
1738+
/hyperdrive/examples/connect-to-postgres/cockroachdb/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/cockroachdb/ 301
1739+
/hyperdrive/examples/connect-to-postgres/digital-ocean/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/digital-ocean/ 301
1740+
/hyperdrive/examples/connect-to-postgres/fly/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/fly/ 301
1741+
/hyperdrive/examples/connect-to-postgres/google-cloud-sql/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/google-cloud-sql/ 301
1742+
/hyperdrive/examples/connect-to-postgres/materialize/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/materialize/ 301
1743+
/hyperdrive/examples/connect-to-postgres/neon/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/ 301
1744+
/hyperdrive/examples/connect-to-postgres/nile/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/nile/ 301
1745+
/hyperdrive/examples/connect-to-postgres/pgedge/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/pgedge/ 301
1746+
/hyperdrive/examples/connect-to-postgres/supabase/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/ 301
1747+
/hyperdrive/examples/connect-to-postgres/timescale/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/timescale/ 301
1748+
/hyperdrive/examples/connect-to-postgres/xata/ /hyperdrive/examples/connect-to-postgres/postgres-database-providers/xata/ 301
1749+
1750+
/hyperdrive/examples/connect-to-mysql/planetscale/ /hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale/ 301
1751+
/hyperdrive/examples/connect-to-mysql/azure/ /hyperdrive/examples/connect-to-mysql/mysql-database-providers/azure/ 301
1752+
/hyperdrive/examples/connect-to-mysql/google-cloud-sql/ /hyperdrive/examples/connect-to-mysql/mysql-database-providers/google-cloud-sql/ 301
1753+
/hyperdrive/examples/connect-to-mysql/aws-rds-aurora/ /hyperdrive/examples/connect-to-mysql/mysql-database-providers/aws-rds-aurora/ 301
17351754

17361755
/hyperdrive/reference/supported-databases/ /hyperdrive/reference/supported-databases-and-features/ 301
17371756
/hyperdrive/configuration/connect-to-postgres/ /hyperdrive/examples/connect-to-postgres/ 301

src/content/docs/hyperdrive/configuration/connect-to-private-database.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Validate that you can connect to your database from Workers and make queries.
199199

200200
Use Postgres.js to send a test query to validate that the connection has been successful.
201201

202-
<Render file="use-postgresjs-to-make-query" product="hyperdrive" />
202+
<Render file="use-postgres-js-to-make-query" product="hyperdrive" />
203203

204204
Now, deploy your Worker:
205205

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

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,7 @@ npx wrangler hyperdrive create my-first-hyperdrive --connection-string="mysql://
3232

3333
The command above will output the ID of your Hyperdrive, which you will need to set in the [Wrangler configuration file](/workers/wrangler/configuration/) for your Workers project:
3434

35-
<WranglerConfig>
36-
37-
```toml
38-
# required for database drivers to function
39-
compatibility_flags = ["nodejs_compat"]
40-
compatibility_date = "2024-09-23"
41-
42-
[[hyperdrive]]
43-
binding = "HYPERDRIVE"
44-
id = "<your-hyperdrive-id-here>"
45-
```
46-
47-
</WranglerConfig>
35+
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive" />
4836

4937
This will allow Hyperdrive to generate a dynamic connection string within your Worker that you can pass to your existing database driver. Refer to [Driver examples](#driver-examples) to learn how to set up a database driver with Hyperdrive.
5038

@@ -105,57 +93,9 @@ The following Workers code shows you how to use [mysql2](https://github.com/sido
10593

10694
### `mysql`
10795

108-
Install the `mysql` driver:
96+
The following Workers code shows you how to use [mysql](https://github.com/mysqljs/mysql) with Hyperdrive.
10997

110-
```sh
111-
npm install mysql
112-
```
113-
114-
**Ensure you have `compatibility_flags` and `compatibility_date` set in your [Wrangler configuration file](/workers/wrangler/configuration/)** as shown below:
115-
116-
<Render file="nodejs-compat-wrangler-toml" product="workers" />
117-
118-
Create a new connection and pass the Hyperdrive parameters:
119-
120-
```ts
121-
import { createConnection } from "mysql";
122-
123-
export interface Env {
124-
HYPERDRIVE: Hyperdrive;
125-
}
126-
127-
export default {
128-
async fetch(request, env, ctx): Promise<Response> {
129-
const result = await new Promise<any>((resolve) => {
130-
const connection = createConnection({
131-
host: env.HYPERDRIVE.host,
132-
user: env.HYPERDRIVE.user,
133-
password: env.HYPERDRIVE.password,
134-
database: env.HYPERDRIVE.database,
135-
port: env.HYPERDRIVE.port,
136-
});
137-
138-
connection.connect((error: { message: string }) => {
139-
if (error) {
140-
throw new Error(error.message);
141-
}
142-
143-
connection.query("SHOW tables;", [], (error, rows, fields) => {
144-
connection.end();
145-
146-
resolve({ fields, rows });
147-
});
148-
});
149-
});
150-
151-
return new Response(JSON.stringify(result), {
152-
headers: {
153-
"Content-Type": "application/json",
154-
},
155-
});
156-
},
157-
} satisfies ExportedHandler<Env>;
158-
```
98+
<Render file="use-mysql-to-make-query" product="hyperdrive" />
15999

160100
## Identify connections from Hyperdrive
161101

src/content/docs/hyperdrive/examples/connect-to-mysql/aws-rds-aurora.mdx renamed to src/content/docs/hyperdrive/examples/connect-to-mysql/mysql-database-providers/aws-rds-aurora.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ With a database user, password, database endpoint (hostname and port), and datab
9797
## 3. Create a database configuration
9898

9999
<Render file="create-hyperdrive-config-mysql" />
100+
<Render file="create-hyperdrive-config-mysql-next-steps" />

src/content/docs/hyperdrive/examples/connect-to-mysql/azure.mdx renamed to src/content/docs/hyperdrive/examples/connect-to-mysql/mysql-database-providers/azure.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ To connect to a private Azure Database for MySQL instance, refer to [Connect to
3838
## 2. Create a database configuration
3939

4040
<Render file="create-hyperdrive-config-mysql" />
41+
<Render file="create-hyperdrive-config-mysql-next-steps" />

src/content/docs/hyperdrive/examples/connect-to-mysql/google-cloud-sql.mdx renamed to src/content/docs/hyperdrive/examples/connect-to-mysql/mysql-database-providers/google-cloud-sql.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ With the username, password, public IP address and (optional) database name (def
4141
## 2. Create a database configuration
4242

4343
<Render file="create-hyperdrive-config-mysql" />
44+
<Render file="create-hyperdrive-config-mysql-next-steps" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
type: overview
3+
pcx_content_type: navigation
4+
title: Database Providers
5+
hideChildren: false
6+
sidebar:
7+
order: 5
8+
group:
9+
hideIndex: true
10+
---
11+
12+
import { DirectoryListing } from "~/components";
13+
14+
<DirectoryListing />

src/content/docs/hyperdrive/examples/connect-to-mysql/planetscale.mdx renamed to src/content/docs/hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ With the host, database name, username and password, you can now create a Hyperd
2727
## 2. Create a database configuration
2828

2929
<Render file="create-hyperdrive-config-mysql" />
30+
31+
:::note
32+
33+
When connecting to a Planetscale database with Hyperdrive, you should use a driver like [node-postgres (pg)](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/) or [Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/) to connect directly to the underlying database instead of the [Planetscale serverless driver](https://planetscale.com/docs/tutorials/planetscale-serverless-driver). Hyperdrive is optimized for database access for Workers and will perform global connection pooling and fast query routing by connecting directly to your database.
34+
35+
:::
36+
37+
<Render file="create-hyperdrive-config-mysql-next-steps" />
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
pcx_content_type: example
3+
title: Drizzle ORM
4+
sidebar:
5+
order: 3
6+
meta:
7+
title: Using Drizzle ORM with Hyperdrive for MySQL
8+
---
9+
10+
import { Render, Steps } from "~/components";
11+
12+
[Drizzle ORM](https://orm.drizzle.team/) is a lightweight TypeScript ORM with a focus on type safety. This example demonstrates how to use Drizzle ORM with MySQL via Cloudflare Hyperdrive in a Workers application.
13+
14+
## Prerequisites
15+
16+
- A Cloudflare account with Workers access
17+
- A MySQL database
18+
- A [Hyperdrive configuration to your MySQL database](/hyperdrive/get-started/#3-connect-hyperdrive-to-a-database)
19+
20+
## 1. Install Drizzle
21+
22+
Install the Drizzle ORM and its dependencies such as the [mysql2](https://github.com/sidorares/node-mysql2) driver:
23+
24+
```sh
25+
# mysql2 v3.13.0 or later is required
26+
npm i drizzle-orm mysql2 dotenv
27+
npm i -D drizzle-kit tsx @types/node
28+
```
29+
30+
Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.jsonc` file:
31+
32+
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive"/>
33+
34+
## 2. Configure Drizzle
35+
36+
### 2.1. Define a schema
37+
38+
With Drizzle ORM, we define the schema in TypeScript rather than writing raw SQL.
39+
40+
<Steps>
41+
1. Create a folder `/db/` in `/src/`.
42+
2. Create a `schema.ts` file.
43+
3. In `schema.ts`, define a `users` table as shown below.
44+
45+
```ts title="src/db/schema.ts"
46+
// src/schema.ts
47+
import { mysqlTable, int, varchar, timestamp } from "drizzle-orm/mysql-core";
48+
49+
export const users = mysqlTable("users", {
50+
id: int("id").primaryKey().autoincrement(),
51+
name: varchar("name", { length: 255 }).notNull(),
52+
email: varchar("email", { length: 255 }).notNull().unique(),
53+
createdAt: timestamp("created_at").defaultNow(),
54+
});
55+
```
56+
</Steps>
57+
58+
### 2.2. Connect Drizzle ORM to the database with Hyperdrive
59+
60+
Use your the credentials of your Hyperdrive configuration for your database when using the Drizzle ORM.
61+
62+
Populate your `index.ts` file as shown below.
63+
64+
```ts title="src/index.ts"
65+
// src/index.ts
66+
67+
import { drizzle } from "drizzle-orm/mysql2";
68+
import { createConnection } from "mysql2";
69+
import { users } from "./db/schema";
70+
71+
export interface Env {
72+
HYPERDRIVE: Hyperdrive;
73+
}
74+
75+
export default {
76+
async fetch(request, env, ctx): Promise<Response> {
77+
// Create a connection using the mysql2 driver with the Hyperdrive credentials (only accessible from your Worker).
78+
const connection = await createConnection({
79+
host: env.HYPERDRIVE.host,
80+
user: env.HYPERDRIVE.user,
81+
password: env.HYPERDRIVE.password,
82+
database: env.HYPERDRIVE.database,
83+
port: env.HYPERDRIVE.port,
84+
85+
// Required to enable mysql2 compatibility for Workers
86+
disableEval: true,
87+
});
88+
89+
// Create the Drizzle client with the mysql2 driver connection
90+
const db = drizzle(connection);
91+
92+
// Sample query to get all users
93+
const allUsers = await db.select().from(users);
94+
95+
return Response.json(allUsers);
96+
},
97+
} satisfies ExportedHandler<Env>;
98+
```
99+
100+
### 2.3. Configure Drizzle-Kit for migrations (optional)
101+
102+
:::note
103+
You need to set up the tables in your database so that Drizzle ORM can make queries that work.
104+
105+
If you have already set it up (for example, if another user has applied the schema to your database), or if you are starting to use Drizzle ORM and the schema matches what already exists in your database, then you do not need to run the migration.
106+
:::
107+
108+
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/mysql-new) for additional guidance.
109+
110+
<Steps>
111+
1. Create a `.env` file in the root folder of your project, and add your database connection string. The Drizzle Kit CLI will use this connection string to create and apply the migrations.
112+
113+
```toml title=".env"
114+
# .env
115+
# Replace with your direct database connection string
116+
DATABASE_URL='mysql://user:[email protected]/database-name'
117+
```
118+
119+
2. Create a `drizzle.config.ts` file in the root folder of your project to configure Drizzle Kit and add the following content:
120+
121+
```ts title="drizzle.config.ts"
122+
import 'dotenv/config';
123+
import { defineConfig } from 'drizzle-kit';
124+
export default defineConfig({
125+
out: './drizzle',
126+
schema: './src/db/schema.ts',
127+
dialect: 'mysql',
128+
dbCredentials: {
129+
url: process.env.DATABASE_URL!,
130+
},
131+
});
132+
```
133+
134+
3. Generate the migration file for your database according to your schema files and apply the migrations to your database.
135+
136+
```bash
137+
npx drizzle-kit generate
138+
```
139+
```bash output
140+
No config path provided, using default 'drizzle.config.ts'
141+
Reading config file 'drizzle.config.ts'
142+
Reading schema files:
143+
/src/db/schema.ts
144+
145+
1 tables
146+
users 4 columns 0 indexes 0 fks
147+
148+
[✓] Your SQL migration file ➜ drizzle/0000_daffy_rhodey.sql 🚀
149+
```
150+
```bash
151+
npx drizzle-kit migrate
152+
```
153+
```bash output
154+
No config path provided, using default 'drizzle.config.ts'
155+
Reading config file 'drizzle.config.ts'
156+
```
157+
</Steps>
158+
159+
## 3. Deploy your Worker
160+
161+
Deploy your Worker.
162+
163+
```bash
164+
npx wrangler deploy
165+
```
166+
167+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Libraries and Drivers
4+
sidebar:
5+
order: 8
6+
group:
7+
hideIndex: true
8+
---
9+
10+
import { DirectoryListing } from "~/components";
11+
12+
<DirectoryListing />

0 commit comments

Comments
 (0)