How to send requests to other files?

Good evening, everyone. For a long time struggling with one bug which appears when you try to query in another js file. I'll show an example.

database.js
var mysql = require("mysql");

module.exports = {
 Pool : null,
 Connect : function(callback) {
 this.Pool = mysql.createConnection({
 host: 'localhost',
 user: 'root',
 password: 'admin',
 database: 'server'
})
 this.Pool.connect(function(err) {
 if(err) {
 console.log('[Database ERROR] ' + err.message);
 throw err;
 } else {
 console.log(`\x1b[92m[DATABASE]\x1b[0m Connection established.`);
}
})
}
}


main.js
require('./module.js');

const DB = require('./database.js');

DB.Connect(() => {
 DB.Pool.query("CREATE TABLE IF NOT EXISTS `mdb_players` (`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `Name` varchar(24) NOT NULL, `Money` int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;", (err) => {
 if (err) console.log('\x1b[91m[Database]\x1b[0m ' + err.message);
});
});


Now begins the next phase, which I can't understand. For example we have a file module.js. In this file I need to make a request, not re-connecting to the database, that is, not to use a DB.Connect(), and once to use DB.query().

module.js

const DB = require('./database.js')

DB.Pool.query("CREATE TABLE IF NOT EXISTS `mdb_players` (`UID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `Name` varchar(24) NOT NULL, `Money` int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;", (err) => {
 if (err) console.log('\x1b[91m[Database]\x1b[0m ' + err.message);
 });


It would be possible to use a DB.Connect() in other files, but then in the console will repeatedly display a message when a connection is established.
April 4th 20 at 13:24
0 answer

Find more questions by tags MySQLNode.js