Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove steps for snaplet #6456

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from 2 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
122 changes: 1 addition & 121 deletions content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ 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 Down Expand Up @@ -385,124 +383,6 @@ 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`.

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 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 @@ -556,5 +436,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:

- [Snaplet](https://docs.snaplet.dev/recipes/prisma)
- Supabase community project: [Seed by Snaplet](https://github.com/supabase-community/seed)
nikolasburk marked this conversation as resolved.
Show resolved Hide resolved
- [Replibyte](https://www.replibyte.com/docs/introduction/)
Loading