Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeorm synchronize in production

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.

like image 354
Vahid Najafi Avatar asked Dec 09 '20 18:12

Vahid Najafi


People also ask

What does synchronize do TypeORM?

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.

How does TypeORM generate migration?

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.

What is migration in TypeORM?

Creating new migration migrations − TypeORM loads migrations from given directory. cli − indicates migration will create inside the specific directory.


3 Answers

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.

like image 153
Mohsen jalali Avatar answered Oct 19 '22 05:10

Mohsen jalali


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.

like image 34
Roger King Avatar answered Oct 19 '22 06:10

Roger King


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.

like image 20
Maik Avatar answered Oct 19 '22 06:10

Maik