Skip to content

Commit cc77b72

Browse files
committed
Addressing comments #2
1 parent 8dee3b1 commit cc77b72

File tree

7 files changed

+19
-17
lines changed

7 files changed

+19
-17
lines changed

packages/firestore-types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,13 @@ export class Query {
10541054
*/
10551055
limit(limit: number): Query;
10561056

1057+
/**
1058+
* Creates and returns a new Query where only the last matching documents
1059+
* are returned as results.
1060+
*
1061+
* @param limit The maximum number of items to return.
1062+
* @return The created Query.
1063+
*/
10571064
limitToLast(limit: number): Query;
10581065

10591066
/**

packages/firestore/src/api/database.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,12 +1554,6 @@ export class Query implements firestore.Query {
15541554
limit(n: number): firestore.Query {
15551555
validateExactNumberOfArgs('Query.limit', arguments, 1);
15561556
validateArgType('Query.limit', 'number', 1, n);
1557-
return this.limitToFirst(n);
1558-
}
1559-
1560-
limitToFirst(n: number): firestore.Query {
1561-
validateExactNumberOfArgs('Query.limitToFirst', arguments, 1);
1562-
validateArgType('Query.limitToFirst', 'number', 1, n);
15631557
this.verifyPositiveLimit(n);
15641558
return new Query(this._query.withLimitToFirst(n), this.firestore);
15651559
}
@@ -1857,14 +1851,14 @@ export class Query implements firestore.Query {
18571851
complete: args[currArg + 2] as CompleteFn
18581852
};
18591853
}
1854+
this.validateHasExplicitOrderByForLimitToLast(this._query);
18601855
return this.onSnapshotInternal(options, observer);
18611856
}
18621857

18631858
private onSnapshotInternal(
18641859
options: ListenOptions,
18651860
observer: PartialObserver<firestore.QuerySnapshot>
18661861
): Unsubscribe {
1867-
this.validateHasExplicitOrderByForLimitToLast(this._query);
18681862
let errHandler = (err: Error): void => {
18691863
console.error('Uncaught Error in onSnapshot:', err);
18701864
};
@@ -1897,7 +1891,7 @@ export class Query implements firestore.Query {
18971891
if (query.hasLimitToLast() && query.explicitOrderBy.length === 0) {
18981892
throw new FirestoreError(
18991893
Code.UNIMPLEMENTED,
1900-
'limitToLast() Query without explicit orderBy() is not supported yet.'
1894+
'limitToLast() queries require specifying an orderBy() on at least on document field.'
19011895
);
19021896
}
19031897
}
@@ -1907,6 +1901,7 @@ export class Query implements firestore.Query {
19071901
validateGetOptions('Query.get', options);
19081902
return new Promise(
19091903
(resolve: Resolver<firestore.QuerySnapshot>, reject: Rejecter) => {
1904+
this.validateHasExplicitOrderByForLimitToLast(this._query);
19101905
if (options && options.source === 'cache') {
19111906
this.firestore
19121907
.ensureClientConfigured()

packages/firestore/src/core/query.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class Query {
168168
}
169169

170170
withLimitToLast(limit: number | null): Query {
171-
const q = new Query(
171+
return new Query(
172172
this.path,
173173
this.collectionGroup,
174174
this.explicitOrderBy.slice(),
@@ -178,7 +178,6 @@ export class Query {
178178
this.startAt,
179179
this.endAt
180180
);
181-
return q;
182181
}
183182

184183
withStartAt(bound: Bound): Query {

packages/firestore/src/core/sync_engine.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
218218
viewSnapshot = queryView.view.computeInitialSnapshot();
219219
} else {
220220
const queryData = await this.localStore.allocateQuery(query);
221+
// Make sure no query view is mapped yet. This happens when a 'mirror' query
222+
// is already being listened to.
223+
assert(
224+
!this.queryViewsByTarget[queryData.targetId],
225+
'Not Implemented: Mapping multiple distinct client queries to one backend query.'
226+
);
221227
const status = this.sharedClientState.addLocalQueryTarget(
222228
queryData.targetId
223229
);

packages/firestore/src/local/local_store.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,6 @@ export class LocalStore {
721721
.getQueryData(txn, targetQuery)
722722
.next((cached: QueryData | null) => {
723723
if (cached) {
724-
// Make sure target is not allocated to a mirror query already.
725-
assert(
726-
cached.query.isEqual(query),
727-
'Not Implemented: Mapping multiple distinct client queries to one backend query.'
728-
);
729-
730724
// This query has been listened to previously, so reuse the
731725
// previous targetID.
732726
// TODO(mcg): freshen last accessed date?

packages/firestore/src/local/query_data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export enum QueryPurpose {
3838
/**
3939
* An immutable set of metadata that the local store tracks for each query.
4040
*/
41+
// TODO(wuandy): Rename this to TargetData?
4142
export class QueryData {
4243
constructor(
4344
/** The query being listened to. */

packages/firestore/test/integration/api/query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ apiDescribe('Queries', (persistence: boolean) => {
7171
await expect(
7272
collection.limitToLast(2).get()
7373
).to.be.eventually.rejectedWith(
74-
'limitToLast() Query without explicit orderBy() is not supported yet.'
74+
'limitToLast() queries require specifying an orderBy() on at least on document field.'
7575
);
7676
});
7777
});

0 commit comments

Comments
 (0)