You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					42 lines
				
				1.0 KiB
			
		
		
			
		
	
	
					42 lines
				
				1.0 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								'use strict';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const handleCallback = require('../utils').handleCallback;
							 | 
						||
| 
								 | 
							
								const OperationBase = require('./operation').OperationBase;
							 | 
						||
| 
								 | 
							
								const toError = require('../utils').toError;
							 | 
						||
| 
								 | 
							
								const Aspect = require('./operation').Aspect;
							 | 
						||
| 
								 | 
							
								const defineAspects = require('./operation').defineAspects;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class FindOneOperation extends OperationBase {
							 | 
						||
| 
								 | 
							
								  constructor(collection, query, options) {
							 | 
						||
| 
								 | 
							
								    super(options);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    this.collection = collection;
							 | 
						||
| 
								 | 
							
								    this.query = query;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  execute(callback) {
							 | 
						||
| 
								 | 
							
								    const coll = this.collection;
							 | 
						||
| 
								 | 
							
								    const query = this.query;
							 | 
						||
| 
								 | 
							
								    const options = this.options;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    try {
							 | 
						||
| 
								 | 
							
								      const cursor = coll
							 | 
						||
| 
								 | 
							
								        .find(query, options)
							 | 
						||
| 
								 | 
							
								        .limit(-1)
							 | 
						||
| 
								 | 
							
								        .batchSize(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      // Return the item
							 | 
						||
| 
								 | 
							
								      cursor.next((err, item) => {
							 | 
						||
| 
								 | 
							
								        if (err != null) return handleCallback(callback, toError(err), null);
							 | 
						||
| 
								 | 
							
								        handleCallback(callback, null, item);
							 | 
						||
| 
								 | 
							
								      });
							 | 
						||
| 
								 | 
							
								    } catch (e) {
							 | 
						||
| 
								 | 
							
								      callback(e);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								defineAspects(FindOneOperation, [Aspect.EXPLAINABLE]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								module.exports = FindOneOperation;
							 |