1- import { CommandExecutor , Command , ExecutionResult , CursorResult } from '../interfaces' ;
1+ import {
2+ CommandExecutor ,
3+ Command ,
4+ ExecutionResult ,
5+ CursorResult ,
6+ CursorOptions ,
7+ } from '../interfaces' ;
28import { Document , MongoClient , ObjectId } from 'mongodb' ;
39
410/**
@@ -47,28 +53,32 @@ export class MongoExecutor implements CommandExecutor {
4753 for ( const command of commands ) {
4854 switch ( command . type ) {
4955 case 'FIND' :
50- const findCursor = database
51- . collection ( command . collection )
52- . find ( this . convertObjectIds ( command . filter || { } ) ) ;
56+ // Prepare find options
57+ const findOptions : any = { } ;
5358
5459 // Apply projection if specified
5560 if ( command . projection ) {
56- findCursor . project ( command . projection ) ;
61+ findOptions . projection = command . projection ;
5762 }
5863
5964 // Apply sorting if specified
6065 if ( command . sort ) {
61- findCursor . sort ( command . sort ) ;
66+ findOptions . sort = command . sort ;
6267 }
6368
6469 // Apply pagination if specified
6570 if ( command . skip ) {
66- findCursor . skip ( command . skip ) ;
71+ findOptions . skip = command . skip ;
6772 }
6873 if ( command . limit && command . limit > 0 ) {
69- findCursor . limit ( command . limit ) ;
74+ findOptions . limit = command . limit ;
7075 }
7176
77+ // Create the cursor with all options at once
78+ const findCursor = database
79+ . collection ( command . collection )
80+ . find ( this . convertObjectIds ( command . filter || { } ) , findOptions ) ;
81+
7282 // Always return array for the regular execute
7383 result = await findCursor . toArray ( ) ;
7484 break ;
@@ -97,7 +107,14 @@ export class MongoExecutor implements CommandExecutor {
97107 case 'AGGREGATE' :
98108 // Handle aggregation commands
99109 const pipeline = command . pipeline . map ( ( stage ) => this . convertObjectIds ( stage ) ) ;
100- const aggregateCursor = database . collection ( command . collection ) . aggregate ( pipeline ) ;
110+
111+ // Prepare aggregation options
112+ const aggregateOptions : any = { } ;
113+
114+ // Create the cursor with options
115+ const aggregateCursor = database
116+ . collection ( command . collection )
117+ . aggregate ( pipeline , aggregateOptions ) ;
101118
102119 // Always return array for the regular execute
103120 result = await aggregateCursor . toArray ( ) ;
@@ -114,10 +131,14 @@ export class MongoExecutor implements CommandExecutor {
114131 /**
115132 * Execute a series of MongoDB commands and return cursors
116133 * @param commands Array of commands to execute
134+ * @param options Options for cursor execution
117135 * @returns Cursor for FIND and AGGREGATE commands, null for other commands
118136 * @typeParam T - The type of documents that will be returned (defaults to Document)
119137 */
120- async executeCursor < T = Document > ( commands : Command [ ] ) : Promise < CursorResult < T > > {
138+ async executeCursor < T = Document > (
139+ commands : Command [ ] ,
140+ options ?: CursorOptions
141+ ) : Promise < CursorResult < T > > {
121142 // We assume the client is already connected
122143 const database = this . client . db ( this . dbName ) ;
123144
@@ -128,36 +149,55 @@ export class MongoExecutor implements CommandExecutor {
128149 for ( const command of commands ) {
129150 switch ( command . type ) {
130151 case 'FIND' :
131- const findCursor = database
132- . collection ( command . collection )
133- . find ( this . convertObjectIds ( command . filter || { } ) ) ;
152+ // Prepare find options
153+ const findOptions : any = { } ;
134154
135155 // Apply projection if specified
136156 if ( command . projection ) {
137- findCursor . project ( command . projection ) ;
157+ findOptions . projection = command . projection ;
138158 }
139159
140160 // Apply sorting if specified
141161 if ( command . sort ) {
142- findCursor . sort ( command . sort ) ;
162+ findOptions . sort = command . sort ;
143163 }
144164
145165 // Apply pagination if specified
146166 if ( command . skip ) {
147- findCursor . skip ( command . skip ) ;
167+ findOptions . skip = command . skip ;
148168 }
149169 if ( command . limit && command . limit > 0 ) {
150- findCursor . limit ( command . limit ) ;
170+ findOptions . limit = command . limit ;
151171 }
152172
173+ // Apply batch size from options
174+ if ( options ?. batchSize ) {
175+ findOptions . batchSize = options . batchSize ;
176+ }
177+
178+ // Create the cursor with all options at once
179+ const findCursor = database
180+ . collection ( command . collection )
181+ . find ( this . convertObjectIds ( command . filter || { } ) , findOptions ) ;
182+
153183 // Return the cursor directly
154184 result = findCursor ;
155185 break ;
156186
157187 case 'AGGREGATE' :
158188 // Handle aggregation commands
159189 const pipeline = command . pipeline . map ( ( stage ) => this . convertObjectIds ( stage ) ) ;
160- result = database . collection ( command . collection ) . aggregate ( pipeline ) ;
190+
191+ // Prepare aggregation options
192+ const aggregateOptions : any = { } ;
193+
194+ // Apply batch size from options
195+ if ( options ?. batchSize ) {
196+ aggregateOptions . batchSize = options . batchSize ;
197+ }
198+
199+ // Create the cursor with options
200+ result = database . collection ( command . collection ) . aggregate ( pipeline , aggregateOptions ) ;
161201 break ;
162202
163203 case 'INSERT' :
0 commit comments