@@ -21,28 +21,26 @@ export class FirestoreApi {
21
21
printer = new PrettyPrint ( ) ;
22
22
23
23
/**
24
- * Process indexes by filtering out implicit __name__ fields with ASCENDING order.
25
- * Keeps explicit __name__ fields with DESCENDING order.
26
- * @param indexes Array of indexes to process
27
- * @return Processed array of indexes with filtered fields
24
+ * Process indexes by appending the implicit __name__ fields with default order for STANDARD edition database.
25
+ * No-op if exists __name__ field at the end.
26
+ * No-op is ENTERPRISE edition databases.
27
+ * @param index Spec index to process
28
+ * @return Processed spec index with potential additional __name__ suffix
28
29
*/
29
- public static processIndexes ( indexes : types . Index [ ] ) : types . Index [ ] {
30
- return indexes . map ( ( index : types . Index ) : types . Index => {
31
- // Per https://firebase.google.com/docs/firestore/query-data/index-overview#default_ordering_and_the_name_field
32
- // this matches the direction of the last non-name field in the index.
33
- let fields = index . fields ;
34
- const lastField = index . fields ?. [ index . fields . length - 1 ] ;
35
- if ( lastField ?. fieldPath === "__name__" ) {
36
- const defaultDirection = index . fields ?. [ index . fields . length - 2 ] ?. order ;
37
- if ( lastField ?. order === defaultDirection ) {
38
- fields = fields . slice ( 0 , - 1 ) ;
39
- }
40
- }
41
- return {
42
- ...index ,
43
- fields,
44
- } ;
45
- } ) ;
30
+ public static processIndex ( index : Spec . Index ) : Spec . Index {
31
+ // Per https://firebase.google.com/docs/firestore/query-data/index-overview#default_ordering_and_the_name_field
32
+ // this matches the direction of the last non-name field in the index.
33
+ const fields = index . fields ;
34
+ const lastField = index . fields ?. [ index . fields . length - 1 ] ;
35
+ if ( lastField ?. fieldPath !== "__name__" ) {
36
+ const defaultDirection = index . fields ?. [ index . fields . length - 1 ] ?. order ;
37
+ const nameSuffix = { fieldPath : "__name__" , order : defaultDirection } as types . IndexField ;
38
+ fields . push ( nameSuffix ) ;
39
+ }
40
+ return {
41
+ ...index ,
42
+ fields,
43
+ } ;
46
44
}
47
45
48
46
/**
@@ -212,7 +210,7 @@ export class FirestoreApi {
212
210
return [ ] ;
213
211
}
214
212
215
- return FirestoreApi . processIndexes ( indexes ) ;
213
+ return indexes ;
216
214
}
217
215
218
216
/**
@@ -559,14 +557,19 @@ export class FirestoreApi {
559
557
560
558
// TODO(b/439901837): Compare `unique` index configuration when it's supported.
561
559
562
- if ( index . fields . length !== spec . fields . length ) {
560
+ let specIdx = spec ;
561
+ if ( edition === DatabaseEdition . STANDARD ) {
562
+ specIdx = FirestoreApi . processIndex ( specIdx ) ;
563
+ }
564
+
565
+ if ( index . fields . length !== specIdx . fields . length ) {
563
566
return false ;
564
567
}
565
568
566
569
let i = 0 ;
567
570
while ( i < index . fields . length ) {
568
571
const iField = index . fields [ i ] ;
569
- const sField = spec . fields [ i ] ;
572
+ const sField = specIdx . fields [ i ] ;
570
573
571
574
if ( iField . fieldPath !== sField . fieldPath ) {
572
575
return false ;
0 commit comments