@@ -137,54 +137,39 @@ function isSingleIndexTuple(t: unknown): t is [string, IndexDirection] {
137
137
return Array . isArray ( t ) && t . length === 2 && isIndexDirection ( t [ 1 ] ) ;
138
138
}
139
139
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 ) ;
167
162
}
168
163
}
169
- return fieldHash ;
170
164
}
171
165
172
- const fieldHash = getFieldHash ( indexSpec ) ;
173
-
174
- // Generate the index name
175
- const name = typeof options . name === 'string' ? options . name : null ;
176
-
177
166
// 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
+ } ;
188
173
}
189
174
190
175
/** @internal */
@@ -235,27 +220,24 @@ export class CreateIndexesOperation<
235
220
this . collectionName = collectionName ;
236
221
237
222
// Ensure we generate the correct name if the parameter is not set
238
- const normalizedIndexes = [ ] ;
223
+ const namedIndexes = [ ] ;
239
224
for ( const userIndex of indexes ) {
240
- const key =
241
- userIndex . key instanceof Map ? userIndex . key : new Map ( Object . entries ( userIndex . key ) ) ;
242
225
const index : Omit < IndexDescription , 'key' > & { key : Map < string , IndexDirection > } = {
243
226
...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 ) )
245
229
} ;
246
230
if ( index . name == null ) {
231
+ // Ensure every index is named
247
232
const keys = [ ] ;
248
-
249
233
for ( const [ name , direction ] of index . key ) {
250
234
keys . push ( `${ name } _${ direction } ` ) ;
251
235
}
252
-
253
- // Set the name
254
236
index . name = keys . join ( '_' ) ;
255
237
}
256
- normalizedIndexes . push ( index ) ;
238
+ namedIndexes . push ( index ) ;
257
239
}
258
- this . indexes = normalizedIndexes ;
240
+ this . indexes = namedIndexes ;
259
241
}
260
242
261
243
override execute (
@@ -318,11 +300,6 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
318
300
indexSpec : IndexSpecification ,
319
301
options ?: CreateIndexesOptions
320
302
) {
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
326
303
super ( parent , collectionName , [ makeIndexSpec ( indexSpec , options ) ] , options ) ;
327
304
}
328
305
override execute (
0 commit comments