Vold
Vold

Reputation: 41

How to create ES module ('.mjs') migration files through sequelize-cli?

I'm writing PostgreSQL database migrations in my NodeJs project using sequelize-cli and trying to follow ES module syntax in my files.

Unfortunately, I can't find how to enable creation and execution of migrations files of type '.mjs' through sequelize-cli.

I want to configure sequelize such that when I create a migration through command line, the generated migration file should be of type mjs.

I tried adding migrationFileExtension: 'mjs' in config file as suggested by ChatGPT, but it's not working.

FYI, I'm writing migrations in Raw PostgreSQL (only using sequelize's migration functionality).

'use strict';

/** @type {import('sequelize-cli').Migration} */

console.log('in migration file');

export async function up(queryInterface, Sequelize) {
  console.log('up function');
  const createTableQuery = `
    CREATE TABLE users (
      id SERIAL PRIMARY KEY,
      username VARCHAR(50) NOT NULL UNIQUE,
      email VARCHAR(100) NOT NULL UNIQUE,
      password VARCHAR(255) NOT NULL,
      first_name VARCHAR(50) NOT NULL,
      last_name VARCHAR(50) NOT NULL,
      date_of_birth DATE,
      profile_picture_url VARCHAR(255),
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    );
  `;
  
  await queryInterface.sequelize.query(createTableQuery);
}

export async function down(queryInterface, Sequelize) {
  const dropTableQuery = 'DROP TABLE IF EXISTS users;';
  
  await queryInterface.sequelize.query(dropTableQuery);
}

Upvotes: 0

Views: 306

Answers (0)

Related Questions