cli icon indicating copy to clipboard operation
cli copied to clipboard

Sequelize CLI in 2021

Open papb opened this issue 5 years ago • 11 comments

Hello everyone!

I will be doing a large overhaul of the Sequelize CLI this month. Stay tuned!

papb avatar Apr 05 '21 15:04 papb

Please, add typescript support of migrations and seeds

StanislavTemchenko avatar Apr 07 '21 00:04 StanislavTemchenko

Hello,

I don't know if i'm in the right place to post this question, sorry if it's not.

I use Sequelize Migrations to handle database versioning and migrations are managed by SequelizeMeta table. This table only contains the filename of migration files that have been applied.

At this moment, is it possible to add more information to SequelizeMeta table like a description of migrations changes (createTable, addConstraint, addColumn, ...) ?

The idea behind this is to have more information like databasechangelog table from liquibase.

nassimerrahoui avatar Apr 19 '21 10:04 nassimerrahoui

Typescript implementation would be awesome!

ThomasBerne avatar Apr 30 '21 14:04 ThomasBerne

You can currently use typescript in migrations & seeders. But you need to first compile it to javascript. Here is the example

import { QueryInterface, SequelizeStatic } from "sequelize";
import { Helpers } from "../util/helpers.util";
import { DischargePointType } from "../enums/discharge-point-type.enum";

export = {
  up  : (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.createTable("states", {
      id                                : {
        allowNull    : false,
        autoIncrement: true,
        primaryKey   : true,
        type         : Sequelize.BIGINT
      },
      title                             : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      state_code                        : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      discharge_point_type              : {
        type        : Sequelize.ENUM,
        allowNull   : false,
        values      : Helpers.iterateEnum(DischargePointType),
        defaultValue: DischargePointType.SIMPLE
      },
      createdAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      },
      updatedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE,
      },
      deletedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      }
    });
  },
  down: (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.dropTable("states");
  }
};

Need to change migrations & seeders path in .sequelizerc to point to dist folder

var path = require('path')

module.exports = {
  'config':          path.resolve('dist', 'config', 'database.js'),
  'migrations-path': path.resolve('dist', 'migrations'),
  'models-path':     path.resolve('src', 'models'),
  'seeders-path':    path.resolve('dist', 'seeders'),
  'debug': true,
}

So before running migration first you need to run npm run build or tsc, whatever your configuration is to build the code.

But yes native support will be the best solution.

kunwar97 avatar May 11 '21 14:05 kunwar97

Is this happening? It would be nice to solve this one: https://github.com/sequelize/cli/pull/915#issuecomment-690777113

andrejleitner avatar Sep 22 '21 12:09 andrejleitner

I'm still waiting

pirumu avatar Oct 04 '21 10:10 pirumu

It's almost 2022. How's the overhaul going?

1valdis avatar Dec 12 '21 20:12 1valdis

Unfortunately @papb did not have the time anymore for the overhaul. Therefore I will close this issue

WikiRik avatar Dec 13 '21 10:12 WikiRik

You can currently use typescript in migrations & seeders. But you need to first compile it to javascript. Here is the example

import { QueryInterface, SequelizeStatic } from "sequelize";
import { Helpers } from "../util/helpers.util";
import { DischargePointType } from "../enums/discharge-point-type.enum";

export = {
  up  : (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.createTable("states", {
      id                                : {
        allowNull    : false,
        autoIncrement: true,
        primaryKey   : true,
        type         : Sequelize.BIGINT
      },
      title                             : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      state_code                        : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      discharge_point_type              : {
        type        : Sequelize.ENUM,
        allowNull   : false,
        values      : Helpers.iterateEnum(DischargePointType),
        defaultValue: DischargePointType.SIMPLE
      },
      createdAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      },
      updatedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE,
      },
      deletedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      }
    });
  },
  down: (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.dropTable("states");
  }
};

Need to change migrations & seeders path in .sequelizerc to point to dist folder

var path = require('path')

module.exports = {
  'config':          path.resolve('dist', 'config', 'database.js'),
  'migrations-path': path.resolve('dist', 'migrations'),
  'models-path':     path.resolve('src', 'models'),
  'seeders-path':    path.resolve('dist', 'seeders'),
  'debug': true,
}

So before running migration first you need to run npm run build or tsc, whatever your configuration is to build the code.

But yes native support will be the best solution.

Sir, how did you generate your migrations in TS?

AnthonyLzq avatar Jul 23 '22 04:07 AnthonyLzq

@AnthonyLzq The sequelize-cli migration generate command creates migration in javascript, so you can't use that.

You can either create it manually or you can write a custom script to generate typescript migration.

kunwar97 avatar Jul 25 '22 09:07 kunwar97

You can currently use typescript in migrations & seeders. But you need to first compile it to javascript. Here is the example

import { QueryInterface, SequelizeStatic } from "sequelize";
import { Helpers } from "../util/helpers.util";
import { DischargePointType } from "../enums/discharge-point-type.enum";

export = {
  up  : (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.createTable("states", {
      id                                : {
        allowNull    : false,
        autoIncrement: true,
        primaryKey   : true,
        type         : Sequelize.BIGINT
      },
      title                             : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      state_code                        : {
        type     : Sequelize.STRING,
        allowNull: false,
      },
      discharge_point_type              : {
        type        : Sequelize.ENUM,
        allowNull   : false,
        values      : Helpers.iterateEnum(DischargePointType),
        defaultValue: DischargePointType.SIMPLE
      },
      createdAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      },
      updatedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE,
      },
      deletedAt                         : {
        allowNull: true,
        type     : Sequelize.DATE
      }
    });
  },
  down: (queryInterface: QueryInterface, Sequelize: SequelizeStatic) => {
    return queryInterface.dropTable("states");
  }
};

Need to change migrations & seeders path in .sequelizerc to point to dist folder

var path = require('path')

module.exports = {
  'config':          path.resolve('dist', 'config', 'database.js'),
  'migrations-path': path.resolve('dist', 'migrations'),
  'models-path':     path.resolve('src', 'models'),
  'seeders-path':    path.resolve('dist', 'seeders'),
  'debug': true,
}

So before running migration first you need to run npm run build or tsc, whatever your configuration is to build the code.

But yes native support will be the best solution.

For some reason this does not read dot env variables from transpiled js

slidenerd avatar Oct 28 '22 08:10 slidenerd