Skip to content

Commit a72eb21

Browse files
Update guides to use prisma.config.ts (#7243)
* (feat) Update guides to use prisma.config.ts * refactor: Updated guides for Prisma 7 changes --------- Co-authored-by: Aman Varshney <[email protected]>
1 parent 8d21ba4 commit a72eb21

33 files changed

+973
-211
lines changed

content/800-guides/010-data-migration.mdx

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Start with a basic schema containing a Post model:
4545

4646
```prisma
4747
generator client {
48-
provider = "prisma-client-js"
48+
provider = "prisma-client"
49+
output = "./generated/prisma"
4950
}
5051
5152
datasource db {
5253
provider = "postgresql"
53-
url = env("DATABASE_URL")
5454
}
5555
5656
model Post {
@@ -61,7 +61,36 @@ model Post {
6161
}
6262
```
6363

64-
### 1.2. Create a development branch
64+
### 1.2. Configure Prisma
65+
66+
Create a `prisma.config.ts` file in the root of your project with the following content:
67+
68+
```typescript file=prisma.config.ts
69+
import 'dotenv/config'
70+
import { defineConfig, env } from 'prisma/config';
71+
72+
export default defineConfig({
73+
schema: 'prisma/schema.prisma',
74+
migrations: {
75+
path: 'prisma/migrations',
76+
},
77+
datasource: {
78+
url: env('DATABASE_URL'),
79+
},
80+
});
81+
```
82+
83+
:::note
84+
85+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
86+
87+
```bash
88+
npm install dotenv
89+
```
90+
91+
:::
92+
93+
### 1.3. Create a development branch
6594

6695
Create a new branch for your changes:
6796

@@ -108,9 +137,17 @@ npx prisma migrate dev --name add-status-column
108137
Create a new TypeScript file for the data migration:
109138

110139
```typescript
111-
import { PrismaClient } from '@prisma/client'
140+
import { PrismaClient } from '../generated/prisma/client'
141+
import { PrismaPg } from '@prisma/adapter-pg'
142+
import 'dotenv/config'
143+
144+
const adapter = new PrismaPg({
145+
connectionString: process.env.DATABASE_URL,
146+
})
112147

113-
const prisma = new PrismaClient()
148+
const prisma = new PrismaClient({
149+
adapter,
150+
})
114151

115152
async function main() {
116153
await prisma.$transaction(async (tx) => {

content/800-guides/020-implementing-schema-changes.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ Source-controlling the `schema.prisma` file is not enough - you must include you
4747
- Customized migrations contain information that cannot be represented in the Prisma schema
4848
- The `prisma migrate deploy` command only runs migration files
4949

50+
### 1.3. Configure Prisma
51+
52+
Create a `prisma.config.ts` file in the root of your project with the following content:
53+
54+
```typescript file=prisma.config.ts
55+
import 'dotenv/config'
56+
import { defineConfig, env } from 'prisma/config';
57+
58+
export default defineConfig({
59+
schema: 'prisma/schema.prisma',
60+
migrations: {
61+
path: 'prisma/migrations',
62+
},
63+
datasource: {
64+
url: env('DATABASE_URL'),
65+
},
66+
});
67+
```
68+
69+
:::note
70+
71+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
72+
73+
```bash
74+
npm install dotenv
75+
```
76+
77+
:::
78+
5079
## 2. Incorporate team changes
5180

5281
### 2.1. Pull latest changes

content/800-guides/030-migrate-from-typeorm.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ Update the `DATABASE_URL` in the `.env` file with your database connection strin
5151
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
5252
```
5353

54+
### 2.3. Configure Prisma
55+
56+
Create a `prisma.config.ts` file in the root of your project with the following content:
57+
58+
```typescript file=prisma.config.ts
59+
import 'dotenv/config'
60+
import { defineConfig, env } from 'prisma/config';
61+
62+
export default defineConfig({
63+
schema: 'prisma/schema.prisma',
64+
migrations: {
65+
path: 'prisma/migrations',
66+
},
67+
datasource: {
68+
url: env('DATABASE_URL'),
69+
},
70+
});
71+
```
72+
73+
:::note
74+
75+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
76+
77+
```bash
78+
npm install dotenv
79+
```
80+
81+
:::
82+
5483
## 3. Migrate the database schema
5584

5685
### 3.1. Introspect your database

content/800-guides/040-migrate-from-sequelize.mdx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ The Prisma schema currently looks as follows:
5858
5959
datasource db {
6060
provider = "postgresql"
61-
url = env("DATABASE_URL")
6261
}
6362
6463
generator client {
65-
provider = "prisma-client-js"
64+
provider = "prisma-client"
65+
output = "./generated/prisma"
6666
}
6767
```
6868

@@ -78,6 +78,35 @@ Update the `DATABASE_URL` in the `.env` file with your database connection strin
7878
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
7979
```
8080

81+
### 1.3. Configure Prisma
82+
83+
Create a `prisma.config.ts` file in the root of your project with the following content:
84+
85+
```typescript file=prisma.config.ts
86+
import 'dotenv/config'
87+
import { defineConfig, env } from 'prisma/config';
88+
89+
export default defineConfig({
90+
schema: 'prisma/schema.prisma',
91+
migrations: {
92+
path: 'prisma/migrations',
93+
},
94+
datasource: {
95+
url: env('DATABASE_URL'),
96+
},
97+
});
98+
```
99+
100+
:::note
101+
102+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
103+
104+
```bash
105+
npm install dotenv
106+
```
107+
108+
:::
109+
81110
## 2. Migrate the database schema
82111

83112
### 2.1. Introspect your database

content/800-guides/050-migrate-from-mongoose.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ Update the `DATABASE_URL` in the `.env` file with your MongoDB connection string
7878
DATABASE_URL="mongodb://USER:PASSWORD@HOST:PORT/DATABASE"
7979
```
8080

81+
### 1.3. Configure Prisma
82+
83+
Create a `prisma.config.ts` file in the root of your project with the following content:
84+
85+
```typescript file=prisma.config.ts
86+
import 'dotenv/config'
87+
import { defineConfig, env } from 'prisma/config';
88+
89+
export default defineConfig({
90+
schema: 'prisma/schema.prisma',
91+
migrations: {
92+
path: 'prisma/migrations',
93+
},
94+
datasource: {
95+
url: env('DATABASE_URL'),
96+
},
97+
});
98+
```
99+
100+
:::note
101+
102+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
103+
104+
```bash
105+
npm install dotenv
106+
```
107+
108+
:::
109+
81110
## 2. Migrate the database schema
82111

83112
### 2.1. Introspect your database

content/800-guides/060-migrate-from-drizzle.mdx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ The Prisma schema currently looks as follows:
8282
8383
datasource db {
8484
provider = "postgresql"
85-
url = env("DATABASE_URL")
8685
}
8786
8887
generator client {
89-
provider = "prisma-client-js"
88+
provider = "prisma-client"
89+
output = "./generated/prisma"
9090
}
9191
```
9292

@@ -107,7 +107,6 @@ If you're not using PostgreSQL, you need to adjust the `provider` field on the `
107107
```prisma file=schema.prisma showLineNumbers
108108
datasource db {
109109
provider = "postgresql"
110-
url = env("DATABASE_URL")
111110
}
112111
```
113112

@@ -118,7 +117,6 @@ datasource db {
118117
```prisma file=schema.prisma showLineNumbers
119118
datasource db {
120119
provider = "mysql"
121-
url = env("DATABASE_URL")
122120
}
123121
```
124122

@@ -129,7 +127,6 @@ datasource db {
129127
```prisma file=schema.prisma showLineNumbers
130128
datasource db {
131129
provider = "sqlserver"
132-
url = env("DATABASE_URL")
133130
}
134131
```
135132

@@ -140,7 +137,6 @@ datasource db {
140137
```prisma file=schema.prisma showLineNumbers
141138
datasource db {
142139
provider = "sqlite"
143-
url = env("DATABASE_URL")
144140
}
145141
```
146142

@@ -150,7 +146,36 @@ datasource db {
150146

151147
Once that's done, you can configure your [database connection URL](/orm/reference/connection-urls) in the `.env` file. Drizzle and Prisma ORM use the same format for connection URLs, so your existing connection URL should work fine.
152148

153-
### 2.3. Introspect your database using Prisma ORM
149+
### 2.3. Configure Prisma
150+
151+
Create a `prisma.config.ts` file in the root of your project with the following content:
152+
153+
```typescript file=prisma.config.ts
154+
import 'dotenv/config'
155+
import { defineConfig, env } from 'prisma/config';
156+
157+
export default defineConfig({
158+
schema: 'prisma/schema.prisma',
159+
migrations: {
160+
path: 'prisma/migrations',
161+
},
162+
datasource: {
163+
url: env('DATABASE_URL'),
164+
},
165+
});
166+
```
167+
168+
:::note
169+
170+
You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:
171+
172+
```bash
173+
npm install dotenv
174+
```
175+
176+
:::
177+
178+
### 2.4. Introspect your database using Prisma ORM
154179

155180
With your connection URL in place, you can [introspect](/orm/prisma-schema/introspection) your database to generate your Prisma models:
156181

@@ -170,7 +195,7 @@ model todo {
170195

171196
The generated Prisma model represents a database table. Prisma models are the foundation for your programmatic Prisma Client API which allows you to send queries to your database.
172197

173-
### 2.4. Create a baseline migration
198+
### 2.5. Create a baseline migration
174199

175200
To continue using Prisma Migrate to evolve your database schema, you will need to [baseline your database](/orm/prisma-migrate/getting-started).
176201

@@ -202,7 +227,7 @@ The command will mark `0_init` as applied by adding it to the `_prisma_migration
202227

203228
You now have a baseline for your current database schema. To make further changes to your database schema, you can update your Prisma schema and use `prisma migrate dev` to apply the changes to your database.
204229

205-
### 2.5. Adjust the Prisma schema (optional)
230+
### 2.6. Adjust the Prisma schema (optional)
206231

207232
Models that are generated via introspection currently _exactly_ map to your database tables. In this section, you'll learn how you can adjust the naming of the Prisma models to adhere to [Prisma ORM's naming conventions](/orm/reference/prisma-schema-reference#naming-conventions).
208233

@@ -254,9 +279,17 @@ touch db/prisma.ts
254279
Now, instantiate `PrismaClient` and export it from the file so you can use it in your route handlers later:
255280

256281
```ts copy file=db/prisma.ts showLineNumbers
257-
import { PrismaClient } from '@prisma/client'
282+
import { PrismaClient } from '../generated/prisma/client'
283+
import { PrismaPg } from '@prisma/adapter-pg'
284+
import 'dotenv/config'
285+
286+
const adapter = new PrismaPg({
287+
connectionString: process.env.DATABASE_URL,
288+
})
258289

259-
export const prisma = new PrismaClient()
290+
export const prisma = new PrismaClient({
291+
adapter,
292+
})
260293
```
261294

262295
### 4.1. Replacing `getData` queries

content/800-guides/070-cloudflare-d1.mdx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ In your Prisma schema, set the `provider` of the `datasource` to `sqlite`. If yo
5353

5454
```prisma file=prisma/schema.prisma
5555
generator client {
56-
provider = "prisma-client-js"
56+
provider = "prisma-client"
5757
output = "../src/generated/prisma"
5858
}
5959
6060
datasource db {
6161
provider = "sqlite"
62-
url = env("DATABASE_URL")
6362
}
6463
6564
//add-start
@@ -179,7 +178,7 @@ CLOUDFLARE_D1_TOKEN="F8Cg..."
179178

180179
Ensure that you have a `prisma.config.ts` file set up in the root of your project with a [driver adapter](/orm/reference/prisma-config-reference#adapter-removed) defined.
181180

182-
```ts
181+
```typescript file=prisma.config.ts
183182
import type { PrismaConfig } from 'prisma';
184183
import { PrismaD1 } from '@prisma/adapter-d1';
185184

@@ -191,6 +190,12 @@ export default {
191190
adapter: true,
192191
},
193192
schema: 'prisma/schema.prisma',
193+
migrations: {
194+
path: 'prisma/migrations',
195+
},
196+
datasource: {
197+
url: process.env.DATABASE_URL!,
198+
},
194199
async adapter() {
195200
return new PrismaD1({
196201
CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN!,

0 commit comments

Comments
 (0)