#Running typeorm migrations in production

5 messages · Page 1 of 1 (latest)

spark wharf
#

Hello, friends. I've ran into a conundrum: I can either transpile my .ts migrations to .js, and then run them with typeorm in node mode, or, as typeorm itself is made with typescript in mind, I can just separate the migration files from compilation, and run them in their .ts form, like with "ts-node typeorm migration:run". So would running typescript files in production, but not on the server code itself, be a problem?

Also, considering I'll run the transpiled migrations in production, I'm having trouble thinking about the package.json script to do it. For development, I can use the script
```"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"````

to run
yarn typeorm migration:run

but then, if I want to run the transpiled migrations, how would I separate? should I create another script, like:
"typeorm:prod": "node ./node_modules/typeorm/cli.js"

to run the .js migrations?

Thanks in advance to anybody willing to help me with this!

valid pecan
#

While this question is not directly related to TS itself, I'll attempt to answer...

Since migrations are not coupled to you application beyond preparing a database for use; best practice says to run the migrations separately unless your app can detect the outdated database and execute the migration as needed.
You can use yarn dlx ts-node-dev ... (Preferably by adding it to your scripts)
It may be more worth your while to write your migrations in JS using JSDoc syntaxes to enable your IDE's features. This is how my team accomplishes writing a TS project with Sequelize.

spark wharf
#

That makes sense, migrations are not app code, so I should run them separately. Thanks a lot for your help!

valid pecan
#

No problem, I spent like 2 weeks on this problem a few years ago before I got my first underling to point out my stupidity.

#

@spark wharf please make sure to mark this resolved!