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
Copy file name to clipboardExpand all lines: tidb-cloud/serverless-driver-prisma-example.md
+59-10Lines changed: 59 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,59 @@ summary: Learn how to use TiDB Cloud serverless driver with Prisma ORM.
7
7
8
8
[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:
9
9
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
12
12
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:
Then, queries from Prisma Client can be sent to the TiDB Cloud serverless driver for processing.
14
58
15
59
## Use the Prisma adapter in Node.js environments
16
60
61
+
This section provides an example of how to use `@tidbcloud/prisma-adapter` in Node.js environments.
62
+
17
63
### Before you begin
18
64
19
65
To complete this tutorial, you need the following:
@@ -40,7 +86,7 @@ To complete this tutorial, you need the following:
40
86
npm install @tidbcloud/serverless
41
87
npm install prisma --save-dev
42
88
```
43
-
89
+
44
90
3. In the `package.json` file, specify the ES module by adding `type: "module"`:
45
91
46
92
```json
@@ -125,16 +171,16 @@ To complete this tutorial, you need the following:
125
171
```
126
172
npx prisma db push
127
173
```
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).
130
176
131
177
4. Generate Prisma Client:
132
178
133
179
```
134
180
npx prisma generate
135
181
```
136
182
137
-
This command will generate Prisma Client based on the Prisma schema.
183
+
This command will generate Prisma Client based on the Prisma schema.
138
184
139
185
### Step 4. Execute CRUD operations
140
186
@@ -178,7 +224,7 @@ To complete this tutorial, you need the following:
178
224
},
179
225
})
180
226
```
181
-
227
+
182
228
3. Execute some transaction operations with Prisma Client. For example:
183
229
184
230
```js
@@ -213,7 +259,10 @@ To complete this tutorial, you need the following:
213
259
console.log(e)
214
260
}
215
261
```
216
-
262
+
217
263
## Use the Prisma adapter in edge environments
218
264
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)
0 commit comments