Node.js MySQL SQL File Importer

5.0.26 · active · verified Wed Apr 22

mysql-import is a Node.js library designed to facilitate the programmatic import of `.sql` dump files into a MySQL database. Currently at version 5.0.26, the package provides a streamlined API for tasks such as database seeding, migration, or restoring backups. It differs from general ORMs or query builders by focusing solely on bulk SQL import operations. A key differentiator is its event-driven progress tracking (`onProgress`, `onDumpCompleted`), allowing developers to monitor long-running imports in real-time, which is crucial for large datasets. The library leverages Node.js streams and underlying MySQL client binaries to efficiently process SQL files of various sizes, making it a robust solution for environments requiring automated database provisioning or data synchronization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the Importer, set up progress tracking, and import a single SQL file.

const host = 'localhost';
const user = 'root';
const password = 'password';
const database = 'mydb';

const Importer = require('mysql-import');
const importer = new Importer({host, user, password, database});

importer.onProgress(progress=>{
  var percent = Math.floor(progress.bytes_processed / progress.total_bytes * 10000) / 100;
  console.log(`${percent}% Completed`);
});

importer.import('path/to/dump.sql').then(()=>{
  var files_imported = importer.getImported();
  console.log(`${files_imported.length} SQL file(s) imported.`);
}).catch(err=>{
  console.error(err);
});

view raw JSON →