nl_tokens: Gets a connection and runs a mongo command and returns the results
pl_tokens: function ( command , collection , options , callback ) { var args = this . _makeParamArgs ( arguments ) , sort = null , callback = args [ 2 ] ; log . sql . apply ( null , arguments ) ; if ( options && typeof ( options ) === 'object' ) { if ( options . sort ) sort = options . sort ; } if ( this . internals . dryRun ) { return Promise . resolve ( ) . nodeify ( callback ) ; } return new Promise ( function ( resolve , reject ) { var prCB = function ( err , data ) { return ( err ? reject ( err ) : resolve ( data ) ) ; } ; // Get a connection to mongo this . connection . connect ( this . connectionString , function ( err , db ) { if ( err ) { prCB ( err ) ; } // Callback function to return mongo records var callbackFunction = function ( err , data ) { if ( err ) { prCB ( err ) ; } prCB ( null , data ) ; db . close ( ) ; } ; // Depending on the command, we need to use different mongo methods <mask> ( command ) { case 'find' : if ( sort ) { db . collection ( collection ) [ command ] ( options . query ) . sort ( sort ) . toArray ( callbackFunction ) ; } else { db . collection ( collection ) [ command ] ( options ) . toArray ( callbackFunction ) ; } break ; case 'renameCollection' : db [ command ] ( collection , options . newCollection , callbackFunction ) ; break ; case 'createIndex' : db [ command ] ( collection , options . columns , { name : options . indexName , unique : options . unique } , callbackFunction ) ; break ; case 'dropIndex' : db . collection ( collection ) [ command ] ( options . indexName , callbackFunction ) ; break ; case 'insert' : // options is the records to insert in this case if ( util . isArray ( options ) ) db . collection ( collection ) . insertMany ( options , { } , callbackFunction ) ; else db . collection ( collection ) . insertOne ( options , { } , callbackFunction ) ; break ; case 'remove' : // options is the records to insert in this case if ( util . isArray ( options ) ) db . collection ( collection ) . deleteMany ( options , callbackFunction ) ; else db . collection ( collection ) . deleteOne ( options , callbackFunction ) ; break ; case 'collections' : db . collections ( callbackFunction ) ; break ; case 'indexInformation' : db . indexInformation ( collection , callbackFunction ) ; break ; case 'dropDatabase' : db . dropDatabase ( callbackFunction ) ; break ; case 'update' : db . collection ( collection ) [ command ] ( options . query , options . update , options . options , callbackFunction ) ; break ; case 'updateMany' : db . collection ( collection ) [ command ] ( options . query , options . update , options . options , callbackFunction ) ; break ; case 'getDbInstance' : prCB ( null , db ) ; // When the user wants to get the DB instance we need to return the promise callback, so the DB connection is not automatically closed break ; default : db [ command ] ( collection , callbackFunction ) ; break ; } } ) ; } . bind ( this ) ) . nodeify ( callback ) ; }