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

docs: Add seed configuration for database population with Nitro Tasks #107

Merged
merged 2 commits into from
May 22, 2024
Merged
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
53 changes: 53 additions & 0 deletions docs/content/docs/4.recipes/2.drizzle.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,59 @@ Note that we are also exporting the `User` type, which is inferred from the `use
We also export the `sql`, `eq`, `and`, and `or` functions from `drizzle-orm` to be used in our queries.
::

### Seed the database (Optional)

::collapsible{name="instructions"}

Optionally, you can add a server task to populate your database with initial data. This uses [Nitro Tasks](https://nitro.unjs.io/guide/tasks), which is currently an experimental feature.

1. Update your nuxt.config.js:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
experimental: {
tasks: true
}
}
})
```

2. Create a new file containing the task:

```ts [server/task/seed.ts]
export default defineTask({
meta: {
name: "db:seed",
description: "Run database seed task",
},
async run() {
console.log("Running DB seed task...")
const seed = [
{
name: "John Doe",
email: "john@example.com",
password: "password123",
avatar: "https://example.com/avatar/john.png",
createdAt: Date.now(),
},
{
name: "Jane Doe",
email: "jane@example.com",
password: "password123",
avatar: "https://example.com/avatar/jane.png",
createdAt: Date.now(),
},
]
await useDrizzle().insert(tables.users).values(users);
return { result: "success" }
}
})
```

To run the seed task, start your dev server and open the Nuxt Devtools. Go to _Tasks_ and you will see the `db:seed` task ready to run. This will add the seed data to your database and give you the first users to work with.

::

## Usage

Expand Down
Loading