Nodejs async/await mysql queries - javascript

I have a nodejs project with the current structure below, I need to insert a registry on clients table and return the last inserted ID from this table so I can use it in a second table, but I need to wait until the insert is completed in clients table, before insert the client ID on my second table. I'm trying to use async/await, but I'm always getting a null value.
My MYSQL connection: db.model.js
const config = require('config');
const mysql = require("mysql");
const connection = mysql.createConnection({
host: config.get('mysql.host'),
user: config.get('mysql.user'),
password: config.get('mysql.password'),
database: config.get('mysql.database')
});
connection.connect(function (err) {
if (err) {
console.error(`MySQL Connection Error: ${err.stack}`);
return;
}
console.log(`MySQL connected successfully!`);
});
module.exports = connection;
My CLIENT model
const mysql = require("./db.model");
const Client = function(client) {
this.login = client.login;
};
Client.create = (newClient, result) => {
mysql.query("INSERT INTO clients SET ?", newClient,
(err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
result(null, {
id: res.insertId,
...newClient
});
}
);
};
module.exports = Client;
this is the client controller (i'm trying to use async/await here)
const Client = require('../models/client.model');
exports.create = (login) => {
const client = new Client({
login: login
});
Client.create(client, async (err, data) => {
if(!err) {
return await data.id;
} else {
return false;
}
});
}
And this is another controller, where I want to use methods from my client controller:
const ClientController = require('../controllers/client.controller');
...
utils.connect()
.then(clt => clt.sub.create(data))
.then((sub) => {
let lastInsertedId = ClientController.create(sub.login);
// lastInsertedId always return null here,
// but I know ClientController return a value after some time.
// method below will fail because lastInsertedId cannot be null
TransactionController.transactionCreate(lastInsertedId,
sub.id,
sub.param);
})
.catch(error => res.send(error.response.errors))
any help appreciated.

File to create database connection
const config = require('config');
const mysql = require('mysql2');
const bluebird = require('bluebird');
const dbConf = {
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.database,
Promise: bluebird
};
class Database {
static async getDBConnection() {
try {
if (!this.db) {
// to test if credentials are correct
await mysql.createConnection(dbConf);
const pool = mysql.createPool(dbConf);
// now get a Promise wrapped instance of that pool
const promisePool = pool.promise();
this.db = promisePool;
}
return this.db;
} catch (err) {
console.log('Error in database connection');
console.log(err.errro || err);
}
}
}
module.exports = Database;
Use connection to execute your native query
const database = require('./database');
let query = 'select * from users';
let conn = await dl.getDBConnection();
let [data, fields] = await conn.query(query);

So I'm still using only the npm mysql package, but now I transformed all my queries into promises like below, so I can just wait until all the queries are completed.
const create = (idCliente, transactionId, amount, status) => {
const sql = "INSERT INTO transactions SET ?";
const params = {
id_cliente: idCliente,
transaction_id: transactionId,
amount: amount,
status: status
};
return new Promise((resolve, reject) => {
pool.query(sql, params, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
};
then I use like this:
create(params)
.then((result) => {
//call more queries here if needed
})
.catch((err) => { });

You can use sync-sql package of npm for execute async queries.
https://www.npmjs.com/package/sync-sql
Here is an example of it:
const express = require('express')
const mysql = require('mysql')
const app = express()
var syncSql = require('sync-sql');
// Create Connection
const connect = {
host: 'localhost',
user: 'root',
password: '',
database: 'ddd_test'
}
const db = mysql.createConnection(connect)
db.connect((err) => {
if (err) {
throw err
}
console.log("Connected");
})
function getDbData(query) {
return syncSql.mysql(connect, query).data.rows
}
app.get("/getData", async (req, res, next) => {
let sql = 'SELECT * from registration';
res.json({
data:getDbData(sql)
});
})
app.listen(3000, () => {
console.log('App listening on port 3000!');
});

Related

ReferenceError: Can't find variable: require. How can I fix this error?

I want to export some functions from my database.js file and require them into the other file login.js, which is in the same folder. I tried also to declare the require globally and in a function, both ways did not work. I am using node.js/express.js.
DATABASE.JS
let mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
database: 'minigames',
user: 'root',
password: 'root'
});
function connected(){
connection.connect((err) => {
if(err) throw err;
console.log("Connected...");
})
}
function select(attribute){
return new Promise((resolve, reject) => {
let sql = `SELECT ${attribute} FROM player`;
connection.query(sql, (err, result, field) => {
if(err) reject(err);
resolve(Object.values(JSON.parse(JSON.stringify(result))));
})
})
}
async function query(attribute) {
return await select(attribute);
}
module.exports = {connection, connected, select, query};
LOGIN.JS
let userList = [];
function checkLogin(){
const database = require('./database');
let userListPromise = database.query('username, password');
userListPromise
.then()
.then((values) =>{
let i = 0;
while(values[i] != null){
userList.push(values[i]);
i++;
}
console.log("list filled");
console.log(userList);
})
}
checkLogin();

How to keep object state in electron/node.js?

I'm trying to create a Database class using node.js mysql module.
The point is, I want to be able to use the class in different files. But the class itself should keep one connection persistent. So I need somehow to keep object state between files.
My code so far:
main.js
const database = require('./app/database.js');
//// somewhere later in BrowserWindow initianisation
database.init(config);
custom_file.js
const database = require('./app/database.js');
setInterval(
() => {
updateOrdersTable();
}
, 10000
);
function updateOrdersTable() {
console.log('updateOrdersTable');
database.getCurrentOrders((orders) => {
orders.forEach((row) => {
console.log(row);
});
});
}
app/database.js
const mysql = require('mysql');
class Database {
config = {};
connection = {};
customerWindow = {};
stockWindow = {};
init(config) {
this.config = config;
process.stdout.write('Connecting to MySQL Server...');
this.connect();
}
connect() {
this.connection = mysql.createConnection({
host: this.config.database.host,
port: this.config.database.port,
user: this.config.database.user,
password: this.config.database.password,
database: this.config.database.database
});
const that = this;
this.connection.connect(function(err) {
if (err) {
console.log('Database error', err);
setTimeout(that.connect, that.config.database.reconnect_interval * 1000);
return;
}
console.log('Success!');
});
this.connection.on('error', function(err) {
console.log('Database error', err);
setTimeout(that.connect, that.config.database.reconnect_interval * 1000);
});
}
getCurrentOrders(callback) {
this.connection.query("SELECT * FROM current_orders", function (err, result, fields) {
if (err) throw err;
callback(result)
});
}
}
module.exports = new Database();
The error message I'm getting:
Uncaught TypeError: this.connection.query is not a function
at Database.getCurrentOrders (F:\my-app\app\database.js:91)
Line 91 is this:
this.connection.query("SELECT * FROM current_orders", function (err, result, fields) {

Change from firestore to mongodb, connect to mongodb

I'm moving my db from firestore to mongodb. I used to call the db with a const.
const db = firebase.firestore();
firebase.firestore().settings({
cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
});
db.collection("users").doc(user.uid)
.get().then(function(doc) {
if (doc.exists) {
//do something
} else {
console.log("No such document!")
}}).catch(function(error) {
console.log("Error getting document:", error)
});
Now in mongodb, I create if data needed a mongoClient.connect().
Is there a way to call a function or a const and do the same thing like in firestore, or a cleaner way?
const mongo = require('mongodb');
const MongoClient = mongo.MongoClient;
const url = 'mongodb://localhost:27017';
const dbname = "dbname";
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) throw err;
const db = client.db(dbname);
let collection = db.collection('users');
collection.findOne({ _id: user.uid }).then(user => {
//do something
}).catch((err) => { console.log(err);
}).finally(() => { client.close(); });
});
You can do something like this. Create a wrapper function for database calls
function findOne(query,collectionName,callback){
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) throw err;
const db = client.db(dbname);
let collection = db.collection(collectionName);
collection.findOne(query).then(user => {
callback(null,user);
}).catch((err) => { callback(err,null);
}).finally(() => { client.close(); });
});
}
Similarly you can create multiple functions for another operations or you may create only one function and then use it as wrapper. The main idea here is that we can utilise callback mechanism
For consuming it
route.get('/',(req,res,next)=>{
findOne({ _id: user.uid },'users',(err,user)=>{res.send(user)})
})

