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: content/200-orm/050-overview/500-databases/800-sql-server/index.mdx
+66-4Lines changed: 66 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,8 @@ tocDepth: 4
6
6
toc_max_heading_level: 4
7
7
---
8
8
9
-
<TopBlock>
10
-
11
9
The Microsoft SQL Server data source connector connects Prisma ORM to a [Microsoft SQL Server](https://learn.microsoft.com/en-us/sql/sql-server/?view=sql-server-ver15) database server.
12
10
13
-
</TopBlock>
14
-
15
11
## Example
16
12
17
13
To connect to a Microsoft SQL Server database, you need to configure a [`datasource`](/orm/prisma-schema/overview/data-sources) block in your [Prisma schema](/orm/prisma-schema):
@@ -28,6 +24,72 @@ The fields passed to the `datasource` block are:
28
24
-`provider`: Specifies the `sqlserver` data source connector.
29
25
-`url`: Specifies the [connection URL](#connection-details) for the Microsoft SQL Server database. In this case, an [environment variable is used](/orm/prisma-schema/overview#accessing-environment-variables-from-the-schema) to provide the connection URL.
30
26
27
+
28
+
## Using the `node-mssql` driver
29
+
30
+
As of [`v5.4.0`](https://github.com/prisma/prisma/releases/tag/5.4.0), you can use Prisma ORM with database drivers from the JavaScript ecosystem (instead of using Prisma ORM's built-in drivers). You can do this by using a [driver adapter](/orm/overview/databases/database-drivers).
31
+
32
+
For SQLite, [`node-mssql`](https://github.com/tediousjs/node-mssql) is one of the most popular drivers in the JavaScript ecosystem.
33
+
34
+
This section explains how you can use it with Prisma ORM and the `@prisma/adapter-mssql` driver adapter.
35
+
36
+
### 1. Enable the `driverAdapters` Preview feature flag
37
+
38
+
Since driver adapters are currently in [Preview](/orm/more/releases#preview), you need to enable its feature flag on the `datasource` block in your Prisma schema:
39
+
40
+
```prisma file=schema.prisma
41
+
generator client {
42
+
provider = "prisma-client-js"
43
+
previewFeatures = ["driverAdapters"]
44
+
}
45
+
46
+
datasource db {
47
+
provider = "sqlserver"
48
+
url = env("DATABASE_URL")
49
+
}
50
+
```
51
+
52
+
Once you have added the feature flag to your schema, re-generate Prisma Client:
53
+
54
+
```terminal
55
+
npx prisma generate
56
+
```
57
+
58
+
### 2. Install the dependencies
59
+
60
+
Next, install Prisma ORM's driver adapter for `node-mssql`:
61
+
62
+
```terminal
63
+
npm install @prisma/adapter-mssql
64
+
```
65
+
66
+
### 3. Instantiate Prisma Client using the driver adapter
67
+
68
+
Finally, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the `PrismaClient` constructor:
encrypt: true, // Use this if you're on Windows Azure
82
+
trustServerCertificate: true, // Use this if you're using self-signed certificates
83
+
},
84
+
}
85
+
86
+
const adapter =newPrismaMssql(config)
87
+
const prisma =newPrismaClient({ adapter })
88
+
```
89
+
90
+
91
+
92
+
31
93
## Connection details
32
94
33
95
The connection URL used to connect to an Microsoft SQL Server database follows the [JDBC standard](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15).
Copy file name to clipboardExpand all lines: content/200-orm/050-overview/500-databases/850-planetscale.mdx
+46-12Lines changed: 46 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -6,14 +6,10 @@ tocDepth: 3
6
6
toc: true
7
7
---
8
8
9
-
<TopBlock>
10
-
11
9
Prisma and [PlanetScale](https://planetscale.com/) together provide a development arena that optimizes rapid, type-safe development of data access applications, using Prisma's ORM and PlanetScale's highly scalable MySQL-based platform.
12
10
13
11
This document discusses the concepts behind using Prisma ORM and PlanetScale, explains the commonalities and differences between PlanetScale and other database providers, and leads you through the process for configuring your application to integrate with PlanetScale.
14
12
15
-
</TopBlock>
16
-
17
13
## What is PlanetScale?
18
14
19
15
PlanetScale uses the [Vitess](https://vitess.io/) database clustering system to provide a MySQL-compatible database platform. Features include:
@@ -71,7 +67,7 @@ In Prisma ORM versions 3.1.1 and later, you can [emulate relations in Prisma Cli
71
67
72
68
To enable emulation of relations in Prisma Client, set the `relationMode` field to `"prisma"` in the `datasource` block:
73
69
74
-
```prisma file=schema.prisma showLineNumbers
70
+
```prisma file=schema.prisma
75
71
datasource db {
76
72
provider = "mysql"
77
73
url = env("DATABASE_URL")
@@ -91,7 +87,7 @@ If you use relations in your Prisma schema with the default `"foreignKeys"` opti
91
87
92
88
When [you emulate relations in Prisma Client](#option-1-emulate-relations-in-prisma-client), you need to create your own indexes. As an example of a situation where you would want to add an index, take this schema for a blog with posts and comments:
93
89
94
-
```prisma file=schema.prisma showLineNumbers
90
+
```prisma file=schema.prisma
95
91
model Post {
96
92
id Int @id @default(autoincrement())
97
93
title String
@@ -112,7 +108,7 @@ The `postId` field in the `Comment` model refers to the corresponding `id` field
112
108
113
109
To avoid this, you can define an index on the `postId` field using [Prisma ORM's `@@index` argument](/orm/reference/prisma-schema-reference#index):
@@ -213,13 +209,13 @@ Once you are happy with your changes on your development branch, you can open a
213
209
214
210
For more examples, see PlanetScale's tutorial on [automatic migrations with Prisma ORM](https://planetscale.com/docs/prisma/automatic-prisma-migrations) using `db push`.
215
211
216
-
## How to add in missing relations after Introspection
212
+
## How to add in missing relations after introspection
217
213
218
214
> **Note**: This section is only relevant if you use `relationMode = "prisma"` to emulate foreign key constraints with Prisma ORM. If you enabled foreign key constraints in your PlanetScale database, you can ignore this section.
219
215
220
216
After introspecting with `npx prisma db pull`, the schema you get may be missing some relations. For example, the following schema is missing a relation between the `User` and `Post` models:
221
217
222
-
```prisma file=schema.prisma showLineNumbers
218
+
```prisma file=schema.prisma
223
219
model Post {
224
220
id Int @id @default(autoincrement())
225
221
createdAt DateTime @default(now())
@@ -239,7 +235,7 @@ model User {
239
235
240
236
In this case you need to add the relation in manually:
For a more detailed example, see the [Getting Started guide for PlanetScale](/getting-started/setup-prisma/add-to-existing-project/relational-databases/introspection-typescript-planetscale).
265
261
262
+
## How to define shard keys in your Prisma schema (Preview)
263
+
264
+
[Sharding](https://planetscale.com/docs/vitess/sharding) is a popular technique to scale up when database load grows.
265
+
266
+
As of [v6.10.0](https://github.com/prisma/prisma/releases/tag/6.10.0), Prisma ORM supports sharding on PlanetScale natively (as a [Preview](/orm/more/releases#preview) feature) via the [`@shardKey`](/orm/reference/prisma-schema-reference#shardkey) and [`@@shardKey`](/orm/reference/prisma-schema-reference#shardkey-1) attributes in the Prisma schema which you can apply to the fields in your models that should serve as shard keys in your database setup.
267
+
268
+
In order to use the shard key attributes, you need to specify the `shardKeys` Preview feature on your `generator`:
269
+
270
+
```prisma
271
+
generator client {
272
+
provider = "prisma-client-js"
273
+
output = "../generated/prisma"
274
+
previewFeatures = ["shardKeys"]
275
+
}
276
+
```
277
+
278
+
Now you can use the `@shardKey` and `@@shardKey` attributes:
279
+
280
+
**Single-column shard key**
281
+
282
+
```prisma
283
+
model User {
284
+
id String @default(uuid())
285
+
region String @shardKey
286
+
}
287
+
```
288
+
289
+
**Multi-column shard key**
290
+
291
+
```prisma
292
+
model User {
293
+
id String @default(uuid())
294
+
country String
295
+
customerId String
296
+
@@shardKey([country, customerId])
297
+
}
298
+
```
299
+
266
300
## How to use the PlanetScale serverless driver with Prisma ORM (Preview)
267
301
268
302
The [PlanetScale serverless driver](https://planetscale.com/docs/tutorials/planetscale-serverless-driver) provides a way of communicating with your database and executing queries over HTTP.
Prisma Client is now ready to use in your project.
173
175
176
+
#### Importing generated model types
177
+
178
+
If you're importing types generated for your models, you can do so as follows:
179
+
180
+
```ts file=src/index.ts
181
+
import { User, Post } from"./generated/prisma/models.js"
182
+
```
183
+
184
+
#### Importing generated enum types
185
+
186
+
187
+
If you're importing types generated for your enums, you can do so as follows:
188
+
189
+
```ts file=src/index.ts
190
+
import { Role } from"./generated/prisma/enums.js"
191
+
```
192
+
174
193
### Field reference
175
194
176
195
Use the following options in the `generator client { ... }` block. Only `output` is required. The other fields have defaults or are inferred from your environment and `tsconfig.json`.
0 commit comments