@@ -137,54 +137,39 @@ function isSingleIndexTuple(t: unknown): t is [string, IndexDirection] {
137137 return Array . isArray ( t ) && t . length === 2 && isIndexDirection ( t [ 1 ] ) ;
138138}
139139
140- function makeIndexSpec ( indexSpec : IndexSpecification , options : any ) : IndexDescription {
141- function getFieldHash ( indexSpec : IndexSpecification ) {
142- const fieldHash : Map < string , IndexDirection > = new Map ( ) ;
143-
144- let indexSpecArr : IndexSpecification [ ] ;
145-
146- // Wrap indexSpec in array if needed
147- if ( ! Array . isArray ( indexSpec ) || isSingleIndexTuple ( indexSpec ) ) {
148- indexSpecArr = [ indexSpec ] ;
149- } else {
150- indexSpecArr = indexSpec ;
151- }
152-
153- // Iterate through array and handle different types
154- for ( const spec of indexSpecArr ) {
155- if ( 'string' === typeof spec ) {
156- fieldHash . set ( spec , 1 ) ;
157- } else if ( Array . isArray ( spec ) ) {
158- fieldHash . set ( spec [ 0 ] , spec [ 1 ] ?? 1 ) ;
159- } else if ( spec instanceof Map ) {
160- for ( const [ key , value ] of spec ) {
161- fieldHash . set ( key , value ) ;
162- }
163- } else if ( isObject ( spec ) ) {
164- for ( const [ key , value ] of Object . entries ( spec ) ) {
165- fieldHash . set ( key , value ) ;
166- }
140+ function makeIndexSpec (
141+ indexSpec : IndexSpecification ,
142+ options ?: CreateIndexesOptions
143+ ) : IndexDescription {
144+ const key : Map < string , IndexDirection > = new Map ( ) ;
145+
146+ const indexSpecs =
147+ ! Array . isArray ( indexSpec ) || isSingleIndexTuple ( indexSpec ) ? [ indexSpec ] : indexSpec ;
148+
149+ // Iterate through array and handle different types
150+ for ( const spec of indexSpecs ) {
151+ if ( typeof spec === 'string' ) {
152+ key . set ( spec , 1 ) ;
153+ } else if ( Array . isArray ( spec ) ) {
154+ key . set ( spec [ 0 ] , spec [ 1 ] ?? 1 ) ;
155+ } else if ( spec instanceof Map ) {
156+ for ( const [ property , value ] of spec ) {
157+ key . set ( property , value ) ;
158+ }
159+ } else if ( isObject ( spec ) ) {
160+ for ( const [ property , value ] of Object . entries ( spec ) ) {
161+ key . set ( property , value ) ;
167162 }
168163 }
169- return fieldHash ;
170164 }
171165
172- const fieldHash = getFieldHash ( indexSpec ) ;
173-
174- // Generate the index name
175- const name = typeof options . name === 'string' ? options . name : null ;
176-
177166 // Set up the index
178- const finalIndexSpec : Document = { name, key : fieldHash } ;
179-
180- // merge valid index options into the index spec
181- for ( const optionName in options ) {
182- if ( VALID_INDEX_OPTIONS . has ( optionName ) ) {
183- finalIndexSpec [ optionName ] = options [ optionName ] ;
184- }
185- }
186-
187- return finalIndexSpec as IndexDescription ;
167+ return {
168+ ...Object . fromEntries (
169+ Object . entries ( { ...options } ) . filter ( ( [ optionName ] ) => VALID_INDEX_OPTIONS . has ( optionName ) )
170+ ) ,
171+ key
172+ } ;
188173}
189174
190175/** @internal */
@@ -235,27 +220,24 @@ export class CreateIndexesOperation<
235220 this . collectionName = collectionName ;
236221
237222 // Ensure we generate the correct name if the parameter is not set
238- const normalizedIndexes = [ ] ;
223+ const namedIndexes = [ ] ;
239224 for ( const userIndex of indexes ) {
240- const key =
241- userIndex . key instanceof Map ? userIndex . key : new Map ( Object . entries ( userIndex . key ) ) ;
242225 const index : Omit < IndexDescription , 'key' > & { key : Map < string , IndexDirection > } = {
243226 ...userIndex ,
244- key
227+ // Ensure the key is a Map to preserve index key ordering
228+ key : userIndex . key instanceof Map ? userIndex . key : new Map ( Object . entries ( userIndex . key ) )
245229 } ;
246230 if ( index . name == null ) {
231+ // Ensure every index is named
247232 const keys = [ ] ;
248-
249233 for ( const [ name , direction ] of index . key ) {
250234 keys . push ( `${ name } _${ direction } ` ) ;
251235 }
252-
253- // Set the name
254236 index . name = keys . join ( '_' ) ;
255237 }
256- normalizedIndexes . push ( index ) ;
238+ namedIndexes . push ( index ) ;
257239 }
258- this . indexes = normalizedIndexes ;
240+ this . indexes = namedIndexes ;
259241 }
260242
261243 override execute (
@@ -318,11 +300,6 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
318300 indexSpec : IndexSpecification ,
319301 options ?: CreateIndexesOptions
320302 ) {
321- // createIndex can be called with a variety of styles:
322- // coll.createIndex('a');
323- // coll.createIndex({ a: 1 });
324- // coll.createIndex([['a', 1]]);
325- // createIndexes is always called with an array of index spec objects
326303 super ( parent , collectionName , [ makeIndexSpec ( indexSpec , options ) ] , options ) ;
327304 }
328305 override execute (
0 commit comments