Integrate TiDB Cloud with Cloudflare Workers
Cloudflare Workers is a platform that allows you to run code in response to specific events, such as HTTP requests or changes to a database. Cloudflare Workers is easy to use and can be used to build a variety of applications, including custom APIs, serverless functions, and microservices. It is particularly useful for applications that require low-latency performance or need to scale quickly.
You may find it hard to connect to TiDB Cloud from Cloudflare Workers because Cloudflare Workers runs on the V8 engine which cannot make direct TCP connections. You can use TiDB Cloud serverless driver to help you connect to Cloudflare Workers over HTTP connection.
This document shows how to connect to Cloudflare Workers with TiDB Cloud serverless driver step by step.
Before you begin
Before you try the steps in this article, you need to prepare the following things:
- A TiDB Cloud account and a TiDB Cloud Serverless cluster on TiDB Cloud. For more details, see TiDB Cloud Quick Start.
- A Cloudflare Workers account.
- npm is installed.
Step 1: Set up Wrangler
Wrangler is the official Cloudflare Worker CLI. You can use it to generate, build, preview, and publish your Workers.
Install Wrangler:
npm install wranglerTo authenticate Wrangler, run wrangler login:
wrangler loginUse Wrangler to create a worker project:
wrangler init tidb-cloud-cloudflareIn your terminal, you will be asked a series of questions related to your project. Choose the default values for all questions.
Step 2: Install the serverless driver
Enter your project directory:
cd tidb-cloud-cloudflareInstall the serverless driver with npm:
npm install @tidbcloud/serverlessThis adds the serverless driver dependency in
package.json
.
Step 3: Develop the Cloudflare Worker function
You need to modify the src/index.ts
according to your needs.
For example, if you want to show all the databases, you can use the following code:
import { connect } from '@tidbcloud/serverless'
export interface Env {
DATABASE_URL: string;
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const conn = connect({url:env.DATABASE_URL})
const resp = await conn.execute("show databases")
return new Response(JSON.stringify(resp));
},
};
Step 4: Set the DATABASE_URL in your environment
The DATABASE_URL
follows the mysql://username:password@host/database
format. You can set the environment variable with wrangler cli:
wrangler secret put <DATABASE_URL>
You can also edit the DATABASE_URL
secret via the Cloudflare Workers dashboard.
Step 5: Publish to Cloudflare Workers
You're now ready to deploy to Cloudflare Workers.
In your project directory, run the following command:
npx wrangler publish
Step 6: Try your Cloudflare Workers
Go to Cloudflare dashboard to find your worker. You can find the URL of your worker on the overview page.
Visit the URL and you will get the result.
Examples
See the Cloudflare Workers example.