Skip to content

Commit a79253d

Browse files
Mini256wellwelwel
andauthored
feat: support Cloudflare Workers (#2289)
* feat: add mysql2 on cloudflare docs * lint * Update website/docs/documentation/connect-on-cloudflare.mdx --------- Co-authored-by: Weslley Araújo <[email protected]>
1 parent a0c70a2 commit a79253d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { History } from '@site/src/components/History';
2+
3+
# Cloudflare Workers
4+
5+
<History
6+
records={[
7+
{
8+
version: '3.13.0',
9+
changes: [
10+
<>
11+
Support for <em>non-eval</em> parsers by using{' '}
12+
<strong>disableEval</strong> option.
13+
</>,
14+
],
15+
},
16+
]}
17+
/>
18+
19+
## Prerequisites
20+
21+
- A [Cloudflare](https://dash.cloudflare.com/sign-up) account.
22+
- [Wrangler](https://developers.cloudflare.com/workers/wrangler/) installed.
23+
- **MySQL2** version `3.13.0` or higher.
24+
25+
To learn how to create a **Cloudflare Worker** project, please refer to [**Cloudflare Workers Documentation**](https://developers.cloudflare.com/workers/get-started/guide/).
26+
27+
## Setup
28+
29+
### Wrangler
30+
31+
**MySQL2** relies on **Node.js** modules, such as `net`, `events`, `stream`, `tls`, etc. You can enable **Node.js** compatibility for **Cloudflare Workers** by using the `"nodejs_compat"` flag through the `wrangler.jsonc` file:
32+
33+
```json
34+
{
35+
"compatibility_flags": ["nodejs_compat"]
36+
}
37+
```
38+
39+
:::important
40+
The minimum compatibility date is `2025-02-24`, for example:
41+
42+
```json
43+
{
44+
"compatibility_date": "2025-02-24",
45+
"compatibility_flags": ["nodejs_compat"]
46+
}
47+
```
48+
49+
:::
50+
51+
### MySQL2 connection
52+
53+
**MySQL2** uses a code generation-based parser for performance by default, but since **Cloudflare Workers** don't offer support for evaluations, you can disable it by using the `disableEval` option:
54+
55+
```ts
56+
import { createConnection } from 'mysql2/promise';
57+
58+
export default {
59+
async fetch(): Promise<Response> {
60+
const conn = await createConnection({
61+
host: 'localhost',
62+
user: 'root',
63+
database: 'test',
64+
// highlight-start
65+
disableEval: true,
66+
// highlight-end
67+
});
68+
69+
const [results] = await conn.query('SHOW TABLES;');
70+
71+
return new Response(JSON.stringify(results));
72+
},
73+
} satisfies ExportedHandler<Env>;
74+
```
75+
76+
:::tip
77+
For required **SSL** connections, it is recommended to use the [**Cloudflare Hyperdrive**](https://developers.cloudflare.com/hyperdrive/) connection pool.
78+
:::

0 commit comments

Comments
 (0)