Skip to content

Commit 7a86cf0

Browse files
docs(): more v7 updates (#7333)
* docs(): more v7 updates * Update content/200-orm/500-reference/380-connection-urls.mdx Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent d496d1d commit 7a86cf0

File tree

3 files changed

+177
-71
lines changed

3 files changed

+177
-71
lines changed

content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,29 @@ This guide describes how to seed your database using Prisma Client and Prisma OR
1616

1717
## How to seed your database in Prisma ORM
1818

19-
Prisma ORM's integrated seeding functionality expects a command in the `"seed"` key in the `"prisma"` key of your `package.json` file. This can be any command, `prisma db seed` will just execute it. In this guide and as a default, we recommend writing a seed script inside your project's `prisma/` folder and starting it with the command.
20-
21-
<TabbedContent>
22-
<TabItem value="TypeScript">
23-
```json
24-
"prisma": {
25-
"seed": "ts-node prisma/seed.ts"
26-
},
19+
Prisma ORM's integrated seeding functionality expects a command in the `"seed"` key in the `migrations` object of your `prisma.config.ts`. This can be any command, `prisma db seed` will just execute it. In this guide and as a default, we recommend writing a seed script inside your project's `prisma/` folder and starting it with the command.
20+
21+
```ts
22+
import 'dotenv/config'
23+
import { defineConfig, env } from "prisma/config";
24+
export default defineConfig({
25+
schema: "prisma/schema.prisma",
26+
migrations: {
27+
path: "prisma/migrations",
28+
seed: "tsx prisma/seed.ts"
29+
},
30+
datasource: {
31+
url: env("DATABASE_URL"),
32+
},
33+
});
2734
```
28-
<br/>
29-
30-
:::info
31-
32-
With TypeScript,`ts-node` does transpiling and typechecking by default; typechecking can be disabled with the following flag `--transpile-only`.
33-
34-
Example:
35-
`"seed": "ts-node --transpile-only prisma/seed.ts"`
36-
37-
This can be useful to reduce memory usage (RAM) and increase execution speed of the seed script.
38-
39-
:::
40-
41-
</TabItem>
42-
43-
<TabItem value="JavaScript">
44-
```json
45-
"prisma": {
46-
"seed": "node prisma/seed.js"
47-
},
48-
```
49-
</TabItem>
50-
51-
</TabbedContent>
5235

5336
## Integrated seeding with Prisma Migrate
5437

55-
Database seeding happens in two ways with Prisma ORM: manually with `prisma db seed` and automatically in `prisma migrate reset` and (in some scenarios) `prisma migrate dev`.
38+
Database seeding happens when you run `prisma db seed`. With `prisma db seed`, _you_ decide when to invoke the seed command. It can be useful for a test setup or to prepare a new development environment, for example.
5639

57-
With `prisma db seed`, _you_ decide when to invoke the seed command. It can be useful for a test setup or to prepare a new development environment, for example.
5840

41+
### Prisma 6 Only
5942
Prisma Migrate also integrates seamlessly with your seeds, assuming you follow the steps in the section below. Seeding is triggered automatically when Prisma Migrate resets the development database.
6043

6144
Prisma Migrate resets the database and triggers seeding in the following scenarios:
@@ -153,37 +136,28 @@ Here we suggest some specific seed scripts for different situations. You are fre
153136
})
154137
```
155138

156-
3. Add `typescript`, `ts-node` and `@types/node` development dependencies:
139+
3. Add `typescript`, `tsx` and `@types/node` development dependencies:
157140
```
158-
npm install -D typescript ts-node @types/node
141+
npm install -D typescript tsx @types/node
159142
```
160143

161-
<a id="compiler-options"></a>
162-
163-
4. Add the `prisma.seed` field to your `package.json` file:
164-
```json file=package.json highlight=5;normal
165-
{
166-
"name": "my-project",
167-
"version": "1.0.0",
168-
"prisma": {
169-
"seed": "ts-node prisma/seed.ts"
170-
},
171-
"devDependencies": {
172-
"@types/node": "^14.14.21",
173-
"ts-node": "^9.1.1",
174-
"typescript": "^4.1.3"
175-
}
176-
}
144+
4. Add the `seed` field to your `prisma.config.ts` file:
145+
```ts file=prisma.config.ts
146+
import 'dotenv/config'
147+
import { defineConfig, env } from "prisma/config";
148+
export default defineConfig({
149+
schema: "prisma/schema.prisma",
150+
migrations: {
151+
path: "prisma/migrations",
152+
// add-start
153+
seed: "tsx prisma/seed.ts"
154+
// add-end
155+
},
156+
datasource: {
157+
url: env("DATABASE_URL"),
158+
},
159+
});
177160
```
178-
179-
Some projects may require you to add compile options. When using Next.js for example, you would setup your seed script like so:
180-
181-
```json file=package.json
182-
"prisma": {
183-
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
184-
},
185-
```
186-
187161
5. To seed the database, run the `db seed` CLI command:
188162
```
189163
npx prisma db seed
@@ -281,15 +255,22 @@ Here we suggest some specific seed scripts for different situations. You are fre
281255
})
282256
```
283257

284-
3. Add the `prisma.seed` to your `package.json` file:
285-
```json file=package.json highlight=5;normal
286-
{
287-
"name": "my-project",
288-
"version": "1.0.0",
289-
"prisma": {
290-
"seed": "node prisma/seed.js"
291-
}
292-
}
258+
3. Add the `seed` field to your `prisma.config.ts` file:
259+
```ts file=prisma.config.ts
260+
import 'dotenv/config'
261+
import { defineConfig, env } from "prisma/config";
262+
export default defineConfig({
263+
schema: "prisma/schema.prisma",
264+
migrations: {
265+
path: "prisma/migrations",
266+
// add-start
267+
seed: "node prisma/seed.js"
268+
// add-end
269+
},
270+
datasource: {
271+
url: env("DATABASE_URL"),
272+
},
273+
});
293274
```
294275

