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/300-prisma-migrate/300-workflows/10-seeding.mdx
+51-70Lines changed: 51 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,46 +16,29 @@ This guide describes how to seed your database using Prisma Client and Prisma OR
16
16
17
17
## How to seed your database in Prisma ORM
18
18
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
-
<TabItemvalue="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
+
exportdefaultdefineConfig({
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
+
});
27
34
```
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`.
This can be useful to reduce memory usage (RAM) and increase execution speed of the seed script.
38
-
39
-
:::
40
-
41
-
</TabItem>
42
-
43
-
<TabItemvalue="JavaScript">
44
-
```json
45
-
"prisma": {
46
-
"seed": "node prisma/seed.js"
47
-
},
48
-
```
49
-
</TabItem>
50
-
51
-
</TabbedContent>
52
35
53
36
## Integrated seeding with Prisma Migrate
54
37
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.
56
39
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.
58
40
41
+
### Prisma 6 Only
59
42
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.
60
43
61
44
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
153
136
})
154
137
```
155
138
156
-
3. Add `typescript`, `ts-node` and `@types/node` development dependencies:
139
+
3. Add `typescript`, `tsx` and `@types/node` development dependencies:
157
140
```
158
-
npm install -D typescript ts-node @types/node
141
+
npm install -D typescript tsx @types/node
159
142
```
160
143
161
-
<aid="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
+
exportdefaultdefineConfig({
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
+
});
177
160
```
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:
Copy file name to clipboardExpand all lines: content/200-orm/500-reference/380-connection-urls.mdx
+122-1Lines changed: 122 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ tocDepth: 3
7
7
8
8
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).
9
9
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)):
11
11
12
12
-**User**: The name of your database user
13
13
-**Password**: The password for your database user
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:
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:
The connection string for connecting to a [local Prisma Postgres](/postgres/database/local-development) instance mirrors the structure of a remote instance via Accelerate:
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.
0 commit comments