sequelize / cli

The Sequelize CLI
MIT License
2.53k stars 529 forks source link

Feature request: Natively run Typescript migrations and seeders #693

Open mgibson91 opened 5 years ago

mgibson91 commented 5 years ago

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:

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

raphaelsoul commented 5 years ago

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?

TranBaVinhSon commented 5 years ago

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

jaideepghosh commented 4 years ago

@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.

iMuFeng commented 4 years ago

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 }
    })
  }
}
kabirbaidhya commented 3 years ago

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

TomerAtarScopio commented 5 months ago

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 });