4 Mysql2
4 Mysql2
4 Mysql2
● MySQL clients
● mysql2 examples
● Prepared Statements
● Benefits of using Prepared statements
sidorares/node-mysql2 - https://github.com/sidorares/node-mysql2
Based on nodejs-mysql-native. Offers additional feature like prepared
statements.
mysqljs/mysql
Another popular node.js driver for mysql, with limited features.
mysql2
• We can use the mysql2 module to do connect to the MySQL DBMS
programmatically.
• We have already used MySQL workbench which helps us connect and
interreact with the database using a GUI.
• mysql2 module allows us to submit a query to our MySQL server and
get a response back with the information we need.
• Before we can do that, we need to provide setup the mysql2 module with
our login information for our server, which is best stored within a .env file
Using the node-mysql2 module
• Setup a simple express
const express = require('express');
server in a new folder. const mysql = require('mysql2');
• npm init –y
const dotenv = require('dotenv').config();
• npm install dotenv
express const app = express();
app.get('/', (req, res) => {
• Install the mysql2 module res.send('hello world');
});
• npm install mysql2
const PORT = process.env.PORT;
app.listen(PORT);
Using the mysql2 module continued…
const express = require('express');
• After importing our mysql2 module, we const dotenv = require('dotenv').config();
const mysql = require('mysql2')
can use the mysql.createConnection()
method to provide our login info const app = express();
db.query("SELECT * from users WHERE email = ? AND password = ?", [req.body.email, req.body.password])
Prepared Statements
● Prepared statements, also known as parameterized statements or
parameterized SQL, are like templates for SQL statements.
● Also helps to execute the same statement repeatedly with high efficiency.
● Values can be plugged into the query after the query is “prepared”, and
ready to be executed.
● Prepared statements use question marks (?), which are positional
placeholders for where actual values that will be used in the SQL should
be “plugged” in.
The prepared statement execution consists of two stages:
1. Prepare - A statement template is sent to the database server.
Better performance
● It is compiled and optimized only once by the database engine.
● Does not have to be compiled and optimized each and every time the
values in the query change.
Preventing SQL Injection
● One of the best solution to preventing SQL injection attack.
● Values that will be inserted into a SQL query are sent to the SQL server
after the actual query is sent to the server.
connection.execute(
'SELECT * FROM `users` WHERE `name` = ? AND `age` > ?',
['John Doe', 23],
(err, results, fields) => {
// results contains rows returned by server
console.log(results);
// fields contains extra meta data about results, if
available
console.log(fields);
}
);
connection.unprepare('SELECT * FROM `users` WHERE `name` = ?
AND `age` > ?');
Execute (Prepare + Query) | UnPrepare
// with placeholder
connection.query(
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
['Page', 45],
function(err, results) {
console.log(results);
}
);
MySQL Connection Pools
● Connection pools help reduce the time spent connecting to the
MySQL server by reusing a previous connection.
● Connections are left open instead of being closed when you are
done with them.
● Improves the latency of queries by avoiding the overheads in
establishing a new connection every time.
// Simple single connection to
database
const connection =
mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test'
});
// Creates a connection pool to the database