When initializing your application or testing it can be exhausting to create sample data for your database. The solution is to use seeding. Create factories for your entities and use them in the seed script or combine multiple seed scripts.
A seeder class contains one method
run. This method is called when you use the command
npx mikro-orm seeder:run. In the
run method you define how and what data you want to insert into the database. You can create entities using the EntityManager or you can use Factories.
You can create your own seeder classes using the following CLI command:
This creates a new seeder class. By default it will be generated in the
./database/seeder/ directory. You can configure the directory in the config with the key
As an example we will look at a very basic seeder.
Running a seeder from the command line or programmatically will automatically call
runmethod has completed.
Instead of specifying all the attributes for every entity, you can also use entity factories. These can be used to generate large amounts of database records. Please read the documentation on how to define factories to learn how to define your factories.
As an example we will generate 10 authors.
run method you can specify other seeder classes. You can use the
call method to breakup the database seeder into multiple files to prevent a seeder file from becoming too large. The
call method accepts an array of seeder classes.
When testing you may insert entities in the database before starting a test. Instead of specifying every attribute of every entity by hand, you could also use a
Factory to define a set of default attributes for an entity using entity factories.
Lets have a look at an example factory for an Author entity.
Basically you extend the base
Factory class, define a
model property and a
definition method. The
model defines for which entity the factory generates entity instances. The
definition method returns the default set of attribute values that should be applied when creating an entity using the factory.
Via the faker property, factories have access to the Faker library, which allows you to conveniently generate various kinds of random data for testing.
Once you defined your factories you can use them to generate entities. Simply import the factory, instantiate it and call the
Generate multiple entities by calling the
make method. The parameter of the
make method is the number of entities you generate.
If you would like to override some of the default values of your factories, you may pass an object to the make method. Only the specified attributes will be replaced while the rest of the attributes remain set to their default values as specified by the factory.
create method instantiates entities and persists them to the database using the
persistAndFlush method of the EntityManager.
You can override the default values of your factories by passing an object to the
It is nice to create large quantities of data for one entity, but most of the time we want to create data for multiple entities and also have relations between these. For this we can use the
each method which can be chained on a factory. The
each method can be called with a function that transforms output entity from the factory before returning it. Lets look at some examples for the different relations.
You may execute the
seeder:run MikroORM CLI command to seed your database. By default, the
seeder:run command runs the DatabaseSeeder class, which may in turn invoke other seed classes. However, you may use the
--class option to specify a specific seeder class to run individually:
You may also seed your database using the
schema:fresh command in combination with the
--seed option, which will drop all tables and re-run all of your migrations or generate the database based on the current entities. This command is useful for completely re-building your database:
Now we know how to create seeders and factories, but how can we effectively use them in tests. We will show an example how it can be used.