295276
4. To seed the database, run the `db seed` CLI command:

content/200-orm/500-reference/380-connection-urls.mdx

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tocDepth: 3
77

88
Prisma ORM needs a connection URL to be able to connect to your database, e.g. when sending queries with [Prisma Client](/orm/prisma-client) or when changing the database schema with [Prisma Migrate](/orm/prisma-migrate).
99

10-
The connection URL is provided via the `url` field of a `datasource` block in your Prisma schema. It usually consists of the following components (except for SQLite and [Prisma Postgres](/postgres)):
10+
The connection URL is provided via the `url` field of a `datasource` block in your Prisma config (or Prisma schema if on version 6). It usually consists of the following components (except for SQLite and [Prisma Postgres](/postgres)):
1111

1212
- **User**: The name of your database user
1313
- **Password**: The password for your database user
@@ -66,82 +66,203 @@ DATABASE_URL="postgres://2f9881cc7eef46f094ac913df34c1fb441502fe66cbe28cc48998d4
6666

6767
When connecting via Prisma Accelerate, the connection string doesn't require a user/password like a conventional connection string does. Instead, authentication works via an API key:
6868

69+
<TabbedContent code>
70+
<TabItem value="Prisma 7">
71+
```ts file=prisma.config.ts
72+
export default defineConfig({
73+
datasource: {
74+
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
75+
},
76+
});
77+
```
78+
</TabItem>
79+
<TabItem value="Prisma 6">
6980
```prisma file=schema.prisma
7081
datasource db {
7182
provider = "postgresql"
7283
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
7384
}
7485
```
86+
</TabItem>
87+
88+
</TabbedContent>
7589

