Establishing database connection between node.js angular and mysql workbench - javascript

Im learning from a ready application and im trying to make the connection so the app to start working and save data.
picture of the hostname and port
I done the connectin also make a 'employees database' with the two tables that I need
picture of the schema
The server is running on localhost 3000 and the front end is listening on port 4200. This is the code for the connection and creating the database/schema.
config.json
{
"host": "localhost",
"user": "cveto",
"database": "employees",
"password": "1234567"
}
database.js creating pool
const mysql = require('mysql2');
const config = require('../config/config.json');
const pool = mysql.createPool({
host: config.host,
user: config.user,
database: config.database,
password: config.password,
});
module.exports = pool.promise();
And some query models
const db = require('../util/database');
module.exports = class Employee {
constructor(names, address, number, salary) {
this.names = names;
this.address = address;
this.number = number;
this.salary = salary;
}
static fetchAll() {
return db.execute('SELECT * FROM employees');
}
static save(employee) {
return db.execute(
'INSERT INTO employees (names, address, number, salary) VALUES (?, ?, ?, ?)',
[employee.names, employee.address, employee.number, employee.salary]
);
}
static delete(id) {
return db.execute('DELETE FROM employees WHERE id = ?', [id]);
}
};
I just want to know how I can make the connection so the app to work and save to my database the users and new employees. The code is from public repositority so the code is working, its now from the code its from tha database or server conncetion.

DB.js File
var mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 100,
host : 'localhost',
user : 'root',
password : '',
database : '',
});
let myDb = {};
myDb.searchDataQ = (query) => {
return new Promise((resolve, reject) => {
pool.query(query, function (err, rows) {
if (err) {
reject(err);
}
resolve(rows)
})
});
};
module.exports = myDb;
Other JS File to execute
const db = require('../db/db');
function testMethod() {
let query = 'SELECT * FROM employees';
return new Promise(async (resolve, reject) => {
try {
resolve(await db.searchDataQ(query));
} catch (e) {
reject(0);
}
});
}

Related

asyncronous Mysql.createConnection({})

I need to create a mysql connection in nodejs , but the credentials for the mysql comes from a third party credential manager service. Can somebody suggest me a way to achieve this?
database.js - i am using connection from this file in all other database operations
const mysql = require("mysql");
const {env} = require('./globals')
const connection = mysql.createConnection({
host: env.DATABASE.HOST,
user: env.DATABASE.USER,
password: env.DATABASE.PASSWORD,
database: env.DATABASE.NAME,
multipleStatements: true
});
connection.connect(function (err) {
if (err) {
console.log("Error in DB connection");
console.log("err", err);
} else console.log("Connected!");
});
module.exports = connection
globals.js
const {getSecret} = require('../src/service')
require("dotenv").config();
async function getCredentials() {
const result = await getSecret()
return JSON.parse(result?.SecretString || {})
}
const credentials = getCredentials() // not working, and i can't use await here
const env = {
DATABASE: {
HOST: credentials.ip_address,
PASSWORD: credentials.password,
NAME: credentials.dbname,
USER: credentials.username,
},
SKU: process.env.SKU
}
module.exports.env = env
Your 2 main options are:
Don't export connection but export an async function that returns a connection.
Write an init() function that sets up your database connection, and ensure it's one of the first things your application calls before anything else.
Well first, you need to fix up that globals.js file. Logic that depends on an async function must be async itself. You can just move everything into the async function like so:
const {getSecret} = require('../src/service')
require("dotenv").config();
async function getCredentials() {
const result = await getSecret()
const credentials = JSON.parse(result?.SecretString || {})
return {
DATABASE: {
HOST: credentials.ip_address,
PASSWORD: credentials.password,
NAME: credentials.dbname,
USER: credentials.username,
},
SKU: process.env.SKU
}
}
module.exports = { getCredentials }
And since even your database connection in database.js depends on this async function, it will have to be async as well:
const mysql = require("mysql");
const {getCredentials} = require('./globals')
const getConnection = getSecret().then(function (env) {
const connection = mysql.createConnection({
host: env.DATABASE.HOST,
user: env.DATABASE.USER,
password: env.DATABASE.PASSWORD,
database: env.DATABASE.NAME,
multipleStatements: true
});
connection.connect(function (err) {
if (err) {
console.log("Error in DB connection");
console.log("err", err);
} else console.log("Connected!");
});
return connection;
})
module.exports = getConnection

