Seeds And Factories

Seed File: A seed file contains real/dummy data that you want to default to your database. Factory File: A factory file is a function or method that returns objects of class, which is assumed to be "new".

How to run a seed file?

To run a seed file you must be run the command:

Note: you must be very careful when creating factories or seeders since every time you execute the npm run db:seed command they will be executed.

It is recommended to use some validation to verify that the record does not exist.

In the src/common/database/init.seeder.ts file is the main configuration to be able to run all the seeds and factories files.

When you create a new seed file you should create it in src/common/database/seeds/ since the init.seeder.ts file is configured to read seeds only from this folder. Seeder example:

If you need to create factories to be able to generate data dummy in your database, you must be create the factory file in src/common/databases/factories Factory example:

How to use a factory file in Seed file?

Now in the file the factoryManager.get() method is used to call the factory created for the Entity "user". And with the saveMany() method we establish how many records we want to generate. Example:

Last updated

Was this helpful?