@studiosonrai/nestjs-migrations
v1.1.0
Published
SQL Server migrations module for NestJS applications
Readme
@studiosonrai/nestjs-migrations
SQL Server migrations module for NestJS. Runs TypeScript migrations on startup or via CLI.
Installation
npm install @studiosonrai/nestjs-migrationsUsage
import { MigrationModule } from '@studiosonrai/nestjs-migrations';
@Module({
imports: [
MigrationModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
migrationsDir: path.join(__dirname, '../migrations'),
autoRun: config.get('AUTO_RUN_MIGRATIONS') === 'true',
}),
}),
],
})
export class AppModule {}CLI
npx nestjs-migrations --dir ./migrations --connection-string "your-connection-string"Migration Files
Name format: 001_description.sql, 002_description.sql, etc.
Place SQL files in your migrations directory. They will be executed in alphabetical order.
Hash Integrity Checking
Migration files are tracked with SHA256 hashes. If a migration file is modified after being applied, the migration runner will fail with a HashMismatchError. This prevents accidental changes to already-applied migrations.
import { HashMismatchError } from '@studiosonrai/nestjs-migrations';
try {
await migrationService.runMigrations();
} catch (error) {
if (error instanceof HashMismatchError) {
console.error(`Migration ${error.migrationName} was modified!`);
}
}