Synchronize: true destroyed a production database: Here's the migration setup that prevents it
I used synchronize: true in a NestJS project for way longer than I should have. It was fine in development. But when I renamed a column in an entity, pushed to production, and TypeORM dropped the old column and created a new one on the next restart., data was gone.
You can fix this with migrations, but the TypeORM + NestJS migration setup is very confusing because the TypeORM CLI doesn't know about NestJS's dependency injection. You end up needing two configs, one for AppModule, one for the CLI, and getting them to share the same values without duplicating everything takes a bit of work.
The piece that finally made it click for me was the data-source.ts file. It's a standalone TypeORM DataSource that reads from your .env directly, no NestJS involved. The CLI uses that. NestJS uses ConfigService in AppModule. Both point to the same database, same entities, same migrations folder.