Registry / database / mysql-migrator

mysql-migrator

JSON →
library2.0.0jsnpmunverified

mysql-migrator is an npm package for managing MySQL/MariaDB database migrations and data seeding in Node.js projects, currently at version 2.0.0. It provides a simple CLI-like interface to create and run migration and seeding files with built-in rollback support. Key differentiators include support for both ES6 and CommonJS module systems, a straightforward API using a Migrator class, and built-in table creation/drop helpers via the tbl object. The package is actively maintained and integrates directly with npm scripts for ease of use.

npm install mysql-migrator
INSTALL
IMPORT
SIG · MYSQL-MIGRATOR
M
mysql-migrator
databasejavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Migrator
import { Migrator } from 'mysql-migrator'
const Migrator = require('mysql-migrator')
Named export in ESM; CommonJS uses destructured require.
Output
import { Output } from 'mysql-migrator'
const Output = require('mysql-migrator')
Utility function for logging migration results.
tbl
module.exports = { up: async (tbl) => { ... } }
Using tbl without passing it as parameter to up/rollback
tbl is an object passed to migration functions for table operations.
query
module.exports = { up: async (query) => { ... } }
Using query without passing it to seeding functions
query is a function passed to seeding functions for raw SQL queries.

Sets up a migrator instance, creates a migration, runs it up, and rolls back.

// Install: npm install mysql-migrator // Initialize migrator.js: import { Migrator, Output } from 'mysql-migrator'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const dbConfig = { host: 'localhost', user: process.env.DB_USER ?? 'root', port: 3306, password: process.env.DB_PASSWORD ?? '', database: process.env.DB_NAME ?? 'mydb' }; const migrationsPath = __dirname + '/migrations'; const migrator = new Migrator(dbConfig, migrationsPath); const result = await migrator.init(); Output(result); process.exit(); // Create migration: node migrator.js migration:create create_table_user // Create migration file: exports.up = async (tbl) => { await tbl.create('users', { id: 'int NOT NULL AUTO_INCREMENT', name: 'varchar(255) NOT NULL' }); }; // Run up: node migrator.js migration:up // Rollback: node migrator.js migration:rollback
Debug
Known issues
gotchaMigration and seeding files must be CommonJS (module.exports) even if the app is ESM.
fix
Use module.exports = { up: async (tbl) => { ... } } in migration files.
affects: >=2.0.0
breakingVersion 2.0.0 changed the API: Migrator constructor now requires a config object instead of separate parameters.
fix
Update to new Migrator({ host, user, password, database }, migrationsPath) syntax.
affects: 2.0.0
deprecatedThe old migration file format using exports.up is deprecated in favor of module.exports.
fix
Change exports.up to module.exports = { up: ... }
affects: >=1.0.0 <2.0.0
gotchaRollback will only revert the most recent batch of migrations, not individual ones.
fix
Run rollback multiple times to revert multiple batches.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'mysql-migrator'
Package not installed or import path incorrect
fix
Run 'npm install mysql-migrator' and ensure import statements match the module system.
Migrator is not a constructor
Using require incorrectly for ESM build
fix
Use import { Migrator } from 'mysql-migrator' for ESM or const { Migrator } = require('mysql-migrator') for CJS.
Cannot read properties of undefined (reading 'create')
Migration file uses tbl without receiving it as parameter
fix
Ensure up/rollback function signature is async (tbl) => { ... }
Access denied for user 'user'@'host'
Database credentials are incorrect or insufficient privileges
fix
Check DB_USER, DB_PASSWORD, host, and port in dbConfig.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
mysql2requiredMySQL driver required for database connection
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-migrator — npm install mysql-migrator · libregistry