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 UserThis 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
BaseSeederand implements arunmethod: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
updateOrCreateManyto avoid duplicating records.
Running Seeders
- Run all seeders:
node ace db:seedThis command executes the
runmethod of every seeder in thedatabase/seedersdirectory. - Run a specific seeder file:
node ace db:seed --files "./database/seeders/UserSeeder.ts"You can use the
--filesflag multiple times to specify more than one seeder. - Interactive mode:
node ace db:seed -iThis lets you select which seeders to run interactively.
- Specify a database connection:
node ace db:seed --connection=your_connection_nameThe 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
--forceflag.
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.