How to connect Dialogflow Fulfillment with MySQL DB?

I tried to connect my Dialogflow agent with MySQL DB at my GCP, but it didn't work well.
I just wanna see the results in console, but there's no results about that.
I can only see the result "Undefined", but since I am not familiar with Node.js, I am not sure what this means.
Since there is no content in the result, I cannot proceed to the next step.
Here is my index.js script :
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const mysql = require('mysql');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function connectToDatabase() {
const connection = mysql.createConnection({
host: '34.64.***.***',
user: 'username',
password: 'password',
database: 'dbname'
});
return new Promise((resolve,reject) => {
connection.connect();
console.log('connection successed.');
resolve(connection);
});
}
function queryDatabase(connection, name){
return new Promise((resolve, reject) => {
connection.query('SELECT * FROM tb_user WHERE name = ?', name, (error, results, fields) => {
console.log('query successed.');
resolve(results);
});
});
}
function queryDatabase2(connection){
return new Promise((resolve, reject) => {
connection.query('SELECT * FROM tb_user', (error, results, fields) => {
if (error) console.log(error);
console.log('results', results);
});
});
}
function handleReadFromMysql(agent) {
const user_name = agent.parameters.name;
console.log(user_name.name);
return connectToDatabase()
.then(connection => {
console.log('connectToDatabase passed');
return queryDatabase(connection, user_name.name)
//return queryDatabase2(connection)
.then(result => {
console.log('queryDatabase passed');
console.log(result);
agent.add(`User name is ${user_name} .`);
//result.map(user => {
//if(user_name === user.name) {
//agent.add(`UserID is ${user.id}`);
//}
//});
connection.end();
});
});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('findName', handleReadFromMysql);
agent.handleRequest(intentMap);
});
and the logs are :
enter image description here
Please somebody help me...
I solved this problem 4 months ago, so I share my code to help another like me.
I use socketPath of DB info. I forgot the reason.
function connectToDatabase() {
const connection = mysql.createConnection({
socketPath: '/cloudsql/*************:asia-northeast3:****',
user: '****',
password: '****',
database: '****'
});
return new Promise((resolve, reject) => {
connection.connect();
console.log('connection successed.');
resolve(connection);
});
}
And make query function like :
function findNameCountQuery(connection, name) {
return new Promise((resolve, reject) => {
var sql = `SELECT * FROM tb_user WHERE name = ?`;
var execSql = connection.query(sql, [name], (error, results) => {
if (error) throw error;
resolve(results);
});
});
Then I use agent function like :
async function findName(agent) {
const user_name = agent.parameters.person.name;
const connection = await connectToDatabase();
const result = await findNameQuery(connection, user_name);
result.map(user => {
message += `사용자 ID seq number : ${user.seq}\n`;
});
agent.add(message);
connection.end();
}
Hope to be helpful.

Close MSSQL connection in Node

I'm connecting with SQL Server using Node mssql package in my Electron app.
I can't create REST API.
It works fine although I have concerns:
it doesn't close SQL connection after query
it makes a new DB connection for each query
Is that ok?
How it works:
app.js makes 2 queries and logs results
sql.js connects with DB
// app.js
const { getUser, getUsers } = require('./sql');
getUser(10).then((result) => {
console.dir(result);
});
getUsers.then((result) => {
console.dir(result);
})
// sql.js
const sql = require("mssql");
// DB credentials
const config = {
user: 'myuser',
password: '123',
server: 'myserver',
database: 'mydb',
options: {
encrypt: true
}
}
// Creates new connection pool for each query
function connectDB() {
const pool = new sql.ConnectionPool(config);
return pool.connect()
.then(pool => {
console.log('Connected to database');
// returns Promise
return pool;
})
.catch(err => console.log('Database connection failed!', err));
}
// 1st query
function getUser(id) {
const connection = connectDB();
return connection
.then(pool => {
return pool.request()
.input('PK_ID', sql.Int, parseInt(id))
.execute('[uspGetUser]');
})
.then(result => {
return result.recordset[0];
})
.catch(err => {
console.log('Query failed!', err);
});
}
// 2nd query
function getUsers() {
const DB = connectDB();
return DB
.then(pool => {
return pool.request()
.execute('[uspGetUsers]');
})
.then(result => {
return result.recordset[0];
})
.catch(err => {
console.log('Query failed!', err);
});
}
module.exports = {
getUser,
getUsers
}
No, you don't need to close a.k.a. release a connection back to the connection pool after every query. The library already does that for you.
The pool.close() method will close all the connection in the pool. Technically, you should only do that when you're terminating your application, not after every query, since creating a new pool every time create quite an overhead on your application.
I had the same question myself, so I looked up the project's source code.
Solved!
To close DB connection and return results, we can use finally statement and asnyc/await functions.
The finally statement lets you execute code, after try and catch, regardless of the result.
// sql.js
const sql = require("mssql");
// DB credentials
const config = {
user: 'myuser',
password: '123',
server: 'myserver',
database: 'mydb',
options: {
encrypt: true
}
}
async function connectDB() {
const pool = new sql.ConnectionPool(config);
try {
await pool.connect();
console.log('Connected to database');
return pool;
}
catch(err) {
console.log('Database connection failed!', err);
return err;
}
}
async function getAll() {
const DB = await connectDB();
try {
const result = await DB.request()
.query('select * from [your_table]');
return result.recordset;
}
catch (err) {
console.log('Error querying database', err);
return err;
}
finally {
DB.close();
}
}
async function execute() {
let result = await getAll();
console.dir(JSON.stringify(result));
return result;
}
execute();

Categories

Resources