@@ -933,9 +933,6 @@ export class Query<
933933
934934 private createImplicitOrderBy ( ignoreInequalityFields = false ) : FieldOrder [ ] {
935935 const fieldOrders = this . _queryOptions . fieldOrders . slice ( ) ;
936- const fieldsNormalized = new Set ( [
937- ...fieldOrders . map ( item => item . field . toString ( ) ) ,
938- ] ) ;
939936
940937 /** The order of the implicit ordering always matches the last explicit order by. */
941938 const lastDirection =
@@ -954,17 +951,16 @@ export class Query<
954951 const inequalityFields = this . getInequalityFilterFields ( ) ;
955952 for ( const field of inequalityFields ) {
956953 if (
957- ! fieldsNormalized . has ( field . toString ( ) ) &&
954+ ! fieldOrders . some ( item => item . field . isEqual ( field ) ) &&
958955 ! field . isEqual ( FieldPath . documentId ( ) )
959956 ) {
960957 fieldOrders . push ( new FieldOrder ( field , lastDirection ) ) ;
961- fieldsNormalized . add ( field . toString ( ) ) ;
962958 }
963959 }
964960 }
965961
966962 // Add the document key field to the last if it is not explicitly ordered.
967- if ( ! fieldsNormalized . has ( FieldPath . documentId ( ) . toString ( ) ) ) {
963+ if ( ! fieldOrders . some ( item => item . field . isEqual ( FieldPath . documentId ( ) ) ) ) {
968964 fieldOrders . push ( new FieldOrder ( FieldPath . documentId ( ) , lastDirection ) ) ;
969965 }
970966
@@ -1475,6 +1471,7 @@ export class Query<
14751471 toProto (
14761472 transactionOrReadTime ?: Uint8Array | Timestamp | api . ITransactionOptions ,
14771473 explainOptions ?: firestore . ExplainOptions ,
1474+ forceImplicitOrderBy ?: boolean ,
14781475 ) : api . IRunQueryRequest {
14791476 const projectId = this . firestore . projectId ;
14801477 const databaseId = this . firestore . databaseId ;
@@ -1483,18 +1480,18 @@ export class Query<
14831480 databaseId ,
14841481 ) ;
14851482
1486- const structuredQuery = this . toStructuredQuery ( ) ;
1483+ const structuredQuery = this . toStructuredQuery ( forceImplicitOrderBy ) ;
14871484
14881485 // For limitToLast queries, the structured query has to be translated to a version with
14891486 // reversed ordered, and flipped startAt/endAt to work properly.
14901487 if ( this . _queryOptions . limitType === LimitType . Last ) {
1491- if ( ! this . _queryOptions . hasFieldOrders ( ) ) {
1492- throw new Error (
1493- 'limitToLast() queries require specifying at least one orderBy() clause.' ,
1494- ) ;
1495- }
1488+ const forceImplicit =
1489+ forceImplicitOrderBy || this . _firestore . alwaysUseImplicitOrderBy ;
1490+ const fieldOrders = forceImplicit
1491+ ? this . createImplicitOrderBy ( )
1492+ : this . _queryOptions . fieldOrders ;
14961493
1497- structuredQuery . orderBy = this . _queryOptions . fieldOrders ! . map ( order => {
1494+ structuredQuery . orderBy = fieldOrders . map ( order => {
14981495 // Flip the orderBy directions since we want the last results
14991496 const dir =
15001497 order . direction === 'DESCENDING' ? 'ASCENDING' : 'DESCENDING' ;
@@ -1564,7 +1561,9 @@ export class Query<
15641561 return bundledQuery ;
15651562 }
15661563
1567- private toStructuredQuery ( ) : api . IStructuredQuery {
1564+ private toStructuredQuery (
1565+ forceImplicitOrderBy ?: boolean ,
1566+ ) : api . IStructuredQuery {
15681567 const structuredQuery : api . IStructuredQuery = {
15691568 from : [ { } ] ,
15701569 } ;
@@ -1586,9 +1585,19 @@ export class Query<
15861585 ) . toProto ( ) ;
15871586 }
15881587
1589- if ( this . _queryOptions . hasFieldOrders ( ) ) {
1590- structuredQuery . orderBy = this . _queryOptions . fieldOrders . map ( o =>
1591- o . toProto ( ) ,
1588+ // orders
1589+ const forceImplicit =
1590+ forceImplicitOrderBy || this . _firestore . alwaysUseImplicitOrderBy ;
1591+ let fieldOrders = this . _queryOptions . fieldOrders ;
1592+ if ( forceImplicit ) {
1593+ fieldOrders = this . createImplicitOrderBy ( ) ;
1594+ }
1595+
1596+ if ( fieldOrders . length > 0 ) {
1597+ structuredQuery . orderBy = fieldOrders . map ( o => o . toProto ( ) ) ;
1598+ } else if ( this . _queryOptions . limitType === LimitType . Last ) {
1599+ throw new Error (
1600+ 'limitToLast() queries require specifying at least one orderBy() clause.' ,
15921601 ) ;
15931602 }
15941603
0 commit comments