MySQL Databases with Protractor/nodejs

Node.js MySQL is one of the external libraries of Node.js. It helps Node.js developers to connect to the MySQL database and execute MySQL Queries.

As Node.js MySQL is an external module, it can be installed using NPM (Node Package Manager). Run the following command in terminal or command prompt to install the MySQL module and use it in Node.js programs.

npm install mysql

After successful installation, you need to import the MySQL module in node.js/protractor programs by declaring its usage with a require statement.

var mysql = require('mysql');


Steps to connect database:

  • Establish a connection to the MySQL database server.
  • Execute statements and process the result set.
  • Close the database connection.

Protractor Interview Questions

Connect to the database in Protractor/nodejs

To create a connection variable with the IP Address of the server where MySQL server is running, User Name and Password of the user that has access to the MySQL database.

An example is provided below :

mysql.createConnection({
	host:"sqlrrrrr.eu",
	user:"u233333333n",
	password:"B2222223",
	database:"u555555b"
})

Actual database rows

mysql-data-base-protractor

Video Tutorial for Database connection in protractor

Subscribe to my youtube channel :

NodeMailer to send emails

Query Database with Protractor

Create a function with the above connection configuration code, and then we have to call that cerated methods from our it blocks to establish the connection and to execute our query in the database.

function ConnectDatabase(){
	var mysql = require("mysql")
	this.connection = mysql.createConnection({
		host:"sql444444.eu",
		user:"u2055555n",
		password:"Bhhhhh3",
		database:"uyyyyy_db"
	})
}

Establish the connection : We need to connect to the database using connect() function from the mysql module, we have to object to the function to connect with details

var connectDatabase = new ConnectDatabase()
connectDatabase.connection.connect();

Execute the query We have to use the query() function from the mysql module to query the database an asynchronous function, so it accepts the query as the first parameter, and it returns a callback.

If query execution is successful, then call back will have data; otherwise, we receive an error, so we got to check for the error using if block.

If no error is present, then we can process our result. In the below code, we parse the resulting map into keys then we iterate the keys to get the values.

var sql = "delete from node_testing where name='vikas'"
connectDatabase.connection.query(sql, function(err, rows){
	if(err){
		console.log(err)
	}else{
		// console.log(JSON.stringify(rows))

		Object.keys(rows).forEach(function(keyItem){
			var row = rows[keyItem]
			console.log(row.name +" "+ row.designation)
		})
	}
	connectDatabase.connection.end()
	done()
        })

Close connection : It is very important to have a connection end statement as we want to end the connection as soon as our operation is over; otherwise, at certain point database will not let connect you because there are so many non-close connections.

connectDatabase.connection.end()

done() : If you have noticed very closes, I have used function(done) in it block description as well as I have closed it at the end. Without this, you cannot make a database connection in protractor.

This one makes sure the code executes properly Complete Code for database connection and execution.

describe('MYSQL Test', function() {
    function ConnectDatabase(){
        var mysql = require("mysql")
        this.connection = mysql.createConnection({
            host:"sql55555.eu",
            user:"u2555555n",
            password:"B555553",
            database:"u266666_db"
        })
    }
    fit('DataBase testing in Protractor', function(done) {
        var connectDatabase = new ConnectDatabase()
        connectDatabase.connection.connect();
        var sql = "delete from node_testing where name='vikas'"
        connectDatabase.connection.query(sql, function(err, rows){
            if(err){
                console.log(err)
            }else{
                // console.log(JSON.stringify(rows))

                Object.keys(rows).forEach(function(keyItem){
                    var row = rows[keyItem]
                    console.log(row.name +" "+ row.designation)
                })
            }
            connectDatabase.connection.end()
            done()
        })
    });
});

JSON File in Protractor

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Reply
  • Parag
    I need code to connect the Mongo DB and serach in collection for some value.
    
    T
    Reply
  • vishnu
    Hi Sir,
                    I need to connect the DB but what are the requirements need please tell me 
    Reply
  • Rahul Sharma
    Does this is valid for Oracle database as well
    Reply