# MySQL Database
Let's look at how you may read the MySQL
database state in vREST NG Application. But the process is the same for any other database as well.
Setup the package.json file and install the desired NPM module
First set up a
package.json
file in your vREST NG project to install the desired NPM module for your database. ForMySQL
, you may set up the followingpackage.json
file.{ "name": "db-validation", "description": "Database Validation Demo", "version": "1.0.0", "keywords": [ "vREST NG", "npm", "db-integration", "mysql" ], "dependencies": { "mysql": "^2.18.1" } }
Now install the dependencies using the command below:
npm install
Setting up the database connection
Now create a utility method to setup the database connection as shown in the figure below:
You may use the following script for setting up the MySQL connection:
(function(){ var aFunction = function(){ var vars = this.variables; var mysql = require('mysql'); return mysql.createConnection({ host : vars.mysql_host, user : vars.mysql_user, password : vars.mysql_password, database : vars.mysql_db }); }; return aFunction; })();
Setup the desired variables for the database connection
You may set up any variables required for the database connection. And also set up a variable for your database connection and value will be your utility method created in the previous step as shown in the figure below.
Setup the database query method
Now create a utility method to execute the database query as shown in the figure below:
You may use the following script for executing the MySQL Query:
(function(){ var aFunction = function(query){ var connection = this.variables.mysql_con; return new Promise(function(resolve, reject){ connection.query(query, function(error, results){ if(error) return reject(error); else { return resolve(results); } }); }); }; return aFunction; })();
Note:
- You will need to use the Javascript
Promise
feature in order to return asynchronous results from utility methods.
- You will need to use the Javascript
Execute the database query and return results
Now you may execute the database query in the
Variable Extractor
tab and store the results in a variable as shown in the figure below:These extracted variables are available in the same request but only during the response validation phase.
And you may validate the database results in
Validation
tab using theCustom Source
assertion as shown in the figure below:You may use any of the operators available to validate your database state.
So, in this way, you may validate the database state in vREST NG Application.