π₯ Drizzle-like migrations for Sequelize - Automatically generate migrations from your Sequelize models, just like Drizzle ORM but for Sequelize!
- π¦ Models as Source of Truth - Define your schema using Sequelize models
- π Automatic Migration Generation - Compare models vs database and generate migrations
- π CLI Commands - Simple commands for generate, migrate, and rollback
- π TypeScript Support - Full TypeScript support with type definitions
- π― Powered by Umzug - Built on the reliable Umzug migration framework
- π οΈ Production Ready - Handles complex schema changes, foreign keys, and more
- π Directory-based Loading - Auto-discover models from directories
- π Dry-run Mode - Preview changes before generating migrations
- π² Smart Naming - Auto-generates migration names when not provided
npm install sequelize-schema-sync
# or
yarn add sequelize-schema-sync
- Initialize configuration:
npx sequelize-schema-sync init
- Configure your models in
schema-sync.config.ts
:
import { Sequelize } from 'sequelize';
import { SchemaSyncConfig } from 'sequelize-schema-sync';
import { User } from './models/User';
import { Post } from './models/Post';
const sequelize = new Sequelize({
dialect: 'postgres', // or 'mysql', 'sqlite', etc.
host: 'localhost',
database: 'myapp',
username: 'user',
password: 'password',
});
const config: SchemaSyncConfig = {
sequelize,
// Option 1: Provide models array directly
models: [User, Post],
// Option 2: Auto-discover from directory (recommended)
// modelsPath: './models',
migrationsPath: './migrations',
};
export default config;
- Generate your first migration:
npx sequelize-schema-sync generate
- Apply migrations:
npx sequelize-schema-sync migrate
Generate a new migration based on model changes
npx sequelize-schema-sync generate
npx sequelize-schema-sync generate --name "add_user_avatar"
npx sequelize-schema-sync generate --dry-run
Options:
-n, --name <name>
- Custom migration name--dry-run
- Preview changes without generating files
Run all pending migrations
npx sequelize-schema-sync migrate
Rollback the last applied migration
npx sequelize-schema-sync rollback
Show migration status
npx sequelize-schema-sync status
Initialize a new schema-sync.config.ts
file
npx sequelize-schema-sync init
- Define Models: Create your Sequelize models as usual
- Run Generate: The tool compares your models against the current database schema
- Automatic Diff: Detects changes like new tables, columns, type changes, etc.
- Generate Migration: Creates a timestamped migration file with
up
anddown
functions - Apply Changes: Use the migrate command to apply changes to your database
When you change your models, the tool generates migrations like this:
import { QueryInterface, DataTypes } from 'sequelize';
export async function up(queryInterface: QueryInterface): Promise<void> {
await queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
});
}
export async function down(queryInterface: QueryInterface): Promise<void> {
await queryInterface.dropTable('users');
}
const config: SchemaSyncConfig = {
sequelize,
models: [User, Post, Comment], // Import and list your models
migrationsPath: './migrations',
};
const config: SchemaSyncConfig = {
sequelize,
modelsPath: './models', // Auto-discover models from directory
migrationsPath: './migrations',
};
Model Factory Pattern:
// models/User.ts
export const UserModel = (sequelize: Sequelize) => {
const User = sequelize.define('User', {
email: DataTypes.STRING,
name: DataTypes.STRING,
});
return User;
};
Preview what changes will be made before generating migrations:
npx sequelize-schema-sync generate --dry-run
Example Output:
π Schema Analysis Results:
==================================================
Found 2 table(s) with changes:
π Table: users
Action: alter
β Will modify 2 column(s):
- email: change
- avatar: add
π Table: posts
Action: create
β Will create table with 5 columns: id, title, content, userId, createdAt
π‘ To generate the migration file, run without --dry-run flag
- β Create new tables
- β Drop tables
- β Add columns
- β Remove columns
- β Change column types
- β Add/remove primary keys
- β Add/remove auto-increment
- β Add/remove unique constraints
- β Change default values
- β Add/remove foreign keys
const config: SchemaSyncConfig = {
sequelize,
modelsPath: './models',
migrationsPath: './migrations',
// Optional: Custom config path
configPath: './custom-config.ts',
};
Configure global options for all your models using Sequelize's define
option:
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './database.sqlite',
// Global define options for all models
define: {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
// Use snake_case for all auto-generated fields (timestamps, foreign keys, etc.)
underscored: true,
// Prevent table name pluralization
freezeTableName: true,
timestamps: true,
},
});
For MySQL and MariaDB, set charset and collation in both define
and dialectOptions
:
const sequelize = new Sequelize({
dialect: 'mysql',
host: 'localhost',
database: 'myapp',
username: 'user',
password: 'password',
// MySQL-specific options
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
},
// Global define options
define: {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
// Use snake_case for all auto-generated fields (timestamps, foreign keys, etc.)
underscored: true,
freezeTableName: true,
timestamps: true,
},
});
Option | Description | Example |
---|---|---|
charset |
Character set for tables | 'utf8mb4' |
collate |
Collation for string comparisons | 'utf8mb4_general_ci' |
underscored |
Use snake_case for all auto-generated fields | true β createdAt becomes created_at , userId becomes user_id |
freezeTableName |
Prevent table name pluralization | true β User table stays User , not Users |
timestamps |
Add createdAt/updatedAt automatically | true |
Individual models can override global define options:
// models/User.ts
export const UserModel = (sequelize: Sequelize) => {
return User.init({
email: DataTypes.STRING,
firstName: DataTypes.STRING, // Will be first_name in DB with underscored: true
}, {
sequelize,
modelName: 'User',
// Global options apply, but you can override:
// freezeTableName: false, // Would override global setting
// underscored: false, // Would override global setting
});
};
Check out the example/
directory for complete working examples:
- Directory-based model loading
- Factory function patterns
- TypeScript migration generation
- Migration execution and rollback
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built on top of Umzug for migration management
- Inspired by Drizzle ORM migration approach
- Powered by Sequelize