No description
Find a file
QuinnFTW eed6ef4c9f
Some checks failed
Build and Publish / build-and-publish (push) Failing after 31s
deps-upgrade(deps): ⬆️ Update dependencies to latest stable versions for bug fixes and performance improvements
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-06-10 04:15:14 -07:00
.forgejo/workflows chore: 🔧 Update files 2026-01-15 06:57:45 -08:00
.githooks chore: configure GitLab CI/CD with workspace protocol 2025-12-28 03:33:32 -08:00
bin fix(@typeorm/migrations): 🛠 resolve missing semicolons in TypeScript files 2026-01-04 20:45:40 -08:00
src fix(@typeorm/migrations): 🛠 resolve missing semicolons in TypeScript files 2026-01-04 20:45:40 -08:00
.gitignore chore(gitignore): Add missing patterns 2026-01-21 13:26:42 -08:00
eslint.config.js fix(@typeorm/migrations): 🛠 resolve missing semicolons in TypeScript files 2026-01-04 20:45:40 -08:00
package.json deps-upgrade(deps): ⬆️ Update dependencies to latest stable versions for bug fixes and performance improvements 2026-06-10 04:15:14 -07:00
README.md chore: trigger CI publish 2026-01-30 15:48:53 -08:00
tsconfig.json chore(tsconfig): 🔧 Update TypeScript compiler options for better type safety 2026-01-21 13:27:01 -08:00
tsup.config.ts chore(build): Update TypeScript build config for performance optimizations 2026-01-21 15:31:33 -08:00

@lilith/typeorm-migrations

Migration utilities and CLI for TypeORM.

Features

  • Migration Runner: Run and revert migrations programmatically
  • Migration Generator: Generate migration files from templates
  • CLI Tool: Command-line interface for migrations
  • Status Tracking: Get pending and executed migrations

Installation

pnpm add @lilith/typeorm-migrations

Peer Dependencies

pnpm add typeorm

Quick Start

CLI Usage

# Run all pending migrations
npx typeorm-migrate run

# Revert last migration
npx typeorm-migrate revert

# Show pending migrations
npx typeorm-migrate pending

# Generate new migration
npx typeorm-migrate generate AddUserTable

Programmatic Usage

import { runMigrations, revertMigration } from '@lilith/typeorm-migrations';
import { dataSource } from './data-source';

// Run all pending migrations
const results = await runMigrations(dataSource);
console.log(`Ran ${results.length} migrations`);

// Revert last migration
await revertMigration(dataSource);

API Reference

runMigrations

Run all pending migrations:

import { runMigrations } from '@lilith/typeorm-migrations';

const results = await runMigrations(dataSource, {
  transaction: 'all', // 'all' | 'each' | 'none'
  fake: false,        // Mark as run without executing
});

for (const result of results) {
  console.log(`Ran: ${result.name} (${result.timestamp})`);
}

revertMigration

Revert the last executed migration:

import { revertMigration } from '@lilith/typeorm-migrations';

const result = await revertMigration(dataSource, {
  transaction: 'all',
  fake: false,
});

console.log(`Reverted: ${result.name}`);

getPendingMigrations

Get list of pending migrations:

import { getPendingMigrations } from '@lilith/typeorm-migrations';

const pending = await getPendingMigrations(dataSource);
console.log(`${pending.length} migrations pending`);

for (const migration of pending) {
  console.log(`- ${migration.name}`);
}

getExecutedMigrations

Get list of executed migrations:

import { getExecutedMigrations } from '@lilith/typeorm-migrations';

const executed = await getExecutedMigrations(dataSource);
console.log(`${executed.length} migrations executed`);

for (const migration of executed) {
  console.log(`- ${migration.name} (${migration.timestamp})`);
}

generateMigration

Generate a new migration file:

import { generateMigration } from '@lilith/typeorm-migrations';

await generateMigration({
  name: 'AddUserTable',
  directory: './src/migrations',
  template: 'default', // 'default' | 'empty'
});

// Creates: ./src/migrations/1234567890123-AddUserTable.ts

generateTimestamp

Generate a timestamp for migration naming:

import { generateTimestamp } from '@lilith/typeorm-migrations';

const timestamp = generateTimestamp();
// '1704067200000'

toClassName

Convert migration name to class name:

import { toClassName } from '@lilith/typeorm-migrations';

const className = toClassName('add-user-table');
// 'AddUserTable'

listMigrationFiles

List migration files in a directory:

import { listMigrationFiles } from '@lilith/typeorm-migrations';

const files = await listMigrationFiles('./src/migrations');
// ['1704067200000-CreateUsers.ts', '1704067201000-AddUserEmail.ts']

validateMigrationName

Validate a migration name:

import { validateMigrationName } from '@lilith/typeorm-migrations';

validateMigrationName('AddUserTable');    // OK
validateMigrationName('add user table');  // Throws error
validateMigrationName('123-invalid');     // Throws error

CLI Commands

typeorm-migrate run

Run pending migrations:

npx typeorm-migrate run [options]

Options:
  -c, --config <path>     Path to ormconfig/data-source file
  -t, --transaction <mode> Transaction mode: all|each|none (default: all)
  --fake                  Mark migrations as run without executing
  --dry-run               Show migrations that would run without executing

typeorm-migrate revert

Revert last migration:

npx typeorm-migrate revert [options]

Options:
  -c, --config <path>     Path to ormconfig/data-source file
  -t, --transaction <mode> Transaction mode: all|each|none (default: all)
  --fake                  Mark as reverted without executing

typeorm-migrate pending

Show pending migrations:

npx typeorm-migrate pending [options]

Options:
  -c, --config <path>  Path to ormconfig/data-source file

typeorm-migrate generate

Generate new migration:

npx typeorm-migrate generate <name> [options]

Arguments:
  name                  Migration name (PascalCase recommended)

Options:
  -d, --dir <path>      Output directory (default: ./src/migrations)
  -t, --template <type> Template type: default|empty (default: default)

Types

interface MigrationResult {
  name: string;
  timestamp: number;
  instance?: MigrationInterface;
}

interface RunMigrationsOptions {
  transaction?: 'all' | 'each' | 'none';
  fake?: boolean;
}

interface GenerateMigrationOptions {
  name: string;
  directory?: string;
  template?: 'default' | 'empty';
}

Migration Template

Generated migrations follow this template:

import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddUserTable1704067200000 implements MigrationInterface {
  name = 'AddUserTable1704067200000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add your migration code here
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Add your rollback code here
  }
}

License

MIT