Database seeders will be used to seed the database with some initial data. Database seeders need to be run when you are initializing your database, just after you run your migrations.
A seeder is just a class which implements
ISeeder interface. ISeeder makes the class implement a method called
seed() which will be run when you run your seeder through the CLI.
There is also an
order static property which indicates the order of seeder execution. This will be used when you are
running all your migrations all at once. If you don't define this property, Silvie takes
0 as its default value and
may result in an unexpected behavior when you run all your migrations. So it is recommended to define this static
property with a proper value.
You can create a seeder manually or by using the make command of Silvie CLI. The following command will
roles.ts file in
Note that you can use this command along with other entity makers. The following command will create a
Role model, a
roles migration and a
After you created a seeder class and organized its order, You need to do the seeding job in the
seed() method. You can
either create your data with your models or insert them manually with raw database commands. An empty database seeder
will look like this:
Your models can be imported into the seeders to ease the seeding process. Here is an example of the
roles which uses
This example will create 4 roles in the database.
You can also use the database helper. The above example with raw MySQL queries will be like this:
The result will be same as the previous example.