Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,22 @@ Here we suggest some specific seed scripts for different situations. You are fre

Create some new users and posts in your `seed.ts` file:

:::info[Prisma ORM v7 requires an adapter]

In Prisma ORM v7, PrismaClient requires either an `adapter` or `accelerateUrl` option. The example below shows how to use the PostgreSQL adapter. For other databases, see the [database drivers documentation](/orm/overview/databases/database-drivers).

:::

```js file=seed.ts
import { PrismaClient } from '../prisma/generated/client'
const prisma = new PrismaClient()
import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import { Pool } from 'pg'
import 'dotenv/config'

const pool = new Pool({ connectionString: process.env.DATABASE_URL })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })

async function main() {
const alice = await prisma.user.upsert({
where: { email: 'alice@prisma.io' },
Expand Down Expand Up @@ -137,20 +150,31 @@ Here we suggest some specific seed scripts for different situations. You are fre
main()
.then(async () => {
await prisma.$disconnect()
await pool.end()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
await pool.end()
process.exit(1)
})
```

3. Add `typescript`, `tsx` and `@types/node` development dependencies:
3. Add common development dependencies:
```
npm install -D typescript tsx @types/node
npm install -D typescript tsx @types/node dotenv
```

4. Add the `seed` field to your `prisma.config.ts` file:
4. Add database-specific adapter dependencies.

For PostgreSQL (as shown in the example above):
```
npm install -D @prisma/adapter-pg pg @types/pg
```

For other databases, see the [database drivers documentation](/orm/overview/databases/database-drivers).

5. Add the `seed` field to your `prisma.config.ts` file:
```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from "prisma/config";
Expand Down Expand Up @@ -208,9 +232,21 @@ Here we suggest some specific seed scripts for different situations. You are fre

Create some new users and posts in your `seed.js` file:

:::info[Prisma ORM v7 requires an adapter]

In Prisma ORM v7, PrismaClient requires either an `adapter` or `accelerateUrl` option. The example below shows how to use the PostgreSQL adapter. For other databases, see the [database drivers documentation](/orm/overview/databases/database-drivers).

:::

```js file=seed.js
require('dotenv/config')
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const { PrismaPg } = require('@prisma/adapter-pg')
const { Pool } = require('pg')

const pool = new Pool({ connectionString: process.env.DATABASE_URL })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })

async function main() {
const alice = await prisma.user.upsert({
Expand Down Expand Up @@ -256,15 +292,26 @@ Here we suggest some specific seed scripts for different situations. You are fre
main()
.then(async () => {
await prisma.$disconnect()
await pool.end()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
await pool.end()
process.exit(1)
})
```

3. Add the `seed` field to your `prisma.config.ts` file:
3. Add database-specific adapter dependencies.

For PostgreSQL (as shown in the example above):
```
npm install -D @prisma/adapter-pg pg dotenv
```

For other databases, see the [database drivers documentation](/orm/overview/databases/database-drivers).

4. Add the `seed` field to your `prisma.config.ts` file:
```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from "prisma/config";
Expand All @@ -282,7 +329,7 @@ Here we suggest some specific seed scripts for different situations. You are fre
});
```

4. To seed the database, run the `db seed` CLI command:
5. To seed the database, run the `db seed` CLI command:
```
npx prisma db seed
```
Expand Down