@@ -269,62 +269,71 @@ export function queryNormalizedOrderBy(query: Query): OrderBy[] {
269
269
* Converts this `Query` instance to it's corresponding `Target` representation.
270
270
*/
271
271
export function queryToTarget ( query : Query ) : Target {
272
- return _queryToTarget ( query , queryNormalizedOrderBy ( query ) ) ;
272
+ const queryImpl = debugCast ( query , QueryImpl ) ;
273
+ if ( ! queryImpl . memoizedTarget ) {
274
+ queryImpl . memoizedTarget = _queryToTarget (
275
+ queryImpl ,
276
+ queryNormalizedOrderBy ( query )
277
+ ) ;
278
+ }
279
+
280
+ return queryImpl . memoizedTarget ;
273
281
}
274
282
275
283
/**
276
284
* Converts this `Query` instance to it's corresponding `Target` representation,
277
285
* for use within an aggregate query.
278
286
*/
279
- export function aggregateQueryToTarget ( query : Query ) : Target {
280
- // Do not include implicit order-bys for aggregate queries.
281
- return _queryToTarget ( query , query . explicitOrderBy ) ;
282
- }
283
-
284
- export function _queryToTarget ( query : Query , orderBys : OrderBy [ ] ) : Target {
287
+ export function queryToAggregateTarget ( query : Query ) : Target {
285
288
const queryImpl = debugCast ( query , QueryImpl ) ;
286
- if ( ! queryImpl . memoizedTarget ) {
287
- if ( queryImpl . limitType === LimitType . First ) {
288
- queryImpl . memoizedTarget = newTarget (
289
- queryImpl . path ,
290
- queryImpl . collectionGroup ,
291
- orderBys ,
292
- queryImpl . filters ,
293
- queryImpl . limit ,
294
- queryImpl . startAt ,
295
- queryImpl . endAt
296
- ) ;
297
- } else {
298
- // Flip the orderBy directions since we want the last results
299
- orderBys = orderBys . map ( orderBy => {
300
- const dir =
301
- orderBy . dir === Direction . DESCENDING
302
- ? Direction . ASCENDING
303
- : Direction . DESCENDING ;
304
- return new OrderBy ( orderBy . field , dir ) ;
305
- } ) ;
306
-
307
- // We need to swap the cursors to match the now-flipped query ordering.
308
- const startAt = queryImpl . endAt
309
- ? new Bound ( queryImpl . endAt . position , queryImpl . endAt . inclusive )
310
- : null ;
311
- const endAt = queryImpl . startAt
312
- ? new Bound ( queryImpl . startAt . position , queryImpl . startAt . inclusive )
313
- : null ;
314
-
315
- // Now return as a LimitType.First query.
316
- queryImpl . memoizedTarget = newTarget (
317
- queryImpl . path ,
318
- queryImpl . collectionGroup ,
319
- orderBys ,
320
- queryImpl . filters ,
321
- queryImpl . limit ,
322
- startAt ,
323
- endAt
324
- ) ;
325
- }
289
+
290
+ // Do not include implicit order-bys for aggregate queries.
291
+ return _queryToTarget ( queryImpl , query . explicitOrderBy ) ;
292
+ }
293
+
294
+ export function _queryToTarget (
295
+ queryImpl : QueryImpl ,
296
+ orderBys : OrderBy [ ]
297
+ ) : Target {
298
+ if ( queryImpl . limitType === LimitType . First ) {
299
+ return newTarget (
300
+ queryImpl . path ,
301
+ queryImpl . collectionGroup ,
302
+ orderBys ,
303
+ queryImpl . filters ,
304
+ queryImpl . limit ,
305
+ queryImpl . startAt ,
306
+ queryImpl . endAt
307
+ ) ;
308
+ } else {
309
+ // Flip the orderBy directions since we want the last results
310
+ orderBys = orderBys . map ( orderBy => {
311
+ const dir =
312
+ orderBy . dir === Direction . DESCENDING
313
+ ? Direction . ASCENDING
314
+ : Direction . DESCENDING ;
315
+ return new OrderBy ( orderBy . field , dir ) ;
316
+ } ) ;
317
+
318
+ // We need to swap the cursors to match the now-flipped query ordering.
319
+ const startAt = queryImpl . endAt
320
+ ? new Bound ( queryImpl . endAt . position , queryImpl . endAt . inclusive )
321
+ : null ;
322
+ const endAt = queryImpl . startAt
323
+ ? new Bound ( queryImpl . startAt . position , queryImpl . startAt . inclusive )
324
+ : null ;
325
+
326
+ // Now return as a LimitType.First query.
327
+ return newTarget (
328
+ queryImpl . path ,
329
+ queryImpl . collectionGroup ,
330
+ orderBys ,
331
+ queryImpl . filters ,
332
+ queryImpl . limit ,
333
+ startAt ,
334
+ endAt
335
+ ) ;
326
336
}
327
- return queryImpl . memoizedTarget ! ;
328
337
}
329
338
330
339
export function queryWithAddedFilter ( query : Query , filter : Filter ) : Query {
0 commit comments