Sequelize TypeScript Migration LTS

3.2.5 · active · verified Sun Apr 19

sequelize-typescript-migration-lts is a migration tool designed for Sequelize ORM users who leverage TypeScript for defining their database models. It automates the generation of database migration files by detecting schema changes based on the current state of Sequelize TypeScript models (which use decorators like `@Table` and `@Column`). The package then creates `up` and `down` functions for these changes, eliminating the need for manual migration script writing, similar to Django's `makemigration` feature. The current stable version is 3.2.5, with recent updates focusing on bug fixes and supporting features like snake-case column names. It's important to note that this tool is specifically built for `sequelize-typescript` models and does not support plain `sequelize` model definitions. The project is an LTS (Long-Term Support) fork, integrating improvements from various community forks of the original `sequelize-auto-migrations` project.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to programmatically generate a new database migration file using `SequelizeTypescriptMigration.makeMigration`. It sets up a Sequelize instance, specifies an output directory for the migration, and names the migration file. This code should be executed after defining your Sequelize TypeScript models.

import { join } from 'path';
import { Sequelize } from "sequelize-typescript";
import { SequelizeTypescriptMigration, MakeMigrationOptions } from "sequelize-typescript-migration-lts";

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: './database.sqlite',
  models: [join(__dirname, 'models')], // Ensure your models are loaded
  logging: false,
});

// Define a sample model in models/CarBrand.ts
// import { Table, Column, Model, DataType, Default } from 'sequelize-typescript';
// @Table
// export class CarBrand extends Model<CarBrand> {
//   @Column
//   name: string;
//   @Default(true)
//   @Column(DataType.BOOLEAN)
//   isCertified: boolean;
// }

async function runMigration() {
  try {
    const options: MakeMigrationOptions = {
      outDir: join(__dirname, './migrations'),
      migrationName: 'initial-schema-setup',
      preview: false,
      isTs: true, // Generate TypeScript migration files
    };
    await SequelizeTypescriptMigration.makeMigration(sequelize, options);
    console.log('Migration generated successfully. Run `npx sequelize-cli db:migrate` to apply.');
  } catch (error) {
    console.error('Error generating migration:', error);
  }
}

// In a real application, you'd integrate this with your CLI or startup process.
// For demonstration, we directly call it.
// Make sure your Sequelize instance is initialized and models are defined before calling this.
// For a quick test, you might call `runMigration()` after defining models.
// e.g., runMigration();

view raw JSON →