Skip to content

Instantly share code, notes, and snippets.

@lazzyms
Created July 24, 2018 13:05
Show Gist options
  • Save lazzyms/b8452da9fdb2570e186720919bbcee47 to your computer and use it in GitHub Desktop.
Save lazzyms/b8452da9fdb2570e186720919bbcee47 to your computer and use it in GitHub Desktop.
Node.js code to connect to mysql
var http = require("http");
var mysql = require("mysql");
var fs = require("fs");
var jsonContent = null;
const prompt = require('electron-prompt');
var userdata = {
"host": '',
"port": '',
"username": '',
"password": '',
"database": ''
};
onLoad();
function onLoad() {
// console.log("main window : " + mainWindow);
var connection = null;
try {
//This will prompt user for Database details one time.
var contents = fs.readFileSync(process.env.APPDATA + "/Mcare/sql-config.json");
jsonContent = JSON.parse(contents);
connection = mysql.createConnection({
host: jsonContent.host,
port: jsonContent.port,
user: jsonContent.username,
database: jsonContent.database,
password: jsonContent.password,
debug: false
});
} catch (ex) {
var dummy = JSON.stringify(userdata);
fs.writeFileSync(process.env.APPDATA + "/AngularElectronMySQL/sql-config.json", dummy);
onLoad();
}
connection.connect(function (err) {
if (err) {
console.error("error connecting: " + err.stack);
prompt({
title: 'Sql Config',
label: 'Host:',
value: jsonContent.host,
inputAttrs: {
type: 'text'
},
type: 'input',
})
.then((h) => {
if (h) {
userdata.host = h;
prompt({
title: 'Sql Config',
label: 'Port:',
value: jsonContent.port,
inputAttrs: {
type: 'text'
},
type: 'input',
})
.then((p) => {
if (p) {
userdata.port = p;
prompt({
title: 'Sql Config',
label: 'Username:',
value: jsonContent.username,
inputAttrs: {
type: 'text'
},
type: 'input',
})
.then((u) => {
if (u) {
userdata.username = u;
prompt({
title: 'Sql Config',
label: 'Password:',
value: jsonContent.password,
inputAttrs: {
type: 'password'
},
type: 'input',
})
.then((w) => {
userdata.password = w;
prompt({
title: 'Sql Config',
label: 'Database:',
value: jsonContent.database,
inputAttrs: {
type: 'text'
},
type: 'input',
})
.then((d) => {
if (d) {
userdata.database = d;
var json = JSON.stringify(userdata);
fs.writeFileSync(process.env.APPDATA + "/Mcare/sql-config.json", json);
onLoad();
}
})
.catch(console.error);
})
.catch(console.error);
}
})
.catch(console.error);
}
})
.catch(console.error);
}
})
.catch(console.error);
} else {
connection.end();
console.log("connected as id " + connection.threadId);
afterLoad();
}
});
}
function afterLoad() {
http
.createServer(function (req, res) {
var taskQueue = [];
var token = 0;
function checkExecuteFinished(queue) {
if (queue.length == 0) {
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify(data, null, 4));
res.end();
}
}
function readDataFromMySql(query, fn) {
taskQueue.push(token++);
var connection = mysql.createConnection({
host: jsonContent.host,
port: jsonContent.port,
user: jsonContent.username,
database: jsonContent.database,
password: jsonContent.password,
debug: false
});
connection.connect(function (err) {
if (err) {
console.error("error connecting: " + err.stack);
return;
}
console.log("connected as id " + connection.threadId);
});
connection.query(query, function (error, results, fields) {
if (error) throw error;
fn(results);
connection.end();
});
}
var data = [];
function cleanData() {
data = {
id: "",
name: ""
};
}
function getData(results) {
for (var i = 0; i < results.length; i++) {
cleanData();
data.id = results[i].Id;
data.name = results[i].Name;
}
}
taskQueue.shift();
checkExecuteFinished(taskQueue);
}
readDataFromMySql("select * from table", getData);
// console.log(JSON.stringify(location));
})
.listen(1024);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment