-
-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <46850407+wellwelwel@users.noreply.github.com>
- Loading branch information
1 parent
a0c70a2
commit a79253d
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { History } from '@site/src/components/History'; | ||
|
||
# Cloudflare Workers | ||
|
||
<History | ||
records={[ | ||
{ | ||
version: '3.13.0', | ||
changes: [ | ||
<> | ||
Support for <em>non-eval</em> parsers by using{' '} | ||
<strong>disableEval</strong> option. | ||
</>, | ||
], | ||
}, | ||
]} | ||
/> | ||
|
||
## Prerequisites | ||
|
||
- A [Cloudflare](https://dash.cloudflare.com/sign-up) account. | ||
- [Wrangler](https://developers.cloudflare.com/workers/wrangler/) installed. | ||
- **MySQL2** version `3.13.0` or higher. | ||
|
||
To learn how to create a **Cloudflare Worker** project, please refer to [**Cloudflare Workers Documentation**](https://developers.cloudflare.com/workers/get-started/guide/). | ||
|
||
## Setup | ||
|
||
### Wrangler | ||
|
||
**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: | ||
|
||
```json | ||
{ | ||
"compatibility_flags": ["nodejs_compat"] | ||
} | ||
``` | ||
|
||
:::important | ||
The minimum compatibility date is `2025-02-24`, for example: | ||
|
||
```json | ||
{ | ||
"compatibility_date": "2025-02-24", | ||
"compatibility_flags": ["nodejs_compat"] | ||
} | ||
``` | ||
|
||
::: | ||
|
||
### MySQL2 connection | ||
|
||
**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: | ||
|
||
```ts | ||
import { createConnection } from 'mysql2/promise'; | ||
|
||
export default { | ||
async fetch(): Promise<Response> { | ||
const conn = await createConnection({ | ||
host: 'localhost', | ||
user: 'root', | ||
database: 'test', | ||
// highlight-start | ||
disableEval: true, | ||
// highlight-end | ||
}); | ||
|
||
const [results] = await conn.query('SHOW TABLES;'); | ||
|
||
return new Response(JSON.stringify(results)); | ||
}, | ||
} satisfies ExportedHandler<Env>; | ||
``` | ||
|
||
:::tip | ||
For required **SSL** connections, it is recommended to use the [**Cloudflare Hyperdrive**](https://developers.cloudflare.com/hyperdrive/) connection pool. | ||
::: |