7690
In this snippet, `API_KEY` is a placeholder for the API key you are receiving when setting up a new Prismas Postgres instance via the [Prisma Console](https://console.prisma.io). Here is an example for what a real connection URL to Prisma Postgres may look like:
7791

92+
<TabbedContent code>
93+
<TabItem value="Prisma 7">
94+
```ts file=prisma.config.ts
95+
export default defineConfig({
96+
datasource: {
97+
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMGNkZTFlMjQtNzhiYi00NTY4LTkyM2EtNWUwOTEzZWUyNjU1IiwidGVuYW50X2lkIjoiNzEyZWRlZTc1Y2U2MDk2ZjI4NDg3YjE4NWMyYzA2OTNhNGMxNzJkMjhhOWFlNGUwZTYxNWE4NWIxZWY1YjBkMCIsImludGVybmFsX3NlY3JldCI6IjA4MzQ2Y2RlLWI5ZjktNDQ4Yy04NThmLTMxNjg4ODEzNmEzZCJ9.N1Za6q6NfInzHvRkud6Ojt_-RFg18a0601vdYWGKOrk"
98+
},
99+
});
100+
```
101+
</TabItem>
102+
<TabItem value="Prisma 6">
78103
```prisma file=schema.prisma
79104
datasource db {
80105
provider = "postgresql"
81106
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMGNkZTFlMjQtNzhiYi00NTY4LTkyM2EtNWUwOTEzZWUyNjU1IiwidGVuYW50X2lkIjoiNzEyZWRlZTc1Y2U2MDk2ZjI4NDg3YjE4NWMyYzA2OTNhNGMxNzJkMjhhOWFlNGUwZTYxNWE4NWIxZWY1YjBkMCIsImludGVybmFsX3NlY3JldCI6IjA4MzQ2Y2RlLWI5ZjktNDQ4Yy04NThmLTMxNjg4ODEzNmEzZCJ9.N1Za6q6NfInzHvRkud6Ojt_-RFg18a0601vdYWGKOrk"
82107
}
83108
```
109+
</TabItem>
110+
111+
</TabbedContent>
84112

85113
#### Local Prisma Postgres
86114

87115
The connection string for connecting to a [local Prisma Postgres](/postgres/database/local-development) instance mirrors the structure of a remote instance via Accelerate:
88116

117+
118+
<TabbedContent code>
119+
<TabItem value="Prisma 7">
120+
```ts file=prisma.config.ts
121+
export default defineConfig({
122+
datasource: {
123+
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
124+
},
125+
});
126+
```
127+
</TabItem>
128+
<TabItem value="Prisma 6">
89129
```prisma file=schema.prisma
90130
datasource db {
91131
provider = "postgresql"
92132
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
93133
}
94134
```
135+
</TabItem>
136+
137+
</TabbedContent>
95138

96139
However, in this case the `API_KEY` doesn't provide authentication details. Instead, it encodes information about the local Prisma Postgres instance. You can obtain a local connection string via the [`prisma dev`](/orm/reference/prisma-cli-reference#dev) command.
97140

98141
### PostgreSQL
99142

143+
144+
<TabbedContent code>
145+
<TabItem value="Prisma 7">
146+
```ts file=prisma.config.ts
147+
export default defineConfig({
148+
datasource: {
149+
url: "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
150+
},
151+
});
152+
```
153+
</TabItem>
154+
155+
<TabItem value="Prisma 6">
100156
```prisma file=schema.prisma
101157
datasource db {
102158
provider = "postgresql"
103159
url = "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
104160
}
105161
```
162+
</TabItem>
163+
164+
</TabbedContent>
106165

107166
### MySQL
108167

168+
169+
<TabbedContent code>
170+
<TabItem value="Prisma 7">
171+
```ts file=prisma.config.ts
172+
export default defineConfig({
173+
datasource: {
174+
url: "mysql://janedoe:mypassword@localhost:3306/mydb"
175+
},
176+
});
177+
```
178+
</TabItem>
179+
<TabItem value="Prisma 6">
109180
```prisma file=schema.prisma
110181
datasource db {
111182
provider = "mysql"
112183
url = "mysql://janedoe:mypassword@localhost:3306/mydb"
113184
}
114185
```
186+
</TabItem>
187+
188+
</TabbedContent>
115189

116190
### Microsoft SQL Server
117191

192+
193+
<TabbedContent code>
194+
<TabItem value="Prisma 7">
195+
```ts file=prisma.config.ts
196+
export default defineConfig({
197+
datasource: {
198+
url: "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
199+
},
200+
});
201+
```
202+
</TabItem>
203+
<TabItem value="Prisma 6">
118204
```prisma file=schema.prisma
119205
datasource db {
120206
provider = "sqlserver"
121207
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
122208
}
123209
```
210+
</TabItem>
211+
212+
</TabbedContent>
124213

125214
### SQLite
126215

216+
217+
<TabbedContent code>
218+
<TabItem value="Prisma 7">
219+
```ts file=prisma.config.ts
220+
export default defineConfig({
221+
datasource: {
222+
url: "file:./dev.db"
223+
},
224+
});
225+
```
226+
</TabItem>
227+
<TabItem value="Prisma 6">
127228
```prisma file=schema.prisma
128229
datasource db {
129230
provider = "sqlite"
130231
url = "file:./dev.db"
131232
}
132233
```
234+
</TabItem>
235+
236+
</TabbedContent>
133237

134238
### CockroachDB
135239

240+
241+
<TabbedContent code>
242+
<TabItem value="Prisma 7">
243+
```ts file=prisma.config.ts
244+
export default defineConfig({
245+
datasource: {
246+
url: "postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public"
247+
},
248+
});
249+
```
250+
</TabItem>
251+
<TabItem value="Prisma 6">
136252
```prisma file=schema.prisma
137253
datasource db {
138254
provider = "cockroachdb"
139255
url = "postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public"
140256
}
141257
```
258+
</TabItem>
259+
260+
</TabbedContent>
142261

143262
### MongoDB
144263

264+
_Support for MongoDB is limited to Prisma 6. We're working on support for MongoDB in Prisma 7_
265+
145266
```prisma file=schema.prisma
146267
datasource db {
147268
provider = "mongodb"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,15 @@ export default {
266266

267267
const users = await prisma.user.findMany();
268268
const result = JSON.stringify(users);
269+
ctx.waitUntil(prisma.$disconnect()); // or just await prisma.$disconnect()
269270
return new Response(result);
270271
},
271272
};
272273
```
273274

275+
We explicitly call `prisma.$disconnect()` here to guarantee timely release of resources or else the
276+
worker might run out of memory.
277+
274278
## 7. Run the Worker locally
275279

276280
With the database query in place and Prisma Client generated, you can run the Worker locally.

0 commit comments

Comments
 (0)