Edit Fork Copy module.export = function(){ this.mysql= require('mysql'); this.host=''; this.user=''; this.password=''; this.database=''; this.connected = function(host, user, password){ this.host=host; this.user=user; this.password=password; this.conn=this.mysql.createConnection({ host: this.host, user: this.user, password: this.password }); this.conn.connect(function(err){ if(err) throw err; console.log('Connected'); }) } this.databased= function(){ if(this.connected){ this.conn.query("Create database mydb", function(err, result){ if(err) throw err; console.log("DB created"); console.log(result); return result; }) } } this.crTable= function(){ if(this.connected){ this.conn.query("use mydb"); this.conn.query("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))", function(err, result){ if (err) throw err; console.log("Table created"); }) } } this.inserted= function(){ if(this.connected){ this.conn.query("use mydb"); var values = [ ['John', 'Hà Nam'], ['Peter', 'Hà Nam'], ['Amy', 'Hà Nam'], ]; this.conn.query("Insert into customers (name, address) VALUES ?",[values], function(err, result){ if(err) throw err; console.log("Number of records inserted: " + result.affectedRows); }) } } this.selected= function(){ if(this.connected){ this.conn.query("use mydb"); this.conn.query("Select * from customers", function(err, result, fields){ if(err) throw err; return result; }) } } };