In Typeorm there is a feature called synchronize
. You can synchronize entities with a database, so there is no need for migirations. But as you know synchronize
is dangerous for production.
Here is the question, when should I use the synchronize
feature? Imagine at first (in the development environment) I started using the synchronize
feature. If I disable it in production while I have no migration, how should my production database will going to be created?
Also, I'm going to deliver the project on some milestones. Should I disable it at the first milestone or at the end? And for long time maintenance, should I use synchronize
disabled and use migration after the first production release?
Any idea would be appreciated.
Synchronize makes the entity sync with the database every time you run your application. Hence, whenever you add columns to an entity, create a new table by creating a new entity, or remove columns from an existing table by modifying an entity it will automatically update the database once the server is started.
TypeORM is able to automatically generate migration files with schema changes you made. See, you don't need to write the queries on your own. The rule of thumb for generating migrations is that you generate them after each change you made to your models.
Creating new migration migrations − TypeORM loads migrations from given directory. cli − indicates migration will create inside the specific directory.
Migrations in TypeORM Even Though synchronization is a good option to synchronize your entity with the database, it is unsafe for production databases. Therefore migrations can be an alternative solution for safer migrations in production databases. When doing a migration, you should follow the below steps.
1. Update the Typeorm config file and package.json file
You should change the synchronize attribute to false in Typeorm config file as the first step to prevent schema synchronization. Then add the following command to the scripts attribute under the package.json file.
“typeorm”: “ts-node ./node_modules/typeorm/cli -f ./ormconfig.json”
2. Generate the migration
npm run typeorm migration:generate -n
Here you can give a name to your migration. After you run the command you will find a migration file under migrations with the name .
In the migration file, there are two functions namely up and down where up function responsible for running the migration and down for reverting the migration.
3. Run the migration
npm run typeorm migration:run
This command will run the migration which you have already created in the above command. When you run this command, it will execute the up function in the migration file.
4. Revert the migration
npm run typeorm migration:revert
This command will revert the migration which you have already executed in the above command. When you run this command, it will revert all the migrations which you have already done. Basically, it will run the down command of the migration file.
Synchronize is a great option to get up an running, but in my opinion you should always default to creating migrations. This is because it will enforce you to run your development environment similar to production, which is always key. You want to make your Dev environment run like production.
migration:generate
is a great middle ground to building your migration files from your entities.
I also have to decide that issue now. And i will use the syncronize option only for my e2e test db. As Roger King already mentioned, you want to have your dev and prod environments using the same tools to change the database. In this way you can prevent different behaviors between them.
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