nodejs > Express > SQL - javascript

I am trying to INSERT INTO my SQL Server DB Table using nodejs (Express).
Everything I have tried will not work.
In my app.js I have the below which executes correctly and connects, however where i am struggling is, how do i get data inserted in to my DB table.
app.js
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var async = require('async');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var sql = require('mssql');
var index = require('./routes/index');
var users = require('./routes/users');
var homepage = require('./routes/homepage');
var app = express();
////// DB CONF //////
var config = {
userName: 'xxxxxxxxxxxxxx',
password: 'xxxxxxx',
server: 'xxxxxxxxxxx',
options: {
database: 'xxxxxxxxxxx'
}
}
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log(err);
} else {
console.log('Connected');
}
});

You already included mysql library. now you have to create mysql connection. Like:
const writeDB = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '********',
database: 'dbName'
});
after creating connection run query like:
let squerySet = "INSERT INTO weekDays (`id`, `date`, `day`) values ('1','2018-03-03', 'Monday');"
writeDB.query(squerySet,(err, result) => {
if(err){
return reject(err)
}
else {
resolve("carrierbillreceipt Table COPY DONE .....")
}
})
Because writeDB.query will return a promise then you have to handle this by creating this in promise. Like:
const insertDays = () => {
return new Promise((resolve, reject) => {
let squerySet = "INSERT INTO weekDays (`id`, `date`, `day`) values ('1','2018-03-03', 'Monday');"
writeDB.query(squerySet,(err, result) => {
if(err){
return reject(err)
}
else {
resolve("insertDays Insert DONE .....")
}
})
})
}
you can also insert an JSON object. Like:
let data = { "id": '1', "date":'2018-03-03', "day": 'Monday'}
writeDB.query(`INSERT INTO weekDays SET ?;`,[data],(err, result) => {
if(err){
return reject(err)
}
else {
resolve("insertDays Insert DONE .....")
}
})

Related

Return Json with Nodejs, datas of SQL SERVER ( (express y tedious)

I am trying to transform the data that I obtain with my query to the SQL database in a json to be sent through a query in my REST API.
const express = require('express');
const Request = require('tedious').Request;
const app = express(); // creo mi app
const db = require('./db'); // conexion a la db
app.get('/series', (req, res) => {
const request = new Request(`SELECT VALOR1,VALOR2 FROM DATABASE`, (err, rowCount, rows) => {
if (err) {
console.error(err.message);
} else {
console.log(`${rowCount} row(s) returned`); // APARECE VALOR 63
};
jsonArray = [];
rows.forEach(function(columns) {
var rowObject = {};
columns.forEach(function(column) {
rowObject[column.metadata.colName] = column.value;
});
jsonArray.push(rowObject);
});
res.json(jsonArray);
});
db.execSql(request);
});
But the result is [] (without any value inside), when I make the Get request (with potman and in the browser). I have tried to send with the res.send () function and the result is the same.
Where can the error be?
Thanks in advance :)
PD: code from my file db.js
const {Connection} = require("tedious");
// Create connection to database
const config = {
authentication: {
options: {
userName: "users", // update me
password: "users" // update me
},
type: "default"
},
server: "srvdesarrollo", // update me
options: {
database: "users", //update me
encrypt: false
}
};
const connection = new Connection(config);
module.exports = connection;
I have solved it !! what I did was put the res.json (jsonArray); in the first else where it shows the total of rows

Agenad job is not getting start again after nodejs (express) server restart

