Using TypeScript
If you’re using NodeJS you have at least the two following options to support running TypeScript migration files in NodeJS.
Using tsx
If you want to be able to write and run migration files written in TypeScript an easy way is to install the tsx package.
Installing tsx
npm install tsxpnpm add tsxyarn add tsxLoading TypeScript migrations
After installing tsx you can load it in two ways.
Via CLI
Using the --import flag you can load tsx before running your migration files.
npx emigrate up --import tsxpnpm emigrate up --import tsxyarn emigrate up --import tsxVia configuration file
You can also directly import tsx in your configuration file (will only work if you’re not using TypeScript for your configuration file).
import 'tsx';
export default { // ...};Then you can run your migration files as usual:
npx emigrate uppnpm emigrate upyarn emigrate upBuilding TypeScript migrations
If you don’t want to have tsx (or similar) as a dependency included in your production environment then
you can build your TypeScript migration files using the tsc compiler or
some other tool that are already part of your build process when transpiling your TypeScript code to JavaScript.
Assume that you have all of your migrations in a src/migrations directory and you have built them to a dist/migrations directory.
Then you can run your migration files by pointing to the dist/migrations directory:
npx emigrate up -d dist/migrationspnpm emigrate up -d dist/migrationsyarn emigrate up -d dist/migrations