# DB Validation

In this guide, we will look at how you may use Variable Extractor tab and Utility Methods to validate any database state with vREST NG Application.

Now, we will look at how you may read the MySQL database state in vREST NG Application. But the process is same for any other database as well.

  1. Setup the package.json file and install the desired NPM module

    First setup a package.json file in your vREST NG project to install the desired NPM module for your database. For MySQL, you may setup the following package.json file.

    {
      "name": "db-validation",
      "description": "Database Validation Demo",
      "version": "1.0.0",
      "keywords": [
        "vREST NG",
        "npm",
        "db-integration"
      ],
      "dependencies": {
        "mysql": "^2.18.1"
      }
    }
    

    Now install the dependencies using the command below:

    npm install
    
  2. 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;
    })();
    
  3. Setup the desired variables for the database connection

    You may setup any variables required for the database connection. And also setup a variable for your database connection and value will be your utility method created in previous step as shown in figure below.

  4. 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(res, opts, 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:

    1. You will need to use the Javascript Promise feature in order to return asynchronous results from utility methods.
    2. As we will invoke this utility method from the Variable Extractor tab, so there are two extra input parameters res and opts. We recommend you to not use these parameters as they are deprecated and will be removed in future releases.
  5. 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 the Custom 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.