Sequelize CLI in 2021
Hello everyone!
I will be doing a large overhaul of the Sequelize CLI this month. Stay tuned!
Please, add typescript support of migrations and seeds
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.
Typescript implementation would be awesome!
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.
Is this happening? It would be nice to solve this one: https://github.com/sequelize/cli/pull/915#issuecomment-690777113
I'm still waiting
It's almost 2022. How's the overhaul going?
Unfortunately @papb did not have the time anymore for the overhaul. Therefore I will close this issue
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 buildortsc, 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 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.
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 buildortsc, 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