I am running my Node JS backend using typeorm ORM.
Coming from Entity Framework, it was very easy to seed the db with a few lines such as
Database.SetInitializer(new DbInitializer());
Where the DbInitializer class would contain all the seeding info.
Is there a similar approach to seed the database in TypeOrm? If not, what is the recommended way of doing it?
1) Create a new migration with the data insertion statements? 2) Create a task where you instantiate and save entities?
Data seeding is the process of populating a database with an initial set of data. There are several ways this can be accomplished in EF Core: Model seed data. Manual migration customization.
First create your TypeORM entites. Then for each entity define a factory. The purpose of a factory is to create new entites with generate data. Note: Factories can also be used to generate data for testing. And last but not least, create a seeder. The seeder can be called by the configured cli command seed:run.
For those who are using TypeORM with Nest.js, here is a solution to perform your seeding programatically, from within your code. We create a dedicated "seeding module" containing a "seeding middleware" that is responsible for conducting the seeding and ensuring that all seeding is done before any request is answered.
Database seeding can be done in different contexts, from inserting random batches so that we can test our application faster (which helps in our development experience), but we can also define an array of objects that can be data. admin accounts or simply data that needs to be used in the production environment.
Working with a database starts from creating tables. How do you tell TypeORM to create a database table? The answer is-through the models. Your models in your app are your database tables.
Unfortunately, there is no officially released solution from TypeORM (at the time this answer was being published).
But there is a nice workaround we can use:
ormconfig.js
file and specify another folder for "migrations" - in fact our seeds -c <connection name>
. That's it!Sample ormconfig.js:
module.exports = [ { ..., migrations: [ 'src/migrations/*.ts' ], cli: { migrationsDir: 'src/migrations', } }, { name: 'seed', ..., migrations: [ 'src/seeds/*.ts' ], cli: { migrationsDir: 'src/seeds', } } ]
Sample package.json:
{ ... scripts: { "seed:generate": "ts-node typeorm migration:generate -c seed -n ", "seed:run": "ts-node typeorm migration:run -c seed", "seed:revert": "ts-node typeorm migration:revert -c seed", }, ... }
I would love to see such functionality as well (and we're not alone), but at the moment, there's no official feature for seeding.
in lack of such a built-in feature, I think the next best thing would be to create a migration script named 0-Seed
(so it precedes any other migration scripts you might have) and have the seed data populated there.
@bitwit has created a snippet that may come in handy for you; it's a function that reads the data from yaml files, which you can incorporate in the seed migration script.
after some research, however, I found another interesting approach: bind an after_create
event to the table, and initialize the data in the listener.
I haven't implemented this, so I'm not sure it can be done directly with TypeORM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With