Join us on :

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 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 MySQL module and use it in Node.js programs.


											npm install mysql	
									

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


									var mysql = require('mysql');
									


Steps in data Base connection:

  • 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 IP Address (of server where MySQL server is running), User Name and Password of user that has access 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 :


Query Database with Protractor

Create a function with 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 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 above 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 data base, it is a asynchronous function, so it accepts the query as first parameter and it it returns a call back.

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 below code we parse the result map into keys the 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 database connection in protractor.

This one makes sure the code executes properly Complete Code for data base 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()
        }) 
    });
});		

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • 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