I am using node agenda while scheduling I am able to successfully save job and its running fine. But while restarting the server the previous jobs are not getting start again. Not sure why, I tried few solutions found online but unable to make it work.
Can anyone help me with this.
I am using Nodemon with node express.
I am creating schedule using API calls
Below is app.js file
'use strict';
require('dotenv').config();
const express = require('express');
const { initialize_mongodb_database_connection } = require('./helpers/mongodb_database');
const bodyParser = require('body-parser');
const Agenda = require('agenda');
const app = express();
let logger = require('logger');
let chalk = require('chalk');
let moment = require('moment');
let mongoose = require('mongoose');
const agenda = new Agenda({
db: {address: process.env.MONGODB_URI, collection: 'scheduled_reports'},
processEvery: '30 seconds'
});
app.use(bodyParser.json());
//
// Parse application/x-www-form-urlencoded
//
app.use(bodyParser.urlencoded({extended: false}));
app.use(require('./routes/v1/schedule_report/schedule_report_routes'));
initialize_mongodb_database_connection();
sequr_app(app, [dummy_routes], {
staticDir: true,
});
let gracefulExit = function() {
if (mongoose.connection.readyState === 0) {
return process.exit(0);
}
mongoose.connection.close(function() {
return agenda.stop(function() {
logger.info({});
logger.info(chalk.bold("---------------------[ Server stopped at %s Uptime: %s ]---------------------------"), moment().format("YYYY-MM-DD HH:mm:ss.SSS"), moment.duration(process.uptime() * 1000).humanize());
return process.exit(0);
});
});
};
process.on("SIGINT", gracefulExit).on("SIGTERM", gracefulExit);
And this is my agenda file where I am routing API calls to create schedule
const Agenda = require('agenda');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: 'xx-xx#gmail.com',
pass: 'xx-xx-xx'
}
});
var mailOptions = {
from: 'example#example.com',
to: 'Example#gmail.com',
subject: 'Sending Email using Node.js',
text: 'Agenda Test'
};
const agenda = new Agenda({
db: {address: process.env.MONGODB_URI, collection: 'agendaJobs'},
processEvery: '30 seconds'
});
agenda.start();
agenda.defaultConcurrency(5);
const scheduleReport = async(report_data) => {
// HERE SCHEDULING/CREATING AGENDA SCHEDULE
agenda.on('start', job => {
console.log('-------------------------------------STARTED-----------------------------------------------');
console.log('Job %s starting', job.attrs.name);
console.log('-------------------------------------------------------------------------------------------');
});
const {
report_name,
priority
} = report_data;
agenda.define(report_name, {priority: priority, concurrency: 10}, (job, done) => {
const dateNow = new Date();
const data = job.attrs.data;
// The job.attrs.data is stored in our MongoDB collection so that it can be used to run the jobs.
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
//const date = new Date.now();
console.log('Email sent: ' + info.response + dateNow);
console.log(`Job ${job.attrs.name} finished`);
}
},done());
});
// HERE CREATING SCHEUDLE
await createWeeklySchedule(report_data);
agenda.on('complete', job => {
console.log('---------------------------------------FINISHED---------------------------------------------');
console.log(`Job ${job.attrs.name} completed succesfully...`);
console.log('--------------------------------------------------------------------------------------------');
});
}
const createWeeklySchedule = async(data) => {
const {
report_name,
schedule_info,
scheduled_timezone,
time_cron
} = data;
const weeklyReport = agenda.create(report_name, {data: schedule_info});
await agenda.start();
await agenda.every(time_cron,report_name);
console.log('Job successfully saved');
}
module.exports = scheduleReport;
Also I am starting app with app.js as main

Too many mysql connections / optimizing mysql query

