'Node JS Sequelize - Can it generate migrations from models?

I am new to node js and sequelize in general having come from a .net core background where we use EFCore.

The way I am used to generating migrations is basically create a class file containing all the attributes and then I reference it in a DbContext file and then run a command in the cli to generate a migration file.

Is it possible to do that with sequelize?

C# example:

public class UserAccess
{
    [Key]
    public string Id { get; set; }
    public string Device { get; set; }
    public string IP { get; set; }
    public string UserId { get; set; }
    public virtual User User { get; set; }
    public string SessionToken { get; set; }
    public string RefreshToken { get; set; }
    public string TenantId { get; set; }
    public DateTime Expiry { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

Then add public DbSet UserAccess { get; set; } to a DbContext file

Then Add-Migration MigrationName -Context ContextName -OutputDir Migrations\MigrationFolder

Node example that I am stuck at:

Connect.js

const sequelize = new Sequelize(
    'express_project',
    'root',
    '',
    {
        host: "localhost",
        dialect: 'mysql',
        define:{
            freezeTableName: true
        }
    }
);

User.js

const {Sequelize, DataTypes} = require('sequelize');
const uuid = require('uuid');
const sequelizeCredentials = require('../database/Connect');

const User = sequelizeCredentials.sequelize.define('User',{
    Id: {type:Sequelize.UUIDV4, defaultValue: Sequelize.UUIDV4, allowNull:false, primaryKey:true},
    Name: {type:DataTypes.STRING, allowNull:false},
    UserName: {type:DataTypes.STRING, allowNull:false},
    Email: {type:DataTypes.STRING, allowNull:false},
    Password: {type:DataTypes.STRING, allowNull:false},
    PhoneNumber: {type:DataTypes.STRING, allowNull:false},
    MobileNumber: {type:DataTypes.STRING, allowNull:false},
    DateOfBirth: {type:DataTypes.DATE},
    LockoutEnabled: {type:DataTypes.BOOLEAN},
    LockoutEnd: {type:DataTypes.DATE},
},{freezeTableName: true});


module.exports = { User };
global.sequelize = { User };

I tried typing in npx sequelize migration:create --name initial_migration but that only generated an empty migration file.

How do I create a migration from this User.js file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source