text: /*
   * used to populate the Cve collection without dropping/updating any other
   * collection
   */
  
  const express = require('express')
  const config = require('config')
  const mongoose = require('mongoose')
  const app = express()
  const logger = require('../middleware/logger')
  const Cve = require('../model/cve')
  const cveData = require('../../datadump/pre-population/cves')
  const ProgressBar = require('progress')
  const errors = require('../utils/error')
  const error = new errors.IDRError()
  require('dotenv').config() // This enables us to read from the .env file.
  
  // Body Parser Middleware
  app.use(express.json()) // Allows us to handle raw JSON data
  app.use(express.urlencoded({ extended: false })) // Allows us to handle url encoded data
  // Make mongoose connection available globally
  global.mongoose = mongoose
  
  // construct MongoDB connection string
  // assumes that host, port, database are always defined in default config, but
  // that username and password may not be
  const appEnv = process.env.NODE_ENV
  let dbUser, dbPassword
  
  if (process.env.MONGO_USER && process.env.MONGO_PASSWORD) {
    dbUser = process.env.MONGO_USER
    dbPassword = process.env.MONGO_PASSWORD
  } else {
    dbUser = config.has(`${appEnv}.username`) ? config.get(`${appEnv}.username`) : false
    dbPassword = config.has(`${appEnv}.password`) ? config.get(`${appEnv}.password`) : false
  }
  
  const dbHost = process.env.MONGO_HOST ? process.env.MONGO_HOST : config.get(`${appEnv}.host`)
  const dbPort = process.env.MONGO_PORT ? process.env.MONGO_PORT : config.get(`${appEnv}.port`)
  const dbName = config.get(`${appEnv}.database`)
  const dbLoginPrepend = (dbUser && dbPassword) ? `${dbUser}:${dbPassword}@` : ''
  const dbConnectionStr = `mongodb://${dbLoginPrepend}${dbHost}:${dbPort}/${dbName}`
  
  // Connect to MongoDB database
  mongoose.connect(dbConnectionStr, {
    useNewUrlParser: true,
    useUnifiedTopology: false,
    useFindAndModify: false
  })
  
  // database connection
  const db = mongoose.connection
  
  db.on('error', () => {
    console.error.bind(console, 'Connection Error: Something went wrong!')
    logger.error(error.connectionError())
  })
  
  db.once('open', async () => {
    // we're connected!
    logger.info(`Successfully connected to database ${dbName} at ${dbHost}:${dbPort}`)
  
    // Ask user to confirm pre-population, which will prep MongoDB by removing the Cve-Id, Org, and User collections
    const prompt = require('prompt-sync')({ sigint: true })
    let userInput = prompt(
      `Are you sure you wish to pre-populate the database for the ${appEnv} environment?` +
      `Doing so will drop the 'Cve' collection in the ${dbName} database. (y/n) `
    )
  
    while (userInput.toLowerCase() !== 'n' && userInput.toLowerCase() !== 'y') {
      console.log('Unrecognized Input')
      userInput = prompt(
        `Do you wish to pre-populate the database for the ${appEnv} environment?` +
        `Doing so will drop the 'Cve', collection in the ${dbName} database. (y/n) `
      )
    }
  
    // dropping Cve collection
    // this may be more complex than necessary, but is kept similar to `src/scripts/populate.js`
    if (userInput.toLowerCase() === 'y') {
      let names = []
      let collections = await db.db.listCollections().toArray()
      collections.forEach(collection => {
        names.push(collection.name)
      })
  
      if (names.includes('Cve')) {
        await db.dropCollection('Cve')
      }
  
      names = []
      collections = await db.db.listCollections().toArray()
      collections.forEach(collection => {
        names.push(collection.name)
      })
  
      if (!names.includes('Cve')) {
        await populateCveCollection()
        logger.info('Successfully pre-populated the database!')
      } else {
        logger.error('The database was not populated because some of the collections were not deleted.')
      }
    }
  
    // close MongoDB connection
    mongoose.connection.close()
  })
  
  // Populating Cve collection
  async function populateCveCollection () {
    const bar = new ProgressBar('Cve collection [:bar] :percent', {
      complete: '=',
      incomplete: ' ',
      width: 40,
      total: cveData.length
    })
  
    let batchCves = []
    for (let i = 0; i < cveData.length; i++) {
      const cve = cveData[i]
      batchCves.push(cve)
  
      const isLastItem = i === cveData.length - 1
      if (i % 1000 === 0 || isLastItem) {
        await Cve.insertMany(batchCves)
        bar.tick(batchCves.length)
        batchCves = []
      }
    }
  
    // success!
    logger.info('Cve collection populated')
  }
  