Skip to content

Commit 777f931

Browse files
Merge branch 'main' into patch-1
2 parents 9a44ec6 + 06c8443 commit 777f931

File tree

26 files changed

+601
-240
lines changed

26 files changed

+601
-240
lines changed

β€ŽcSpell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"PGSSLMODE",
127127
"pgloader",
128128
"unikernel",
129-
"Mikro"
129+
"Mikro",
130+
"databaseto"
130131
],
131132
"patterns": [
132133
{

β€Žcontent/200-orm/050-overview/500-databases/200-database-drivers.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ You can connect to your database using a Node.js-based driver from Prisma Client
5454
- SQLite
5555
- [`better-sqlite3`](/orm/overview/databases/sqlite#using-the-better-sqlite3-driver)
5656
- [`libSQL`](/orm/overview/databases/turso#how-to-connect-and-query-a-turso-database) (Turso)
57+
- MSSQL
58+
- [`node-mssql`](/orm/overview/databases/sql-server#using-the-node-mssql-driver)
5759

5860
### Serverless driver adapters
5961

β€Žcontent/200-orm/050-overview/500-databases/800-sql-server/index.mdx

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ tocDepth: 4
66
toc_max_heading_level: 4
77
---
88

9-
<TopBlock>
10-
119
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.
1210

13-
</TopBlock>
14-
1511
## Example
1612

1713
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:
2824
- `provider`: Specifies the `sqlserver` data source connector.
2925
- `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.
3026

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:
69+
70+
```ts
71+
import { PrismaMssql } from '@prisma/adapter-mssql'
72+
import { PrismaClient } from '@prisma/client'
73+
74+
const config = {
75+
server: 'localhost',
76+
port: 1433,
77+
database: 'mydb',
78+
user: 'sa',
79+
password: 'mypassword',
80+
options: {
81+
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 = new PrismaMssql(config)
87+
const prisma = new PrismaClient({ adapter })
88+
```
89+
90+
91+
92+
3193
## Connection details
3294

3395
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).

β€Žcontent/200-orm/050-overview/500-databases/850-planetscale.mdx

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ tocDepth: 3
66
toc: true
77
---
88

9-
<TopBlock>
10-
119
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.
1210

1311
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.
1412

15-
</TopBlock>
16-
1713
## What is PlanetScale?
1814

1915
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
7167

7268
To enable emulation of relations in Prisma Client, set the `relationMode` field to `"prisma"` in the `datasource` block:
7369

74-
```prisma file=schema.prisma showLineNumbers
70+
```prisma file=schema.prisma
7571
datasource db {
7672
provider = "mysql"
7773
url = env("DATABASE_URL")
@@ -91,7 +87,7 @@ If you use relations in your Prisma schema with the default `"foreignKeys"` opti
9187

9288
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:
9389

94-
```prisma file=schema.prisma showLineNumbers
90+
```prisma file=schema.prisma
9591
model Post {
9692
id Int @id @default(autoincrement())
9793
title String
@@ -112,7 +108,7 @@ The `postId` field in the `Comment` model refers to the corresponding `id` field
112108

113109
To avoid this, you can define an index on the `postId` field using [Prisma ORM's `@@index` argument](/orm/reference/prisma-schema-reference#index):
114110

115-
```prisma file=schema.prisma highlight=15;add showLineNumbers
111+
```prisma file=schema.prisma highlight=15;add
116112
model Post {
117113
id Int @id @default(autoincrement())
118114
title String
@@ -150,7 +146,7 @@ You can then use Prisma ORM and define relations in your Prisma schema without t
150146

151147
In that case, you can define a relation as with other database that supports foreign key constraints, for example:
152148

153-
```prisma file=schema.prisma showLineNumbers
149+
```prisma file=schema.prisma
154150
model Post {
155151
id Int @id @default(autoincrement())
156152
title String
@@ -182,7 +178,7 @@ As an example, let's say you decide to decide to add a new `excerpt` field to th
182178

183179
Next, add the following to your `schema.prisma` file:
184180

185-
```prisma file=schema.prisma highlight=5;edit showLineNumbers
181+
```prisma file=schema.prisma highlight=5;edit
186182
model Post {
187183
id Int @id @default(autoincrement())
188184
title String
@@ -213,13 +209,13 @@ Once you are happy with your changes on your development branch, you can open a
213209

214210
For more examples, see PlanetScale's tutorial on [automatic migrations with Prisma ORM](https://planetscale.com/docs/prisma/automatic-prisma-migrations) using `db push`.
215211

216-
## How to add in missing relations after Introspection
212+
## How to add in missing relations after introspection
217213

218214
> **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.
219215
220216
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:
221217

222-
```prisma file=schema.prisma showLineNumbers
218+
```prisma file=schema.prisma
223219
model Post {
224220
id Int @id @default(autoincrement())
225221
createdAt DateTime @default(now())
@@ -239,7 +235,7 @@ model User {
239235

240236
In this case you need to add the relation in manually:
241237

242-
```prisma file=schema.prisma highlight=6,16;add showLineNumbers
238+
```prisma file=schema.prisma highlight=6,16;add
243239
model Post {
244240
id Int @id @default(autoincrement())
245241
createdAt DateTime @default(now())
@@ -263,6 +259,44 @@ model User {
263259

264260
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).
265261

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+
266300
## How to use the PlanetScale serverless driver with Prisma ORM (Preview)
267301

268302
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.

β€Žcontent/200-orm/100-prisma-schema/10-overview/03-generators.mdx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,35 @@ In the future, you can safely include the generated directory in version control
161161

162162
#### 4. Use Prisma Client in your application
163163

164-
After generating the Prisma Client, import the types from the path you specified:
164+
#### Importing Prisma Client
165+
166+
After generating the Prisma Client, import it from the path you specified:
165167

166168
```ts file=src/index.ts
167-
import { PrismaClient } from "./generated/prisma/client"
169+
import { PrismaClient } from "./generated/prisma/client.js"
168170

169171
const prisma = new PrismaClient()
170172
```
171173

172174
Prisma Client is now ready to use in your project.
173175

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+
174193
### Field reference
175194

176195
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`.
@@ -223,7 +242,6 @@ generated/
223242
β”œβ”€β”€ client.ts
224243
β”œβ”€β”€ commonInputTypes.ts
225244
β”œβ”€β”€ enums.ts
226-
β”œβ”€β”€ index.ts
227245
β”œβ”€β”€ internal
228246
β”‚ β”œβ”€β”€ class.ts
229247
β”‚ └── prismaNamespace.ts

0 commit comments

Comments
Β (0)