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
21 changes: 18 additions & 3 deletions apps/docs/content/docs/orm/prisma-migrate/workflows/seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,20 @@ model Post {

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

:::info[Prisma ORM v7 requirement]
In Prisma ORM v7, `PrismaClient` must be initialized with a driver adapter. The example below uses `@prisma/adapter-pg` with a PostgreSQL connection pool.
:::

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

const connectionString = `${process.env.DATABASE_URL}`;
const pool = new Pool({ connectionString });
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 @@ -114,18 +125,20 @@ async function main() {
main()
.then(async () => {
await prisma.$disconnect();
await pool.end();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
await pool.end();
process.exit(1);
});
```

- Add `typescript`, `tsx` and `@types/node` development dependencies:
- Add `typescript`, `tsx`, `@types/node`, `@prisma/adapter-pg`, `pg`, `@types/pg` and `dotenv` development dependencies:

```npm
npm install -D typescript tsx @types/node
npm install -D typescript tsx @types/node @prisma/adapter-pg pg @types/pg dotenv
```

- Add the `seed` field to your `prisma.config.ts` file:
Expand Down Expand Up @@ -176,10 +189,12 @@ main()
.then(rawSql)
.then(async () => {
await prisma.$disconnect();
await pool.end();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
await pool.end();
process.exit(1);
});
```
Expand Down