Database: Seeding
Introduction
Athenna includes the ability to seed your database with data using seed classes. All seed classes are stored in the
database/seeders
directory.
Writing seeders
To generate a seeder, execute the make:seeder
Artisan command. All seeders generated by the framework will be placed in the
database/seeders
directory:
node artisan make:seeder UserSeeder
A seeder class only contains one method by default: run
. This method is called when the db:seed
Artisan command is
executed. Within the run
method, you may insert data into your database however you wish. You may use the query builder
to manually insert data, or you may use the model factories:
import { Seeder, Database } from '@athenna/database'
import { User } from '#app/Models/User'
export class UserSeeder extends Seeder {
/**
* Run the database seeders.
*
* @return {Promise<void>}
*/
async run() {
await Database.table('users').createMany([/*.....*/])
await User.factory().count(20).create()
}
}
Running seeders
You may execute the db:seed
Artisan command to seed your database. By default, the db:seed
command will run all the seeders
inside the database/seeders
folder, but you can run only one seeder using the --class
argument:
node artisan db:seed
node artisan db:seed --class=UserSeeder
Setting the seeder connection
If your seeder will be interacting with a database connection other than your application's default database connection,
you should set the static getter connection
in your seeder:
import { Seeder } from '@athenna/database'
export class UserSeeder extends Seeder {
/**
* Define the database connection to run the seeder.
*
* @return {string}
*/
static get connection() {
return 'postgres'
}
/**
* Run the database seeders.
*
* @return {Promise<void>}
*/
async run() {
await Database.table('users').createMany([/*.....*/])
await User.factory().count(20).create()
}
}