Skip to content

Commit c081b59

Browse files
onmaxatinux
andauthored
docs: Add seed configuration for database population with Nitro Tasks (#107)
Co-authored-by: Sébastien Chopin <[email protected]>
1 parent d06beb2 commit c081b59

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

docs/content/docs/4.recipes/2.drizzle.md

+53
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,59 @@ Note that we are also exporting the `User` type, which is inferred from the `use
154154
We also export the `sql`, `eq`, `and`, and `or` functions from `drizzle-orm` to be used in our queries.
155155
::
156156
157+
### Seed the database (Optional)
158+
159+
::collapsible{name="instructions"}
160+
161+
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.
162+
163+
1. Update your nuxt.config.js:
164+
165+
```ts [nuxt.config.ts]
166+
export default defineNuxtConfig({
167+
nitro: {
168+
experimental: {
169+
tasks: true
170+
}
171+
}
172+
})
173+
```
174+
175+
2. Create a new file containing the task:
176+
177+
```ts [server/task/seed.ts]
178+
export default defineTask({
179+
meta: {
180+
name: "db:seed",
181+
description: "Run database seed task",
182+
},
183+
async run() {
184+
console.log("Running DB seed task...")
185+
const seed = [
186+
{
187+
name: "John Doe",
188+
189+
password: "password123",
190+
avatar: "https://example.com/avatar/john.png",
191+
createdAt: Date.now(),
192+
},
193+
{
194+
name: "Jane Doe",
195+
196+
password: "password123",
197+
avatar: "https://example.com/avatar/jane.png",
198+
createdAt: Date.now(),
199+
},
200+
]
201+
await useDrizzle().insert(tables.users).values(users);
202+
return { result: "success" }
203+
}
204+
})
205+
```
206+
207+
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.
208+
209+
::
157210

158211
## Usage
159212

0 commit comments

Comments
 (0)