Skip to main content

Cloudflare Workers

History
VersionChanges
v3.13.0
Support for non-eval parsers by using disableEval option.

Prerequisites​

To learn how to create a Cloudflare Worker project, please refer to Cloudflare Workers Documentation.

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:

{
"compatibility_flags": ["nodejs_compat"]
}
important

The minimum compatibility date is 2025-02-24, for example:

{
"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:

import { createConnection } from 'mysql2/promise';

export default {
async fetch(): Promise<Response> {
const conn = await createConnection({
host: 'localhost',
user: 'root',
database: 'test',
disableEval: true,
});

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 connection pool.