The application of async Library in Mongoose

Time:2020-6-2

Async library is a super library dealing with asynchronous operations on NPM, which can realize many asynchronous operations and process control. Although we can use promise or async / await syntax to implement some simple asynchronous control, it will be difficult to handle when there are many asynchronous processes.

This paper mainly introduces async in the node environment with mongoose to realize database paging and async with mongoose to query the daily data for N consecutive days.

Installing Async

npm i async

Official documents

Two APIs of asyn

Parallel (tasks, callback)

parallelRun a collection of tasks for a function without waiting for the previous function to complete. If any function passes an error to its callback, the main callback is called immediately with the wrong value. When the task is complete, the result is passed as an array or object to the final callback.

example:

async.parallel({
    one: function(callback) {
        setTimeout(function() {
            callback(null, 1);
        }, 200);
    },
    two: function(callback) {
        setTimeout(function() {
            callback(null, 2);
        }, 100);
    }
}, function(err, results) {
    // results is now equals to: {one: 1, two: 2}
});

Series (tasks, callback) serial

successivelyRun the functions in the task collection, each of which runs after the previous function is completed. If any function in the series passes an error to its callback, no more functions are run and the callback is called immediately with the wrong value. Otherwise, when the task completes, the callback receives an array of results.
It can also be usedobjectNot an array. Each property runs as a function, and the result is passed to the final callback as an object rather than an array. This is the process async.series A more readable method of results.

example:

async.series({
    one: function(callback) {
        setTimeout(function() {
            callback(null, 1);
        }, 200);
    },
    two: function(callback){
        setTimeout(function() {
            callback(null, 2);
        }, 100);
    }
}, function(err, results) {
    // results is now equal to: {one: 1, two: 2}
});

Knowing the two key APIs above, we can complete the application of some functions.

paging

const mongoose = require('mongoose')
const async = require('async')
/**Pagination
 * page, pageSize, Model, populate = '', queryParams = {}, sortParams
 * @param {*} options
 */
exports.queryPagination = (options = {}, callback) => {
  let {
    page,
    pageSize,
    Model,
    populate = '',
    queryParams = {},
    sortParams,
    field = ''
  } = options
  let start = (page - 1) * pageSize
  let $page = {
    pageNumber: page
  }

  async.parallel(
    {
      count: function(cb) {
        Model.countDocuments(queryParams).exec(cb)
      },
      data: function(cb) {
        Model.find(queryParams)
          .skip(start)
          .limit(pageSize)
          .populate(populate)
          .sort(sortParams)
          .select(field)
          .exec(cb)
      }
    },
    function(err, results) {
      let count = results.count
      $page.count = count
      $page.pageTotal = Math.ceil(count / pageSize)
      $page.data = results.data
      callback(err, $page)
    }
  )
}

Query daily data for N consecutive days

For example, query the access trend in the last 7 days

/**
 *7-day visit trend
 */
exports.dayViewsController = (req, res) => {
  //Get date of last 7 days
  const days = get7day()
  // async.series Object parameters for
  const parallelQuerys = {}

  days.forEach((day) => {
    let startDate = new Date(day).getTime()
    let lastDate = new Date(
      moment(startDate).add(1, 'days').format('YYYY-MM-DD')
    ).getTime()

    parallelQuerys[String(day)] = function (cb) {
      Vistor.countDocuments({
        browse_time_stamp: {
          $gte: startDate,
          $lt: lastDate,
        },
      }).exec(cb)
    }
  })

  async.series(parallelQuerys, function (err, results) {
    if (err) {
      responseError({ err, res })
    } else {
      responseSuccess({ res, data: results })
    }
  })
}

Summary:

For asynchronous programming model, it is very easy to do things in synchronous programming, but now it becomes very troublesome. Async process control greatly simplifies a lot of complex operations, and using async library to write database query process is also very simple.

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]