Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM how to seed database

Tags:

typeorm

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?

like image 805
Aaron Ullal Avatar asked Jul 05 '18 19:07

Aaron Ullal


People also ask

What is seed data in database?

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.

How to create a Seeder in typeorm?

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.

Is there a way to programatically seed nest with typeorm?

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.

What is Database seeding and how to do it?

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.

How do you work with a database in typeorm?

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.


Video Answer


2 Answers

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:

  1. create another connection inside ormconfig.js file and specify another folder for "migrations" - in fact our seeds
  2. generate and run your seeds with -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",   },   ... } 
like image 145
oleh.meleshko Avatar answered Sep 20 '22 17:09

oleh.meleshko


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.

like image 35
Eliran Malka Avatar answered Sep 19 '22 17:09

Eliran Malka