How to Seed Data in Adonis.js
Database seeding in Adonis.js allows you to populate your database with initial or sample data, which is especially useful for development and testing.
Creating a Seeder
- Generate a seeder file using the Ace CLI:
node ace make:seeder User
This will create a file in
database/seeders/
, e.g.,UserSeeder.ts
. - Edit the seeder file to define the data you want to insert. A typical seeder extends
BaseSeeder
and implements arun
method:import BaseSeeder from '@ioc:Adonis/Lucid/Seeder' import User from 'App/Models/User' export default class UserSeeder extends BaseSeeder { public async run () { await User.create({ email: 'example@adonisjs.com', password: 'secret', }) } }
You can also use methods like
updateOrCreateMany
to avoid duplicating records.
Running Seeders
- Run all seeders:
node ace db:seed
This command executes the
run
method of every seeder in thedatabase/seeders
directory. - Run a specific seeder file:
node ace db:seed --files "./database/seeders/UserSeeder.ts"
You can use the
--files
flag multiple times to specify more than one seeder. - Interactive mode:
node ace db:seed -i
This lets you select which seeders to run interactively.
- Specify a database connection:
node ace db:seed --connection=your_connection_name
The connection is passed to the seeder and can be used for model operations.
Additional Notes
- Seeders are typically used in development and testing environments.
- You can combine seeders with factories to generate large amounts of dummy data.
- Running seeders in production should be done with caution, and you may need to use the
--force
flag.
Summary:
To seed data in Adonis.js, create a seeder file, define your data in the run
method, and execute node ace db:seed
to populate your database.