lib/mongo.js
function execute (configs, fn, ...args) {
  if ('verbose' in configs && configs.verbose) {
    const isoDate = (new Date()).toISOString()
    console.error(COLOR_FG_YELLOW + '[' + isoDate + '] Start mongo.execute: ' + JSON.stringify([configs, fn, args]) + COLOR_RESET)
  }
  const dbConfigs = util.getDeepCopiedObject(configs)
  const mongoUrl = 'mongoUrl' in dbConfigs ? dbConfigs.mongoUrl : ''
  const dbOption = 'dbOption' in dbConfigs ? dbConfigs.dbOption : {}
  const options = 'options' in dbConfigs ? dbConfigs.options : {}
  const collectionName = 'collectionName' in dbConfigs ? dbConfigs.collectionName : ''
  delete dbConfigs.mongoUrl
  delete dbConfigs.dbOption
  delete dbConfigs.collectionName
  let manager
  // wrap last argument in newCallback
  const callback = args[args.length - 1]
  const newCallback = (error, result) => {
    if ('verbose' in configs && configs.verbose) {
      const isoDate = (new Date()).toISOString()
      console.error(COLOR_FG_YELLOW + '[' + isoDate + '] Finish mongo.execute: ' + JSON.stringify([configs, fn, args]) + COLOR_RESET)
    }
    manager.close()
    callback(error, result)
  }
  args[args.length - 1] = newCallback
  manager = getCompleteManager(mongoUrl, dbOption, options, (error, manager) => {
    if (error) {
      return newCallback(error)
    }
    const collection = getCompleteCollection(manager, collectionName, dbConfigs)
    try {
      return collection[fn](...args)
    } catch (error) {
      return newCallback(error)
    }
  })
}
function execute (configs, fn, ...args) {
  if ('verbose' in configs && configs.verbose) {
    const isoDate = (new Date()).toISOString()
    console.error(COLOR_FG_YELLOW + '[' + isoDate + '] Start mongo.execute: ' + JSON.stringify([configs, fn, args]) + COLOR_RESET)
  }
  const dbConfigs = util.getDeepCopiedObject(configs)
  const mongoUrl = 'mongoUrl' in dbConfigs ? dbConfigs.mongoUrl : ''
  const dbOption = 'dbOption' in dbConfigs ? dbConfigs.dbOption : {}
  const options = 'options' in dbConfigs ? dbConfigs.options : {}
  const collectionName = 'collectionName' in dbConfigs ? dbConfigs.collectionName : ''
  delete dbConfigs.mongoUrl
  delete dbConfigs.dbOption
  delete dbConfigs.collectionName
  let manager
  // wrap last argument in newCallback
  const callback = args[args.length - 1]
  const newCallback = (error, result) => {
    if ('verbose' in configs && configs.verbose) {
      const isoDate = (new Date()).toISOString()
      console.error(COLOR_FG_YELLOW + '[' + isoDate + '] Finish mongo.execute: ' + JSON.stringify([configs, fn, args]) + COLOR_RESET)
    }
    manager.close()
    callback(error, result)
  }
  args[args.length - 1] = newCallback
  manager = getCompleteManager(mongoUrl, dbOption, options, (error, manager) => {
    if (error) {
      return newCallback(error)
    }
    const collection = getCompleteCollection(manager, collectionName, dbConfigs)
    try {
      return collection[fn](...args)
    } catch (error) {
      return newCallback(error)
    }
  })
}
function execute ( configs , fn , ... args ) { if ( 'verbose' in configs && configs . verbose ) { const isoDate = ( new Date ( ) ) . toISOString ( ) console . error ( COLOR_FG_YELLOW + '[' + isoDate + '] Start mongo.execute: ' + JSON . stringify ( [ configs , fn , args ] ) + COLOR_RESET ) } const dbConfigs = util . getDeepCopiedObject ( configs ) const mongoUrl = 'mongoUrl' in dbConfigs ? dbConfigs . mongoUrl : '' const dbOption = 'dbOption' in dbConfigs ? dbConfigs . dbOption : { } const options = 'options' in dbConfigs ? dbConfigs . options : { } const collectionName = 'collectionName' in dbConfigs ? dbConfigs . collectionName : '' delete dbConfigs . mongoUrl delete dbConfigs . dbOption delete dbConfigs . collectionName let manager // wrap last argument in newCallback const callback = args [ args . length - 1 ] const newCallback = ( error , result ) => { if ( 'verbose' in configs && configs . verbose ) { const isoDate = ( new Date ( ) ) . toISOString ( ) console . error ( COLOR_FG_YELLOW + '[' + isoDate + '] Finish mongo.execute: ' + JSON . stringify ( [ configs , fn , args ] ) + COLOR_RESET ) } manager . close ( ) callback ( error , result ) } args [ args . length - 1 ] = newCallback manager = getCompleteManager ( mongoUrl , dbOption , options , ( error , manager ) => { if ( error ) { return newCallback ( error ) } const collection = getCompleteCollection ( manager , collectionName , dbConfigs ) try { return collection [ fn ] ( ... args ) } catch ( error ) { return newCallback ( error ) } } ) }
run something and close the db manager afterward
run something and close the db manager afterward