You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hyperdrive accelerates access to your existing databases from Cloudflare Workers, making even single-region databases feel globally distributed.
11
11
@@ -19,19 +19,19 @@ This guide will instruct you through:
19
19
- Creating a [Cloudflare Worker](/workers/) and binding it to your Hyperdrive configuration.
20
20
- Establishing a database connection from your Worker to a public database.
21
21
22
-
## Prerequisites
23
-
24
-
:::note[Workers Paid plan required]
22
+
:::note
25
23
26
-
Hyperdrive is available to all users on the [Workers Paid plan](/workers/platform/pricing/#workers).
24
+
Hyperdrive currently works with PostgreSQL, MySQL and compatible databases. This includes CockroachDB and Materialize (which are PostgreSQL-compatible), and MariaDB and Planetscale (which are MySQL compatible).
27
25
28
26
:::
29
27
30
-
To continue:
28
+
## Prerequisites
29
+
30
+
Before you begin, ensure you have completed the following:
31
31
32
32
1. Sign up for a [Cloudflare account](https://dash.cloudflare.com/sign-up/workers-and-pages) if you have not already.
33
-
2. Install [`Node.js`](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm). Use a Node version manager like [Volta](https://volta.sh/) or [nvm](https://github.com/nvm-sh/nvm) to avoid permission issues and change Node.js versions. [Wrangler](/workers/wrangler/install-and-update/) requires a Node version of `16.17.0` or later.
34
-
3. Have **a publicly accessible PostgreSQL (or PostgreSQL compatible) database**. Cloudflare recommends [Neon](https://neon.tech/) if you do not have an existing database. Read the [Neon documentation](https://neon.tech/docs/introduction) to create your first database.
33
+
2. Install [`Node.js`](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm). Use a Node version manager like [nvm](https://github.com/nvm-sh/nvm) or [Volta](https://volta.sh/) to avoid permission issues and change Node.js versions. [Wrangler](/workers/wrangler/install-and-update/) requires a Node version of `16.17.0` or later.
34
+
3. Have **a publicly accessible** PostgreSQL/MySQL (or compatible) database.
35
35
36
36
## 1. Log in
37
37
@@ -76,46 +76,49 @@ This will create a new `hyperdrive-tutorial` directory. Your new `hyperdrive-tut
76
76
77
77
### Enable Node.js compatibility
78
78
79
-
[Node.js compatibility](/workers/runtime-apis/nodejs/) is required for database drivers, including Postgres.js, and needs to be configured for your Workers project.
79
+
[Node.js compatibility](/workers/runtime-apis/nodejs/) is required for database drivers, and needs to be configured for your Workers project.
80
80
81
81
<Renderfile="nodejs_compat"product="workers" />
82
82
83
83
## 3. Connect Hyperdrive to a database
84
84
85
-
:::note
86
-
87
-
Hyperdrive currently works with PostgreSQL and PostgreSQL compatible databases, including CockroachDB and Materialize.
85
+
Hyperdrive works by connecting to your database, pooling database connections globally and speeding up your database access through Cloudflare's network.
88
86
89
-
Support for other database engines, including MySQL, is on the roadmap.
90
-
91
-
:::
92
-
93
-
Hyperdrive works by connecting to your database.
87
+
It will provide a secure connection string that is only accessible from your Worker, that you can use to connect to your database through Hyperdrive.
88
+
This means that you can use the Hyperdrive connection string with your existing drivers or ORM libraries without needing significant changes to your code.
94
89
95
90
To create your first Hyperdrive database configuration, change into the directory you just created for your Workers project:
96
91
97
92
```sh
98
93
cd hyperdrive-tutorial
99
94
```
100
95
101
-
:::note
102
-
103
-
Support for the new `hyperdrive` commands in the wrangler CLI requires a wrangler version of `3.10.0` or later. You can use `npx wrangler@latest` to always ensure you are using the latest version of Wrangler.
104
-
105
-
:::
106
-
107
96
To create your first Hyperdrive, you will need:
108
97
109
98
- The IP address (or hostname) and port of your database.
110
99
- The database username (for example, `hyperdrive-demo`).
111
100
- The password associated with that username.
112
-
- The name of the database you want Hyperdrive to connect to. For example, `postgres`.
101
+
- The name of the database you want Hyperdrive to connect to. For example, `postgres` or `mysql`.
113
102
114
103
Hyperdrive accepts the combination of these parameters in the common connection string format used by database drivers:
If you wish to disable caching, pass the flag `--caching-disabled`.
132
+
By default, Hyperdrive will cache query results. If you wish to disable caching, pass the flag `--caching-disabled`.
130
133
131
134
Alternatively, you can use the `--max-age` flag to specify the maximum duration (in seconds) for which items should persist in the cache, before they are evicted. Default value is 60 seconds.
132
135
@@ -137,14 +140,13 @@ If successful, the command will output your new Hyperdrive configuration:
Once you have created a Hyperdrive configuration and bound it to your Worker, you can run a query against your database.
167
+
165
168
### Install a database driver
166
169
170
+
<Tabs>
171
+
<TabItemlabel="PostgreSQL">
172
+
167
173
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.
168
174
169
175
To install `postgres`, ensure you are in the `hyperdrive-tutorial` directory. Open your terminal and run the following command:
@@ -172,8 +178,24 @@ To install `postgres`, ensure you are in the `hyperdrive-tutorial` directory. Op
172
178
173
179
With the driver installed, you can now create a Worker script that queries your database.
174
180
181
+
</TabItem>
182
+
<TabItemlabel="MySQL">
183
+
184
+
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 [mysql2](https://github.com/sidorares/node-mysql2), one of the most widely used MySQL drivers.
185
+
186
+
To install `mysql2`, ensure you are in the `hyperdrive-tutorial` directory. Open your terminal and run the following command:
187
+
188
+
<PackageManagerspkg="mysql2"comment="This should install v3.13.0 or later" />
189
+
190
+
With the driver installed, you can now create a Worker script that queries your database.
191
+
192
+
</TabItem>
193
+
</Tabs>
194
+
175
195
### Write a Worker
176
196
197
+
<Tabs>
198
+
<TabItemlabel="PostgreSQL">
177
199
After you have set up your database, you will run a SQL query from within your Worker.
178
200
179
201
Go to your `hyperdrive-tutorial` Worker and open the `index.ts` file.
0 commit comments