Release PostgeSQL connection Pool in Nodejs

I am trying to connect my application to the database using the connection pool method, its connecting fine, and data insertion is happening fine without any issues but other queries in the same file are slowing down.
I have tried with release() method also not working properly.
How can release the pool to the next query once it's executed the current query?
Below is my dbpool.js file code where I am writing a common generalized database connection,
var pg = require('pg');
var PGUSER = 'postgres';
var PGDATABASE = 'test_database';
var config = {
user: PGUSER, // name of the user account
host: 'localhost',
database: PGDATABASE, // name of the database
password: 'password#AWS',
port: 5432,
max: 10,
idleTimeoutMillis: 10000
};
const pool = new pg.Pool(config);
const DB = {
query: function(query, callback) {
pool.connect((err, client, done) => {
if(err){ return callback(err); }
client.query(query, (err, results) => {
// done();
client.release();
// if(err) { console.error("ERROR: ", err) }
if(err) { return callback(err); }
callback(null, results.rows);
})
});
}
};
module.exports = DB;
I tried with both the done() and client.release() method but no luck. If I use both then I am getting an error message client is already released.
Below is my socket.js file code:
var express = require('express');
const connection = require('./dbpool.js');
if(arData == '0022'){
const queryText = "INSERT INTO alert(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
if(arData == '0011'){
const queryText = "INSERT INTO table2(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
function ReverseCommunication(){
const select1 = "SELECT * FROM alert WHERE action = '0' ORDER BY alert_id ASC LIMIT 1";
connection.query(select1, (err, res) =>{
if(err) {
console.log("Error1");
res.json({"error":true});
}
else{
console.log("res==",res);
}
});
}
setInterval(function(){
ReverseCommunication();
}, 2000)
With pool you shouldn't need to close the connection. With pool it will reuse the connection pool for subsequent request so you don't have to connect to the DB each time.
(i'm not a PG expert here, sure other could expand on that way better then I )
What works for us is to set up the dbpool file you have like this
const {Pool,Client} = require('pg');
const pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_URL,
database: process.env.POSTGRES_DATABASE,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
keepAlive: true,
connectionTimeoutMillis: 10000, // 10 seconds
max: 10
});
pool.connect()
.then(() => console.log('pg connected'))
.catch(err => console.error(err))
module.exports = pool
Then use the pool.query like you have now with pool.connect
Also, just a side note what lib are you using for PG? Noticed your queries are dynamic, you may want to adjust those to prevent possible SQL-injection.

How do I resolve the error "code": "22001" in postgresql

Hello I want to implement database to the node project I'm working on. On testing the authentication endpoint I getting the error "message": { "length": 164, "name": "error", "severity": "ERROR", "code": "22001", "file": "d:\\pginstaller_13.auto\\postgres.windows-x64\\src\\backend\\utils\\adt\\varchar.c", "line": "635", "routine": "varchar" } via postman
I am not certain I did my database set up correctly, any advice on how to set up postgresql environment properly such that I can deploy to Heroku and please how can I resolve the error ?
Auth.js
const bcrypt = require('bcrypt')
const pool = require('../configuration/config');
const validate = require('../middleware/auth.validation')
const signup = async (request, response)=>{
try {
//1. destructure req.body
const {firstname, lastname, username, email, password} = request.body;
//2. validate user input
const validationError = validate.signup(firstname,lastname,username, email, password)
if(validationError.message){
return response.status(400).json({status:'Validation Error', message: validationError.message})
}
//3. check if user exist (if user exist , throw err)
const userExist = await pool.query(`SELECT * FROM users WHERE username = $1`, [username]);
if(userExist.rows.length !== 0){
return response.status(401).json({message: "User Already Exist"})
}
// 4. hash user password
const salt = await bcrypt.genSalt(10)
const hashPassword = await bcrypt.hash(password, salt)
//5. Insert user into db
const newUser = await pool.query('INSERT INTO users (firstname, lastname, username, email, password) VALUES($1, $2, $3, $4, $5) RETURNING *', [firstname, lastname, username,email, hashPassword]);
response.json(newUser)
// 6. generate jwt token
} catch (error) {
response.status(500).send({message:error})
}
}
module.exports = {signup}
config.js
require('dotenv').config()
const { Pool } = require('pg')
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DATABASE_PASSWORD,
port: process.env.DB_PORT,
})
pool.on('connect', ()=>{
console.log('connected to database')
})
module.exports = pool
pool.js database Schema
require('dotenv').config()
const { Pool } = require('pg')
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DATABASE_PASSWORD,
port: process.env.DB_PORT,
})
pool.on('connect', ()=>{
console.log('connected to database')
})
module.exports = pool
queries.js
const pool = require('./pool')
module.exports = {
/**
* DB Query
* #param {object} req
* #param {object} res
* #returns {object} object
*/
query(quertText, params) {
return new Promise((resolve, reject) => {
pool.query(quertText, params)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
},
};
It's weird that it gives you the exact source code line in the postgres source code but not the actual error message... Looking at line 635 of src/backend/utils/adt/varchar.c gives:
ereport(ERROR,
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
errmsg("value too long for type character varying(%d)",
maxlen)));
So, yeah, the column was probably declared as VARCHAR(N) and the string you're trying to put in the column has more than N characters, so it throws an error.

How to use db.query from a .js file

Document Structure:
|-public/
|-js/
|-shop.js
|-views/
|-routes/
|app.js
I have defined my sql connection in my app.js
const mysql = require('mysql');
const db = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
database: 'pfis'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
All sql queries under app.js work fine!
My problem is that i have a shop.js file (see directory structure) which needs to insert some sql (stored procedure) once they clicked on a button element on my page. And i can't figure out how to achieve this.
example shop.js (which is not working!):
function purchaseClicked() {
var stoel = prompt("Enter your chairnumber: ");
alert('Someone is on this way with the ATM-machine');
var cartItems = document.getElementsByClassName('cart-items')[0];
while (cartItems.hasChildNodes()) {
var itemTitle = document.getElementsByClassName('cart-item-title')[0].innerHTML;
var itemQuantity = document.getElementsByClassName('cart-quantity-input')[0].value;
db.query("Call test1_insert(" + itemTitle + ", " + itemQuantity + ", " + stoel + ");",
function (error, results, fields) {
if (error) {
alert("Something went wrong, try again!");
}
alert("Looks like all good!");
});
cartItems.removeChild(cartItems.firstChild);
}
updateCartTotal();
}
I have tried to add the same db connection code from app.js (see above snippet) in the shop.js file but it doesnt like that either.
Who can help me how to execute SQL from a "outside" .js file?
I use Sequelize for this.
Db file like this :
var sequelize = new Sequelize(mysqlDatabase, mysqlUser,mysqlPassword, {
host: mysqlHost,
dialect: 'mysql',
pool: {
max: 1000,
min: 0,
idle: 10000
},
//logging: logger.info
logging: false
});
var db = {};
db.Shop = sequelize.import(__dirname + '/models/Shop.js');
module.exports = db;
After creating db file you can reach shop like this:
const db = require('path/to/sequelize/file');
db.Shop.create(data);

ReferenceError: connection is not defined in nodejs

Both .js files are in the same folder. And I'm attempting to run the app, but I recieve that connection is not defined in user.js. It's on the row where query is called on connection that it says that it's not defined.
index.js
var exports = module.exports = {};
var user = require('./user');
exports.User = user;
exports.startCon = startCon;
var mysql = require('mysql2');
function startCon() {
return mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'users'
})
}
user.js
var dal = require('./index');
function User(){
this.connection = null;
}
User.prototype.getAll = function(cb){
this.connection = dal.startCon();
connection.query('SELECT * FROM user;', function (error, data) {
if(!error){
cb(null, data);
}
else {
console.log("Error Selecting : %s ", error );
cb(error);
}
});
connection.end();
}
module.exports = User;
Try close connection inside the callback function
callback() {
connection.close()
}
And this module really weird
You need construct connection instance inside function
var mysql = require('mysql');
let doSmth = function(userId) {
let connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
return new Promise((resolve, reject) => {
connection.query({
sql: 'SELECT `address` FROM `User` WHERE `user_id` = ?'
},
[userId],
function (error, results, fields) {
console.log(error, results)
if(!error) {
var obj = JSON.parse(JSON.stringify(results));
resolve(obj)
} else {
reject(error)
}
}
);
})
}
module.exports = {
doSmth
};

Categories

Resources