cli icon indicating copy to clipboard operation
cli copied to clipboard

Feature request: Natively run Typescript migrations and seeders

Open mgibson91 opened this issue 7 years ago • 6 comments

In the same way that current CLI works, it would be great to be able to consume Typescript files.

My current project heavily uses aliases paths in development, runtime and testing using:

  • Dev: tsconfig paths
  • Test: Jest moduleNameMapper
  • Production: tsnode and tsconfigpaths

It's extremely pleasant.The amount of different solutions there are (which can also be a nightmare to get configured in all three execution contexts) give an indication to the importance and popularity of aliased paths.

Currently, to reuse existing code, I write my migrations, seeders and models all in Typescript, then transpile the migrations and seeders to JS so the sequelize cli can work its magic.

However, the pain comes when the transpiles JS has not converted the aliased paths to relative paths and understandably, the Sequelize migrator cannot find these aliased paths.

It would be so good to be able to natively run the Typescript migrations and seeders, perhaps via ts-node and tsconfig-paths so that a user can reuse code in the same way the do in the rest of their project with no extra work.

I expect it would require the migration code to be converted to Typescript and then be executed as:

ts-node -r tsconfig-paths/register migrator.ts OR node --require ts-node/register -r tsconfig-paths/register migrator.ts

mgibson91 avatar Sep 18 '18 11:09 mgibson91

Since v5.0+ supports TS natively. I do really think it is a must. I am writing model in TS but migrations and seeders in es6. The most problem is I cannot use enum type in seeder and migrations, which is used to define the possible values for some column.

And is there any workaround for now?

raphaelsoul avatar May 06 '19 05:05 raphaelsoul

It seems like we have to deal manually with this problem by ourself. 🤔

TranBaVinhSon avatar Aug 05 '19 03:08 TranBaVinhSon

@sdepold @sushantdhiman @Americas We are using this feature along with feathersjs (ts), But the sequelize-clli generates the js files only, So help us to configure it to generate ts instead of js migration files.

jaideepghosh avatar Oct 30 '19 08:10 jaideepghosh

Since v5.0+ supports TS natively. I do really think it is a must. I am writing model in TS but migrations and seeders in es6. The most problem is I cannot use enum type in seeder and migrations, which is used to define the possible values for some column.

And is there any workaround for now?

You can do something like this below:

require('tsconfig-paths/register')
const {Status} = require('app/enums')

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const {INTEGER, TINYINT} = Sequelize

    return await queryInterface.createTable('table_name', {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      status: { type: TINYINT, allowNull: false, defaultValue: Status.public }
    })
  }
}

iMuFeng avatar May 28 '20 03:05 iMuFeng

Any updates as of 2021? Is this being planned for https://github.com/sequelize/cli/issues/941?

kabirbaidhya avatar May 11 '21 13:05 kabirbaidhya

I was able to get tsconfig paths to work by passing a flag to node rather than importing tsconfig-paths at the migration level

NODE_OPTIONS="-r ./scripts/bootstrapPaths.js" yarn sequelize-cli db:migrate

scripts/bootstrapPath.js

const tsConfig = require('../tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');

const { baseUrl, paths } = tsConfig.compilerOptions;
for (path in paths) {
	paths[path][0] = paths[path][0].replace('src', 'dist').replace('.ts', '.js');
}

tsConfigPaths.register({ baseUrl, paths });

TomerAtarScopio avatar Apr 07 '24 12:04 TomerAtarScopio