You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
summary: Learn how deploy Cloudflare Workers with TiDB Cloud.
3
+
summary: Learn how to deploy Cloudflare Workers with TiDB Cloud.
4
4
---
5
5
6
6
# Integrate TiDB Cloud with Cloudflare Workers
7
7
8
8
[Cloudflare Workers](https://workers.cloudflare.com/) 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.
9
9
10
-
However, 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.
10
+
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](/tidb-cloud/serverless-driver.md) to help you connect to Cloudflare Workers over HTTP connection.
11
11
12
-
Fortunately, Prisma has your back with the [Data Proxy](https://www.prisma.io/docs/data-platform/data-proxy). It can help you use Cloudflare Workers to process and manipulate the data being transmitted over a TCP connection.
13
-
14
-
This document shows how to deploy Cloudflare Workers with TiDB Cloud and Prisma Data Proxy step by step.
12
+
This document shows how to connect to Cloudflare Workers with TiDB Cloud serverless driver step by step.
15
13
16
14
> **Note:**
17
15
>
18
-
> If you want to connect a locally deployed TiDB to Cloudflare Workers, you can try [worker-tidb](https://github.com/shiyuhang0/worker-tidb), which uses Cloudflare tunnels as a proxy. However, worker-tidb is not recommended for production use.
16
+
> TiDB Cloud serverless driver can only be used in TiDB Serverless.
19
17
20
18
## Before you begin
21
19
22
20
Before you try the steps in this article, you need to prepare the following things:
23
21
24
22
- A TiDB Cloud account and a TiDB Serverless cluster on TiDB Cloud. For more details, see [TiDB Cloud Quick Start](/tidb-cloud/tidb-cloud-quickstart.md#step-1-create-a-tidb-cluster).
25
23
- A [Cloudflare Workers account](https://dash.cloudflare.com/login).
26
-
- A [Prisma Data Platform account](https://cloud.prisma.io/).
27
-
- A [GitHub account](https://github.com/login).
28
-
- Install Node.js and npm.
29
-
- Install dependencies using `npm install -D prisma typescript wrangler`
24
+
-[npm](https://docs.npmjs.com/about-npm) is installed.
30
25
31
26
## Step 1: Set up Wrangler
32
27
33
28
[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.
34
29
35
-
1. To authenticate Wrangler, run wrangler login:
30
+
1. Install Wrangler:
31
+
32
+
```
33
+
npm install wrangler
34
+
```
35
+
36
+
2. To authenticate Wrangler, run wrangler login:
36
37
37
38
```
38
39
wrangler login
39
40
```
40
41
41
-
2. Use Wrangler to create a worker project:
42
+
3. Use Wrangler to create a worker project:
42
43
43
44
```
44
-
wrangler init prisma-tidb-cloudflare
45
+
wrangler init tidb-cloud-cloudflare
45
46
```
46
47
47
-
3. In your terminal, you will be asked a series of questions related to your project. Choose the default values for all questions.
48
+
4. In your terminal, you will be asked a series of questions related to your project. Choose the default values for all questions.
48
49
49
-
## Step 2: Set up Prisma
50
+
## Step 2: Install the serverless driver
50
51
51
52
1. Enter your project directory:
52
53
53
54
```
54
-
cd prisma-tidb-cloudflare
55
-
```
56
-
57
-
2. Use the `prisma init` command to set up Prisma:
58
-
59
-
```
60
-
npx prisma init
55
+
cd tidb-cloud-cloudflare
61
56
```
62
57
63
-
This creates a Prisma schema in `prisma/schema.prisma`.
64
-
65
-
3. Inside `prisma/schema.prisma`, add the schema according to your tables in TiDB. Assume that you have `table1` and `table2` in TiDB, you can add the following schema:
58
+
2. Install the serverless driver with npm:
66
59
67
60
```
68
-
generator client {
69
-
provider = "prisma-client-js"
70
-
}
71
-
72
-
datasource db {
73
-
provider = "mysql"
74
-
url = env("DATABASE_URL")
75
-
}
76
-
77
-
model table1 {
78
-
id Int @id @default(autoincrement())
79
-
name String
80
-
}
81
-
82
-
model table2 {
83
-
id Int @id @default(autoincrement())
84
-
name String
85
-
}
61
+
npm install @tidbcloud/serverless
86
62
```
87
63
88
-
This data model will be used to store incoming requests from your Worker.
89
-
90
-
## Step 3: Push your project to GitHub
64
+
This adds the serverless driver dependency in `package.json`.
91
65
92
-
1. [Create a repository](https://github.com/new) named `prisma-tidb-cloudflare` on GitHub.
66
+
## Step 3: Develop the Cloudflare Worker function
93
67
94
-
2. After you create the repository, you can push your project to GitHub:
You need to modify the `src/index.ts` according to your needs.
102
69
103
-
## Step 4: Import your Project into the Prisma Data Platform
104
-
105
-
With Cloudflare Workers, you cannot directly access your database because there is no TCP support. Instead, you can use Prisma Data Proxy as described above.
106
-
107
-
1. To get started, sign in to the [Prisma Data Platform](https://cloud.prisma.io/) and click **New Project**.
108
-
2. Fill in the **Connection string** with this pattern `mysql://USER:PASSWORD@HOST:PORT/DATABASE?sslaccept=strict`. You can find the connection information in your [TiDB Cloud console](https://tidbcloud.com/console/clusters).
109
-
3. Leave the **Static IPs** as disabled because TiDB Serverless is accessible from any IP address.
110
-
4. Select a Data Proxy region that is geographically close to your TiDB Cloud cluster location. Then click **Create project**.
5. Fill in the repository, and click **Link Prisma schema** in the **Get Started** page.
115
-
6. Click **Create a new connection string** and you will get a new connection string that starts with `prisma://.` Copy this connection string and save it for later.
116
-
117
-

118
-
119
-
7. Click **Skip and continue to Data Platform** to go to the Data Platform.
120
-
121
-
## Step 5: Set the Data Proxy Connection string in your environment
122
-
123
-
1. Add the Data Proxy connection string to your local environment `.env` file:
0 commit comments