Skip to content

Commit

Permalink
Merge branch 'prisma:main' into enhancement/nextjs-best-practices
Browse files Browse the repository at this point in the history
  • Loading branch information
decovicdev authored May 1, 2024
2 parents ffeeef6 + a321776 commit db8093e
Showing 1 changed file with 127 additions and 5 deletions.
132 changes: 127 additions & 5 deletions content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Here we suggest some specific seed scripts for different situations. You are fre

### Seeding your database with TypeScript or JavaScript

> If you're using TypeScript with PostgreSQL, SQLite or MySQL see also: [@snaplet/seed](/orm/prisma-migrate/workflows/seeding#seeding-your-database-with-snapletseed).
<TabbedContent transparent code>

<TabItem value="TypeScript">
Expand All @@ -89,7 +91,7 @@ Here we suggest some specific seed scripts for different situations. You are fre
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
Expand Down Expand Up @@ -205,19 +207,19 @@ Here we suggest some specific seed scripts for different situations. You are fre
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
Expand Down Expand Up @@ -385,6 +387,126 @@ psql file.sql

</TabbedContent>

### Seeding your database with `@snaplet/seed`

[`@snaplet/seed`](https://docs.snaplet.dev/seed/getting-started/overview) offers a toolkit designed to understand your database schema, enabling you to generate production-accurate data and efficiently seed your database. It uses the full power of TypeScript to provide you with a type-safe and auto-completed experience to write your seed scripts.

`@snaplet/seed` supports PostgreSQL, SQLite and MySQL.

1. Begin by setting up a local database using `npx prisma migrate dev`. This example will use the following Prisma schema featuring a `User` and `Post` model:

```prisma file=schema.prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String
published Boolean
user User @relation(fields: [userId], references: [id])
userId Int
}
```

2. Execute `npx @snaplet/seed init prisma/seed` to initialize Snaplet Seed in your Prisma project. This command generates the seed client and creates an example seed file at `prisma/seed/seed.ts`.

> **Note:** Snaplet Seed also has an [AI-enhanced version](https://www.snaplet.dev/seed#ai). You can opt in for improved seed data quality with a free Snaplet account.
3. Use the generated `@snaplet/seed` client within the `prisma/seed/seed.ts` file to compose your seeds.

Here's how to create new users and posts by modifying the initially generated `prisma/seed/seed.ts` file:

```ts file=prisma/seed/seed.ts
/**
* ! Executing this script will delete all data in your database and seed it with 10 users.
* ! Make sure to adjust the script to your needs.
* Use any TypeScript runner to run this script, for example: `npx tsx seed.ts`
* Learn more about the Seed Client by following our guide: https://docs.snaplet.dev/seed/getting-started
*/
import { createSeedClient } from "@snaplet/seed";

async function main() {
const seed = await createSeedClient();

// Truncate all tables in the database
await seed.$resetDatabase();

//highlight-start
// Seed the database with 10 users
await seed.user((createMany) => createMany(10, {
// Create 10 posts for each of those users
posts: (createMany) => createMany(10),
}))
//highlight-end

console.log("Database seeded successfully!");

process.exit();
};

main();
```

4. Add `tsx` (or any other typescript runners) as a development dependency:

```terminal
npm install -D tsx
```

5. Insert the `prisma.seed` field into your `package.json` file and configure commands to seed your database:

```json file=package.json
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
//add-next-line
"seed": "tsx prisma/seed/seed.ts"
},
"devDependencies": {
"@types/node": "^14.14.21",
"tsx": "^4.7.2",
"typescript": "^4.1.3"
}
}
```

6. To seed the database, execute the following CLI command:

```terminal
npx prisma db seed
```

7. To ensure the seed client remains synchronized with your schema, add a `migrate` and `postmigrate` scripts to the package.json:

```json file=package.json
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "npx tsx prisma/seed/seed.ts"
},
"scripts": {
//add-start
"migrate": "prisma migrate dev",
"postmigrate": "npx @snaplet/seed --config prisma/seed/seed.config.ts sync"
//add-end
}
"devDependencies": {
"@types/node": "^14.14.21",
"tsx": "^4.7.2",
"typescript": "^4.1.3"
}
}
```

Now, instead of `npx prisma migrate dev` you can run `npm run migrate` which will also sync the seed client after schema changes and keep both your database and seed client in sync.

### User-defined arguments

> This feature is available from version 4.15.0 and later.
Expand Down Expand Up @@ -438,5 +560,5 @@ npx prisma db seed -- --environment development

Here's a non-exhaustive list of other tools you can integrate with Prisma ORM in your development workflow to seed your database:

- [Replibyte](https://www.replibyte.com/docs/introduction)
- [Snaplet](https://docs.snaplet.dev/recipes/prisma)
- [Replibyte](https://www.replibyte.com/docs/introduction)

0 comments on commit db8093e

Please sign in to comment.