# MongoDB

Let's look at how you may read the MongoDB database state in vREST NG Application. But the process is the same for any other database as well.

The process involves the following steps:

  1. Setup the package.json file and install the desired NPM module
  2. Setting up the database connection
  3. Setup the desired variables for the database connection
  4. Setup the database query method
  5. Execute the database query and return results

Now, let's see these steps in detail.

  1. 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. For MongoDB, you may set up the following package.json file.

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

    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 MongoDB connection:

    (function(){
    
      var aFunction = function(){
        var vars = this.variables;
        const dbName = vars.db_name;
        const MongoClient = require('mongodb').MongoClient;
        const client = new MongoClient(vars.connection_url, { useUnifiedTopology: true });
        return new Promise(function (resolve, reject){
          client.connect(function(err) {
            if(err) return reject(err);
            console.log('Connected successfully to MongoDB server.');
            resolve(client.db(dbName));
          });
        });
      };
    
      return aFunction;
    })();
    
  3. 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 say mongo_con and the value will be your utility method created in the previous step as shown in the figure below. We will use this variable mongo_con in our next step.

  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 finding a document/record by id from MongoDB:

    (function(){
    
      var aFunction = function(colName, id){
        const ObjectID = require('mongodb').ObjectID;
        var connection = this.variables.mongo_con;
        var collection = connection.collection(colName);
        return new Promise(function(resolve, reject){
          collection.findOne({ _id: ObjectID(id) }, function(error, result){
            if(error) return reject(error);
            else {
              if(result){
                result._id = result._id.toString();
              }
              return resolve(result);
            }
          });
        });
      };
    
      return aFunction;
    })();
    

    Note:

    1. You will need to use the Javascript Promise feature in order to return asynchronous results from utility methods.
    2. For fetching a list of records, you may write a separate utility method as per your needs similar to this method.
  5. Execute the database query and return results

    Now, from your API Tests, 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.