Larvit Database Migration Tool

7.0.188 · active · verified Wed Apr 22

larvitdbmigration is a Node.js utility designed for managing database schema and data evolution through a versioned script approach. It supports both traditional relational databases like MariaDB (and MySQL) and NoSQL solutions such as Elasticsearch. Currently stable at version 7.0.188, the package appears to have an active release cadence, frequently publishing patches and minor features as indicated by the high patch number within the 7.x series. A key differentiator is its ability to handle migrations across diverse database types within a single framework. It utilizes simple JavaScript or SQL files for migration scripts, providing flexibility for complex logic or straightforward DDL statements. The tool automatically creates and updates a `db_version` table or index to track the current database state, ensuring migrations are applied only once and in the correct order.

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize and run database migrations for MariaDB using environment variables for sensitive credentials, including a basic logger and an example migration script.

import { DbMigration } from 'larvitdbmigration';
import Db from 'larvitdb'; // Assuming 'larvitdb' is installed for MariaDB/MySQL

const log = {
  silly: (...args: any[]) => console.log('[SILLY]', ...args),
  debug: (...args: any[]) => console.debug('[DEBUG]', ...args),
  verbose: (...args: any[]) => console.log('[VERBOSE]', ...args),
  info: (...args: any[]) => console.info('[INFO]', ...args),
  warn: (...args: any[]) => console.warn('[WARN]', ...args),
  error: (...args: any[]) => console.error('[ERROR]', ...args),
};

async function runMigrations() {
  // Ensure environment variables are set or provide fallbacks
  const dbConfig = {
    host: process.env.DB_HOST ?? '127.0.0.1',
    user: process.env.DB_USER ?? 'root',
    password: process.env.DB_PASSWORD ?? 'password',
    database: process.env.DB_NAME ?? 'test_db',
  };

  const dbDriver = new Db(dbConfig);

  const dbMigration = new DbMigration({
    dbType: 'mariadb',
    dbDriver,
    tableName: 'db_version', // Optional, default is 'db_version'
    migrationScriptPath: './dbmigration_scripts', // Ensure this directory exists with migration files
    log, // Pass a logger instance
  });

  try {
    console.log('Starting database migrations...');
    await dbMigration.run();
    console.log('Database migrations completed successfully!');
  } catch (err) {
    console.error('Database migration failed:', err);
    process.exit(1);
  }
}

// To run this example, create a './dbmigration_scripts/1.js' file like this:
/*
  // ./dbmigration_scripts/1.js
  import type { IMigrationScriptOptions } from 'larvitdbmigration';

  export = async function (options: IMigrationScriptOptions) {
    const { db } = options;
    await db.query('CREATE TABLE IF NOT EXISTS test_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255));');
    options.log.info('Created test_table');
  };
*/

runMigrations();

view raw JSON →