After using my node applications for a couple of time I always get problems with too many MySQL queries. Currently I am using a MySQL Connection Pool which already increased the stability but isn't a final solution.
Is there a better way to connect to the MySQL database and close the connection directly after the query?
General "MySQL Connection":
const mysql = require('mysql');
const config = require('config');
const dbConfig = config.get('Azure.dbConfig');
console.log(`used mysql information: ${dbConfig}`);
const con = mysql.createPool(dbConfig
);
con.on('enqueue', () => {
console.log('connection enqued')
});
con.on('connection', function (connection) {
connection.query('SET SESSION auto_increment_increment=1')
});
con.on('release', function (connection) {
console.log('Connection %d released', connection.threadId);
});
module.exports = con;
Example for a MySQL query:
const con = require('../db.js');
const bodyParser = require('body-parser');
module.exports = function (app) {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/dbCompareName', function (req, res) {
const ingredients = [];
con.query('SELECT wm_wine.name, wm_wine.avocandoIngredient, wm_wine.jahrgang, wm_wine.sorte, wm_cart.anzahl, wm_wine.typ from wm_wine INNER JOIN wm_cart on wm_cart.id = wm_wine.id', function (err, result, info) {
if (err) {
console.log('while querying the mysql database, an error occurred');
console.log(err);
if (err.fatal === true) {
con.connect(err => {
if (err) throw err;
logger.info('reconnected to mysql database');
});
};
}
else {
const transfer = result;
};
};

Passing variable from one js to another js file in nodejs

I'm just getting started with Nodejs, so please bear with me
I store my DB setting on the first JS, connect.js :
var mysql = require('mysql');
module.exports = function(connectDB) {
var connectDB = {};
connectDB.connection = mysql.createConnection({
//db params
});
connectDB.connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
return connectDB;
};
Then I stored my query in another JS file, lets call it dbManager.js :
var db = require('./connect')(connectDB);
var test_connection = connectDB.connection.query('SELECT * FROM `test`', function (error, results, fields) {
console.log(results);
});
exports.test = test_connection;
My goal is to pass the connection variable from connect.js to dbManager.js, so I could use it for running some queries.
The above code return an error, which said the variable is not passed successfully to dbManager.js :
ReferenceError: connectDB is not defined
Thanks in advance
The syntax error is because you cant define variables within an object literal using var.
e.g., you can't do the following,
var t = {
"r": 4,
var g = 5;
};
You can do this,
var t = {
"r": 4,
"g" : 5
};
And to access the properties of the object you can do,
console.log(t["r"]);
console.log(t.g);
In your code the problem is declaring a variable inside an object literal. Yo could do,
var connectDB = {};
connectDB.connection = mysql.createConnection({
//DB params
});
connectDB.connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connectDB.connection.threadId);
});
return connectDB;
Edit1 As per OP's comments,
connect.js:-
Changes- No need of the connectDB param, using module.exports functionality.
var mysql = require('mysql');
var connectDB = {};
connectDB.connection = mysql.createConnection({
//db params
});
connectDB.connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connectDB.connection.threadId);
});
module.exports = connectDB;
dbManager.js:-
var db = require('./connect');//removed the parameter
//use db variable to process queries as returned from the above require statement.
var test_connection = db.connection.query('SELECT * FROM `test`', function (error, results, fields) {
console.log(results);
});
exports.test = test_connection;
**you can do it like this
connection.js**
var mysql=require('mysql');
// Database Connection
var connection = mysql.createConnection({
host : hostname,
user :username,
password : password,
database : databasename,
multipleStatements:true
});
try {
connection.connect();
} catch(e) {
console.log('Database Connetion failed:' + e);
}
module.exports=connection;
**you can use this connection file in your dbmanager file like
this..**
var db = require('./connection.js');var test_connection =
connection.query('SELECT * FROM test', function(err,result) {
console.log(result);
});
Will something like this work for you? You can have a file that returns a connection object from the pool:
var mysql = require('mysql');
module.exports = function() {
var dbConfig = {...};
var database = mysql.createPool(dbConfig);
return {
getConnection: function(callback) {
// callback(error, connection)
database.getConnection(callback);
}
};
};
Wherever you need to use it, you can require it as follows:
var connector = require('./db-connector')();
Then use it like this:
connector.getConnection(function(error, connection) {
// Some code...
// Be sure to release the connection once you're done
connection.release();
});
This is how I store config data to pass around on my node server. I call it config.js and .gitignore it. I keep a sample copy called config.sample.js
let config = {};
config.mysql-host='localhost' || process.env.MYSQL_HOST;
config.mysql-user='me' || process.env.MYSQL_USER;
config.mysql-secret='secret' || process.env.MYSQL_SECRET;
config.mysql-database='my_db' || process.env.MYSQL_DB;
module.exports = config; //important you don't have access to config without this line.
To use it I would do the following.
const config = require('./config');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: config.host,
user: config.user,
password: config.password,
});
connection.connect((err) => {
if(err) {
console.error(`error connecting: ${err.stack});
return;
}
console.log(`connected`);
});
const test_connection = connectDB.connection.query('SELECT * FROM `test`'(error, results, fields) => {
console.log(results);
});

Show informations one by one

My node.js app aim to scan a list of website an return in a panel, statusCode, and other tests.
Now the app work like that :
The page is empty until the end of the load and if one website in the list is slow the loading is slow, when 100% of the websites are scan the panel show.
How can i show the panel when i load the page, and show other informations progressively ?
var express = require('express');
var fs = require('fs');
var http = require('http');
var ejs = require('ejs');
var async = require('async');
var request = require('request');
var app = express();
var mysql = require('mysql');
var cheerio = require('cheerio');
app.use(express.static(__dirname+"/public"));
// Database connection
app.get('/', function(req, res) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'app'
});
connection.connect();
var jsons = new Array();
var odbc;
connection.query('SELECT * from websites', function(err, rows) {
if (err) throw err;
async.each(rows, function(row, callback) {
http.get(row.url, function(resp) {
row.status = resp.statusCode;
request(row.url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
str = $('*').text();
var errorODBC = str.indexOf("function.odbc-connect");
var errorTrans = str.indexOf(".WDD");
if(errorODBC != '-1'){
row.odbc = 'Erreur odbc';
}
if(errorTrans != '-1'){
row.aw = 'Erreur transfert';
}
jsons.push(row);
callback();
}
});
}).on('error', function(e) {
console.log("Erreur : " + e.message);
});
},function(){
res.render('index.ejs', {data : jsons});
console.log("Scan done...");
});
});
connection.end();
});
app.listen(8080);

Categories