Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

feat: add @snaplet/seed recipe example #1

Closed
wants to merge 25 commits into from
Closed
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f54e10a
feat: add @snaplet/seed recipe example
Apr 10, 2024
09dfc82
Apply suggestions from code review
avallete Apr 11, 2024
7d63a89
fix: apply other comments
Apr 11, 2024
f95695e
fix: rename seed.mts to seed.ts
Apr 18, 2024
7524b9c
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
Apr 23, 2024
b87fa0c
fix: tag link
Apr 23, 2024
c1d8550
Apply suggestions from code review
avallete Apr 23, 2024
a350d1c
fix: file indent
Apr 23, 2024
1dce215
fix: examples indents
Apr 23, 2024
876041e
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
avallete Apr 23, 2024
72f35e8
Update content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
jgoux Apr 23, 2024
8f37c74
Update content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
jgoux Apr 23, 2024
c6f7340
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
jgoux Apr 23, 2024
1071587
rename x to createMany
jgoux Apr 24, 2024
74b5a5d
Update content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
jgoux Apr 25, 2024
03bdb42
Update content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
jgoux Apr 25, 2024
bc14595
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
jgoux Apr 29, 2024
0960751
update snaplet seed
jgoux Apr 29, 2024
08db595
chore: mention that cloud account is opt-in and what for
Apr 29, 2024
48d36ad
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
avallete Apr 29, 2024
811dfc1
try out remark-lint GHA (#5956)
jharrell May 1, 2024
ac452e9
Bump sass from 1.75.0 to 1.76.0 (#5963)
dependabot[bot] May 1, 2024
b31909a
Bump wrangler from 3.51.2 to 3.52.0 (#5947)
dependabot[bot] May 1, 2024
d586f8f
Apply suggestions from code review
avallete May 1, 2024
471578c
Merge branch 'main' into feat/add-snaplet-seed-recipe-example
avallete May 1, 2024
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
115 changes: 114 additions & 1 deletion 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 or SQLite, 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,6 +387,117 @@ psql file.sql

</TabbedContent>

### Seeding your database with <inlinecode>@snaplet/seed</inlinecode>

[`@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 use the full power of TypeScript to provide you with
a type-safe and auto-completed experience to write your seed scripts.

It supports PostgreSQL and SQLite.

1. Begin by setting up a local database using `npx prisma migrate dev`. For instance, consider 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` to introspect your database. This command generates the seed client and creates a `seed.ts` file.
3. Use the generated `@snaplet/seed` client within the `seed.ts` file to compose your seeds.

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

```ts file=seed.ts highlight=14-20;normal
/**
* ! Executing this script will delete all data in your database and seed it with 10 versions.
* ! 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'

const seed = await createSeedClient()

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

// Seed the database with 10 User
await seed.User((x) =>
x(10, {
// Create 10 posts for each of those users
Post: (x) => x(10),
})
)

process.exit()
```

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

```
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 highlight=5;normal
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "npx tsx seed.ts"
},
"devDependencies": {
"@types/node": "^14.14.21",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
```

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

```
npx prisma db seed
```

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

```json file=package.json highlight=8-9;normal
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "npx tsx seed.ts"
},
"scripts": {
"migrate": "prisma migrate dev",
"postmigrate": "npx @snaplet/seed sync"
}
"devDependencies": {
"@types/node": "^14.14.21",
"ts-node": "^9.1.1",
"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 +551,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)