{"library":"mysql2-import","title":"MySQL2 SQL File Importer","description":"mysql2-import, currently at version 5.0.22, is a Node.js library specifically designed for importing `.sql` dump files into a MySQL database. It's an adaptation of the `mysql-import` project, updated to seamlessly integrate with the `mysql2` driver. This integration leverages `mysql2`'s enhanced performance and modern features, including prepared statements, which are crucial for secure and efficient database interactions. The library provides an intuitive API for connecting to a database, specifying multiple `.sql` files or directories containing dumps, and executing their commands asynchronously. A key differentiator introduced in version 5.0 is its robust event system, featuring `onProgress` and `onDumpCompleted` callbacks. These allow developers to monitor the import process in real-time, receiving updates on bytes processed and file completion status, making it highly suitable for handling large SQL datasets. While no explicit release cadence is documented, it generally aligns with the versioning of its upstream `mysql-import` project, delivering stable and feature-rich releases.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mysql2-import"],"cli":null},"imports":["import Importer from 'mysql2-import';","const Importer = require('mysql2-import');","importer.import('path/to/dump.sql').then(() => { /* ... */ });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Importer from 'mysql2-import';\nimport path from 'path';\n\n// Ensure .env or similar is set up for production\nconst host = process.env.MYSQL_HOST ?? 'localhost';\nconst user = process.env.MYSQL_USER ?? 'root';\nconst password = process.env.MYSQL_PASSWORD ?? 'password';\nconst database = process.env.MYSQL_DATABASE ?? 'mydb';\n\nconst importer = new Importer({ host, user, password, database });\n\n// Create a dummy SQL file for demonstration if it doesn't exist\nimport fs from 'fs';\nconst dummySqlFilePath = path.join(process.cwd(), 'dummy_data.sql');\nif (!fs.existsSync(dummySqlFilePath)) {\n  fs.writeFileSync(dummySqlFilePath, `\nDROP TABLE IF EXISTS \\`users\\`;\nCREATE TABLE \\`users\\` (\n  \\`id\\` INT AUTO_INCREMENT PRIMARY KEY,\n  \\`name\\` VARCHAR(255) NOT NULL\n);\nINSERT INTO \\`users\\` (name) VALUES ('Alice'), ('Bob');\n`);\n  console.log('Created dummy_data.sql for quickstart.');\n}\n\nimporter.onProgress(progress => {\n  const percent = Math.floor(progress.bytes_processed / progress.total_bytes * 10000) / 100;\n  console.log(`File ${progress.file_no} of ${progress.total_files}: ${percent}% Completed (${progress.file_path})`);\n});\n\nimporter.onDumpCompleted(data => {\n  if (data.error) {\n    console.error(`Error completing dump for ${data.file_path}:`, data.error);\n  } else {\n    console.log(`Dump completed for ${data.file_path}`);\n  }\n});\n\nimporter.import(dummySqlFilePath).then(() => {\n  const files_imported = importer.getImported();\n  console.log(`${files_imported.length} SQL file(s) imported successfully.`);\n}).catch(err => {\n  console.error('An error occurred during import:', err);\n}).finally(() => {\n  importer.disconnect();\n  console.log('Importer disconnected.');\n  // Clean up dummy file if it was created\n  if (fs.existsSync(dummySqlFilePath)) {\n    fs.unlinkSync(dummySqlFilePath);\n    console.log('Removed dummy_data.sql.');\n  }\n});","lang":"javascript","description":"This quickstart demonstrates how to initialize the Importer, set up progress and completion callbacks, import a SQL file, and handle potential errors, including creating a dummy SQL file for a runnable example.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}