Skip to content

Commit 66fefd7

Browse files
authored
cloud: support edge in prisma (#16966)
1 parent 2074aa8 commit 66fefd7

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

tidb-cloud/serverless-driver-prisma-example.md

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,59 @@ summary: Learn how to use TiDB Cloud serverless driver with Prisma ORM.
77

88
[Prisma](https://www.prisma.io/docs) is an open source next-generation ORM (Object-Relational Mapping) that helps developers interact with their database in an intuitive, efficient, and safe way. TiDB Cloud offers [@tidbcloud/prisma-adapter](https://github.com/tidbcloud/prisma-adapter), enabling you to use [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client) over HTTPS with [TiDB Cloud serverless driver](/tidb-cloud/serverless-driver.md). Compared with the traditional TCP way, [@tidbcloud/prisma-adapter](https://github.com/tidbcloud/prisma-adapter) brings the following benefits:
99

10-
- Better performance in serverless environments
11-
- Possibility of using Prisma client in the edge environments (see [#21394](https://github.com/prisma/prisma/issues/21394) for more information)
10+
- Better performance of Prisma Client in serverless environments
11+
- Ability to use Prisma Client in edge environments
1212

13-
This tutorial describes how to use TiDB Cloud serverless driver with the Prisma adapter.
13+
This tutorial describes how to use [@tidbcloud/prisma-adapter](https://github.com/tidbcloud/prisma-adapter) in serverless environments and edge environments.
14+
15+
## Install
16+
17+
You need to install both [@tidbcloud/prisma-adapter](https://github.com/tidbcloud/prisma-adapter) and [TiDB Cloud serverless driver](/tidb-cloud/serverless-driver.md). You can install them using [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) or your preferred package manager.
18+
19+
Taking npm as an example, you can run the following commands for installation:
20+
21+
```shell
22+
npm install @tidbcloud/prisma-adapter
23+
npm install @tidbcloud/serverless
24+
```
25+
26+
## Enable `driverAdapters`
27+
28+
To use the Prisma adapter, you need to enable the `driverAdapters` feature in the `schema.prisma` file. For example:
29+
30+
```prisma
31+
generator client {
32+
provider = "prisma-client-js"
33+
previewFeatures = ["driverAdapters"]
34+
}
35+
36+
datasource db {
37+
provider = "mysql"
38+
url = env("DATABASE_URL")
39+
}
40+
```
41+
42+
## Initialize Prisma Client
43+
44+
Before using Prisma Client, you need to initialize it with `@tidbcloud/prisma-adapter`. For example:
45+
46+
```js
47+
import { connect } from '@tidbcloud/serverless';
48+
import { PrismaTiDBCloud } from '@tidbcloud/prisma-adapter';
49+
import { PrismaClient } from '@prisma/client';
50+
51+
// Initialize Prisma Client
52+
const connection = connect({ url: ${DATABASE_URL} });
53+
const adapter = new PrismaTiDBCloud(connection);
54+
const prisma = new PrismaClient({ adapter });
55+
```
56+
57+
Then, queries from Prisma Client can be sent to the TiDB Cloud serverless driver for processing.
1458

1559
## Use the Prisma adapter in Node.js environments
1660

61+
This section provides an example of how to use `@tidbcloud/prisma-adapter` in Node.js environments.
62+
1763
### Before you begin
1864

1965
To complete this tutorial, you need the following:
@@ -40,7 +86,7 @@ To complete this tutorial, you need the following:
4086
npm install @tidbcloud/serverless
4187
npm install prisma --save-dev
4288
```
43-
89+
4490
3. In the `package.json` file, specify the ES module by adding `type: "module"`:
4591
4692
```json
@@ -125,16 +171,16 @@ To complete this tutorial, you need the following:
125171
```
126172
npx prisma db push
127173
```
128-
129-
This command will create the `user` table in your TiDB Serverless cluster through the traditional TCP connection, rather than through the HTTPS connection using `@tidbcloud/prisma-adapter`. This is because it uses the same engine as Prisma Migrate. For more information about this command, see [Prototype your schema](https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push).
174+
175+
This command will create the `user` table in your TiDB Serverless cluster through the traditional TCP connection, rather than through the HTTPS connection using `@tidbcloud/prisma-adapter`. This is because it uses the same engine as Prisma Migrate. For more information about this command, see [Prototype your schema](https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push).
130176
131177
4. Generate Prisma Client:
132178
133179
```
134180
npx prisma generate
135181
```
136182
137-
This command will generate Prisma Client based on the Prisma schema.
183+
This command will generate Prisma Client based on the Prisma schema.
138184
139185
### Step 4. Execute CRUD operations
140186
@@ -178,7 +224,7 @@ To complete this tutorial, you need the following:
178224
},
179225
})
180226
```
181-
227+
182228
3. Execute some transaction operations with Prisma Client. For example:
183229

184230
```js
@@ -213,7 +259,10 @@ To complete this tutorial, you need the following:
213259
console.log(e)
214260
}
215261
```
216-
262+
217263
## Use the Prisma adapter in edge environments
218264

219-
Currently, `@tidbcloud/prisma-adapter` is not compatible with edge environments such as Vercel Edge Function and Cloudflare Workers. However, there are plans to support these environments. For more information, see [#21394](https://github.com/prisma/prisma/issues/21394).
265+
You can use `@tidbcloud/prisma-adapter` v5.11.0 or a later version in edge environments such as Vercel Edge Functions and Cloudflare Workers.
266+
267+
- [Vercel Edge Function example](https://github.com/tidbcloud/serverless-driver-example/tree/main/prisma/prisma-vercel-example)
268+
- [Cloudflare Workers example](https://github.com/tidbcloud/serverless-driver-example/tree/main/prisma/prisma-cloudflare-worker-example)

0 commit comments

Comments
 (0)