Node DB Migration

1.4.0 · active · verified Wed Apr 22

node-db-migration is a focused Node.js library designed for managing database schema evolution through simple SQL-based migration scripts. Currently at version 1.4.0, it offers robust support for popular relational databases including SQLite3, MySQL (or its modern fork `mysql2`), and PostgreSQL. Its core philosophy emphasizes using bare SQL files, allowing developers to maintain direct control over their database schemas without being tied to an ORM's migration DSL. The package operates by maintaining a dedicated `migrations` table within the database to track which scripts have been applied. It scans a specified directory for `.sql` files, enforcing a strict `YYYYMMDDHHmm-name.sql` naming convention to ensure chronological execution. Key features include sequential script execution, robust tracking of successful and failed migrations, and the ability to prevent further migrations upon failure until manual intervention. This helps ensure data consistency and provides a clear audit trail of schema changes. While its release cadence isn't rapid, it offers a stable and reliable solution for teams preferring a 'SQL-first' approach to database version control.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and run database migrations using `node-db-migration` with a PostgreSQL database. It connects to a database, initializes the `CommandsRunner` with a `PsqlDriver` and a directory for SQL migration scripts, then executes the `migrate` command to apply pending changes.

import { CommandsRunner, PsqlDriver } from 'node-db-migration';
import { Client } from 'pg';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function runMigrations() {
    const client = new Client({
        connectionString: process.env.DATABASE_URL ?? 'postgresql://postgres:@localhost:5432/test_db',
    });

    try {
        await client.connect();
        console.log('Connected to PostgreSQL database.');

        const migrations = new CommandsRunner({
            driver: new PsqlDriver(client),
            directoryWithScripts: path.join(__dirname, 'migrations'), // Ensure 'migrations' directory exists with .sql files
            migrationTable: 'my_app_migrations' // Optional: custom table name for migration tracking
        });

        // Example: Create a dummy migration directory and file for demonstration
        // In a real app, these would be pre-existing.
        const fs = await import('fs/promises');
        const migrationsDir = path.join(__dirname, 'migrations');
        await fs.mkdir(migrationsDir, { recursive: true });
        const migrationFileName = `${new Date().toISOString().replace(/[-:.]/g, '').substring(0, 14)}-create-users-table.sql`;
        await fs.writeFile(path.join(migrationsDir, migrationFileName),
            'CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE);');

        console.log('Running `migrate` command...');
        await migrations.run('migrate');
        console.log('Migrations completed successfully.');

        // You can run other commands like 'list', 'getFailed', 'resolve'
        // console.log('\nListing unapplied migrations:');
        // await migrations.run('list');

    } catch (error) {
        console.error('Migration failed:', error);
        process.exit(1);
    } finally {
        await client.end();
        console.log('Database connection closed.');
    }
}

runMigrations();

view raw JSON →