javascriptmysqlnode.jsmysqlimport

NPM MysqlDump and Mysql-Import , how to add option of drop table if exist , Node.js


I'm using the mysqldump library, and mysql-import. I need to do a restore of my MySQL database, but at the time of doing it, it tells me that you cannot add duplicate files, therefore I manually put DROP TABLE IF EXIST, and it worked and overwritten the database, according to In the Mysqldump documentation there is a way to add the DROP TABLE by default, but I really don't know how to do it, can someone help me?

var mysqldump = require('mysqldump');
const controller = {};


controller.backupDatabase = function(req, res, next) {
  if(mysqldump){
      mysqldump({
          connection: {
              host: 'localhost',
              user: 'root',
              password: '',
              database: 'decoracionesalves',
          },
          dumpToFile: './DecoracionesAlves.sql',
      });
    
  }else{
      //Hacer algo aqui.
  }

};

module.exports = controller;

Solution

  • You can set dropIfExists to true on the schema dump table option.

    mysqldump({
      connection: {
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'decoracionesalves',
      },
      dump: { schema: { table: { dropIfExist: true } } },
      dumpToFile: './DecoracionesAlves.sql',
    });
    

    See API docs for SchemaDumpOptions