DEV Community

Using TypeORM Migration in NestJS with Postgres Database

amir fakoor on May 03, 2023

If you are building a NestJS application and want to use a PostgreSQL database with TypeORM migration, then this article will guide you on creating...
Collapse
 
fozooni profile image
Ahmad Fozooni

Wow! This article is a huge time saver for me.
Thank you so much for sharing your knowledge Amir

Collapse
 
cyimanafaisal profile image
CYIMANA Faisal

Thanks for this it worked 🙏🏽

Collapse
 
polarisiota profile image
Polarisiota

Thanks~!

Collapse
 
dilumdarshana profile image
Dilum Darshana

Good explanation!!!

Collapse
 
codernirdesh profile image
Nirdesh pokharel

Thanks man! Saved my day.

Collapse
 
porya profile image
Porya

thanks for the great article
just note that the windows users must use

"migration:generate": "npm run typeorm -- -d ./src/config/typeorm.ts migration:generate ./src/migrations/%npm_config_name%",
"migration:create": "npm run typeorm -- migration:create ./src/migrations/%npm_config_name%",

for the scripts

Collapse
 
smurawczik profile image
Sebastian Murawczik

thanks for this, was really helpful

Collapse
 
felix_28fd916a3 profile image
Felix

Thank you, helped a lot!

Collapse
 
devwander profile image
Wanderson José

Thanks for the content, it helped me a lot!

Collapse
 
greenflag31 profile image
Green

Article should be reworked. Stuff is presented without explanation, the most important is to UNDERSTAND what you are doing, and no beeing a 'copy pasta' developer.
What does your config service contains ?
Why do you load your entities manually and then add 'autoLoad: true' ? You give the impression you don't understand what you are doing.

Collapse
 
coderkk1992_36 profile image
Kalith Kumasaru

you must be a delight to work with

Collapse
 
developer991 profile image
Devloper991

I agree with you Mr.Green.

Collapse
 
tongocduy1601 profile image
Tô Ngọc Duy • Edited

Hi Sir, when do follow your instructment, the migration file is generated at root folder,
I added those lines to customize migration folder, but not working,
how i do that
cli: {
migrationsDir: './src/database/migrations',
},

Collapse
 
developer991 profile image
Devloper991

This method only works when you're working with JavaScript, but if you're working with TypeScript, it doesn't work.
Also, remember that when using nestjs, Nestjs automatically handles Path aliases resolving. You can't simply assume this script will work even if you just run it with typeorm-ts-node-esm or typeorm-ts-node-commonjs because that's not enough to resolve the path aliases.
And, nonetheless, you didn't mention anything related to the edge cases when using ESM or commonjs.

Collapse
 
carlosalbertor profile image
Carlos Alberto R.

Thanks for this. Well done!