|
| 1 | +# Run on Cloudflare Workers |
| 2 | + |
| 3 | +This document is a step-by-step tutorial on how to use `node-mysql2` on Cloudflare Workers. |
| 4 | + |
| 5 | +## Before you begin |
| 6 | + |
| 7 | +Before you try the steps in this article, you need to prepare the following things: |
| 8 | + |
| 9 | +- A [Cloudflare Workers](https://dash.cloudflare.com/login) account. |
| 10 | +- npm is installed. |
| 11 | + |
| 12 | +## Step 1: Set up project |
| 13 | + |
| 14 | +[Wrangler](https://developers.cloudflare.com/workers/wrangler/) is the official Cloudflare Worker CLI. You can use it to generate, build, preview, and publish your Workers. |
| 15 | + |
| 16 | +1. Install Wrangler: |
| 17 | + |
| 18 | + ``` |
| 19 | + npm install wrangler |
| 20 | + ``` |
| 21 | + |
| 22 | +2. To authenticate Wrangler, run wrangler login: |
| 23 | + |
| 24 | + ``` |
| 25 | + wrangler login |
| 26 | + ``` |
| 27 | +
|
| 28 | +3. Use Wrangler to create a project: |
| 29 | +
|
| 30 | + ``` |
| 31 | + wrangler init mysql2-cloudflare -y |
| 32 | + ``` |
| 33 | +
|
| 34 | +## Step 2: Install node-mysql2 |
| 35 | +
|
| 36 | +1. Enter your project directory: |
| 37 | +
|
| 38 | + ``` |
| 39 | + cd mysql2-cloudflare |
| 40 | + ``` |
| 41 | +
|
| 42 | +2. Install the node-mysql2 with npm: |
| 43 | +
|
| 44 | + ``` |
| 45 | + npm install mysql2 |
| 46 | + ``` |
| 47 | +
|
| 48 | + This adds the node-mysql2 dependency in `package.json`. |
| 49 | +
|
| 50 | +## Step 3: Use node-mysql2 on Cloudflare Workers |
| 51 | +
|
| 52 | +Develop your code in the `src/index.ts`. Here are some examples: |
| 53 | +
|
| 54 | +1. local mysql example |
| 55 | +
|
| 56 | + ```ts |
| 57 | + import { createConnection } from 'mysql2'; |
| 58 | +
|
| 59 | + export default { |
| 60 | + async fetch( |
| 61 | + request: Request, |
| 62 | + env: Env, |
| 63 | + ctx: ExecutionContext |
| 64 | + ): Promise<Response> { |
| 65 | + let result |
| 66 | + const connection = createConnection( |
| 67 | + { |
| 68 | + host: '127.0.0.1', |
| 69 | + port: 3306, |
| 70 | + user: 'user', |
| 71 | + password: 'password', |
| 72 | + useStaticParser:true |
| 73 | + }); |
| 74 | + connection.query( |
| 75 | + 'show databases', |
| 76 | + function(err, rows, fields) { |
| 77 | + if (err) { |
| 78 | + throw err |
| 79 | + } |
| 80 | + result = rows |
| 81 | + } |
| 82 | + ); |
| 83 | + await sleep(2000); |
| 84 | + return new Response(JSON.stringify(result)) |
| 85 | + }, |
| 86 | + }; |
| 87 | +
|
| 88 | + async function sleep(ms) { |
| 89 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 90 | + } |
| 91 | + ``` |
| 92 | + |
| 93 | +2. TiDB Serverless example with TLS: |
| 94 | + |
| 95 | + ```js |
| 96 | + import { createConnection } from 'mysql2'; |
| 97 | + |
| 98 | + export default { |
| 99 | + async fetch( |
| 100 | + request: Request, |
| 101 | + env: Env, |
| 102 | + ctx: ExecutionContext |
| 103 | + ): Promise<Response> { |
| 104 | + let result |
| 105 | + const connection = createConnection( |
| 106 | + { |
| 107 | + host: 'gateway01.ap-southeast-1.prod.aws.tidbcloud.com', |
| 108 | + port: 4000, |
| 109 | + user: 'your_user', |
| 110 | + password: 'your_password', |
| 111 | + database: 'test', |
| 112 | + ssl: { |
| 113 | + minVersion: 'TLSv1.2', |
| 114 | + rejectUnauthorized: true |
| 115 | + }, |
| 116 | + useStaticParser:true |
| 117 | + }); |
| 118 | + connection.query( |
| 119 | + 'show databases', |
| 120 | + function(err, rows, fields) { |
| 121 | + if (err) { |
| 122 | + throw err |
| 123 | + } |
| 124 | + result = rows |
| 125 | + } |
| 126 | + ); |
| 127 | + await sleep(2000); |
| 128 | + return new Response(JSON.stringify(result)) |
| 129 | + }, |
| 130 | + }; |
| 131 | + |
| 132 | + async function sleep(ms) { |
| 133 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 134 | + } |
| 135 | + ``` |
| 136 | + |
| 137 | +3. PlanetScale example with TLS: |
| 138 | + |
| 139 | + ```js |
| 140 | + import { createConnection } from 'mysql2'; |
| 141 | + |
| 142 | + export default { |
| 143 | + async fetch( |
| 144 | + request: Request, |
| 145 | + env: Env, |
| 146 | + ctx: ExecutionContext |
| 147 | + ): Promise<Response> { |
| 148 | + let result |
| 149 | + const connection = createConnection( |
| 150 | + { |
| 151 | + host: 'aws.connect.psdb.cloud', |
| 152 | + port: 3306, |
| 153 | + user: 'your_user', |
| 154 | + password: 'your_password', |
| 155 | + database: 'test', |
| 156 | + ssl: { |
| 157 | + minVersion: 'TLSv1.2', |
| 158 | + rejectUnauthorized: true |
| 159 | + }, |
| 160 | + useStaticParser:true |
| 161 | + }); |
| 162 | + connection.query( |
| 163 | + 'show databases', |
| 164 | + function(err, rows, fields) { |
| 165 | + if (err) { |
| 166 | + throw err |
| 167 | + } |
| 168 | + result = rows |
| 169 | + } |
| 170 | + ); |
| 171 | + await sleep(2000); |
| 172 | + return new Response(JSON.stringify(result)) |
| 173 | + }, |
| 174 | + }; |
| 175 | + |
| 176 | + async function sleep(ms) { |
| 177 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 178 | + } |
| 179 | + ``` |
| 180 | + |
| 181 | +## Step 4: Test locally |
| 182 | + |
| 183 | +1. Add `node_compat = true` in the `wrangler.toml` to make Cloudflare Workers compatible with node dependencies. |
| 184 | + |
| 185 | + ``` |
| 186 | + vim wrangler.toml |
| 187 | + ``` |
| 188 | + |
| 189 | +2. Upgrade wrangler |
| 190 | + |
| 191 | + ``` |
| 192 | + npm install [email protected] --save-dev |
| 193 | + ``` |
| 194 | + |
| 195 | +2. Run locally |
| 196 | + |
| 197 | + ``` |
| 198 | + wrangler dev |
| 199 | + ``` |
| 200 | + |
| 201 | +## Step 5: Deploy |
| 202 | + |
| 203 | +You can also deploy it to Cloudflare, run `wrangler deploy`. If you meet any issues, please refer to the [Cloudflare doc](https://developers.cloudflare.com/workers/get-started/guide/#4-deploy-your-project) |
| 204 | + |
| 205 | +## Develop Guide |
| 206 | + |
| 207 | +If you want to develop the corresponding feature. Here is a simaple example of testing locally. |
| 208 | + |
| 209 | +1. enter to node-mysql2 |
| 210 | + |
| 211 | + ``` |
| 212 | + cd node-mysql2 |
| 213 | + ``` |
| 214 | + |
| 215 | +2. vim wrangler.toml |
| 216 | + |
| 217 | + ``` |
| 218 | + name = "mysql2-cloudflare" |
| 219 | + main = "src/worker.js" |
| 220 | + compatibility_date = "2023-08-21" |
| 221 | + node_compat = true |
| 222 | + ``` |
| 223 | + |
| 224 | +3. add worker.js |
| 225 | + |
| 226 | + ``` |
| 227 | + mkdir src |
| 228 | + cd src |
| 229 | + vim workers.js |
| 230 | + ``` |
| 231 | + |
| 232 | +4. write your test code inside worker.js |
| 233 | + |
| 234 | + ``` |
| 235 | + const { createConnection } = require('../index'); |
| 236 | + export default { |
| 237 | + async fetch(request, env, ctx) { |
| 238 | + let result |
| 239 | + const connection = createConnection( |
| 240 | + { |
| 241 | + host: '127.0.0.1', |
| 242 | + port: 3306, |
| 243 | + user: 'test', |
| 244 | + password: 'password', |
| 245 | + useStaticParser:true |
| 246 | + }); |
| 247 | + connection.query( |
| 248 | + 'show databases', |
| 249 | + function(err, rows, fields) { |
| 250 | + if (err) { |
| 251 | + throw err |
| 252 | + } |
| 253 | + result = rows |
| 254 | + } |
| 255 | + ); |
| 256 | + await sleep(2000); |
| 257 | + return new Response(JSON.stringify(result)) |
| 258 | + } |
| 259 | + }; |
| 260 | +
|
| 261 | + async function sleep(ms) { |
| 262 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 263 | + } |
| 264 | + ``` |
| 265 | + |
| 266 | +5. Test locally |
| 267 | + |
| 268 | + ``` |
| 269 | + npx wrangler dev |
| 270 | + ``` |
0 commit comments