Seeding
You can use a seeder class to quickly populate your database with some dummy data. This is very useful during testing as well as for demo apps. Coupled with an Entity Factory, this is a very powerful tool to quickly cook up some test data.
Getting Started
A seeder is just a class, or an object, extending the abstract dev.alpas.ozone.Seeder
class.
The extending class is required to override the only abstract method — run()
.
The run()
methods receives an instance of Application
to make it easier for you to resolve dependencies, if any.
Inside the run()
method, you can populate your data and insert them into a database in any way you want. However,
you may want to hook it up with an Entity Factory for quickly creating entity instances.
/info/A seeder can be named anything but they usually end with the suffix
Seeder
. They are also conventionally kept indatabase/seeds
folder.
Creating a Seeder
You can create a seeder by extending the abstract Seeder
class or use make:seeder
command
to create one or multiple seeders in a jiffy.
# creates 3 seeders under database/seeds folder
$ alpas make:seeder ArticlesSeeder UsersSeeder CommentsSeeder
internal class ArticlesSeeder : Seeder() {
override fun run(app: Application) {
// Do something
}
}
Running a Seeder
You can run a seeder by using the db:seed
Alpas console command. This command optionally takes the name
of the seeder to run. If no name is passed, it runs the DatabaseSeeder
class. If this class doesn't
exist, then it will throw an error.
# Runs the default DatabaseSeeder
$ alpas db:seed
# Runs the ArticlesSeeder
$ alpas db:seed ArticlesSeeder
You can run multiple seeders by calling their run()
methods from another seeder's run()
method.
Here is an example of running 3 seeders from within a DatabaseSeeder
class.
internal class DatabaseSeeder : Seeder() {
override fun run(app: Application) {
UsersSeeder().run(app)
ArticlesSeeder().run(app)
CommentsSeeder().run(app)
}
}
Now, if you run the DatabaseSeeder
by using alpas db:seed
, it will effectively run
UserSeeder
, ArticleSeeder
, and CommentSeeder
.