-
Notifications
You must be signed in to change notification settings - Fork 943
[2.8/3] Add limitToLast implementation and tests. #2296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
06c79f5
0059aec
8dee3b1
cc77b72
56b76ba
407a2a5
782e08b
8c8026b
2da1cb7
0ccfb75
06b1162
aec8064
72bf7d2
9a00cac
eec17b0
dd99e3c
71e38f7
1608d11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8079,14 +8079,26 @@ declare namespace firebase.firestore { | |
): Query; | ||
|
||
/** | ||
* Creates and returns a new Query where the results are limited to the | ||
* specified number of documents. | ||
* Creates and returns a new Query that only returns the first matching | ||
* documents. | ||
* | ||
* @param limit The maximum number of items to return. | ||
* @return The created Query. | ||
*/ | ||
limit(limit: number): Query; | ||
|
||
/** | ||
* Creates and returns a new Query that only returns the last matching | ||
* documents. | ||
* | ||
* You must specify at least one `orderBy` clause for `limitToLast` queries, | ||
* otherwise an exception will be thrown thrown during execution. | ||
* | ||
* @param limit The maximum number of items to return. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This documentation should mention that the query requires an orderBy() clause. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about:
If you like your version better, we should still make it "an exception will be thrown". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found your version better, as always.. |
||
* @return The created Query. | ||
*/ | ||
limitToLast(limit: number): Query; | ||
|
||
/** | ||
* Creates and returns a new Query that starts at the provided document | ||
* (inclusive). The starting position is relative to the order of the query. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1083,14 +1083,26 @@ export class Query { | |
): Query; | ||
|
||
/** | ||
* Creates and returns a new Query that's additionally limited to only | ||
* return up to the specified number of documents. | ||
* Creates and returns a new Query that only returns the first matching | ||
* documents. | ||
* | ||
* @param limit The maximum number of items to return. | ||
* @return The created Query. | ||
*/ | ||
limit(limit: number): Query; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure that both API definitions/comments are in sync (they seemed to be slightly different even before this PR). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
/** | ||
* Creates and returns a new Query that only returns the last matching | ||
* documents. | ||
* | ||
* Queries with `limitToLast` must have at least one `orderBy` clause on | ||
* one of the document fields, or an Exception will throw during execution. | ||
* | ||
* @param limit The maximum number of items to return. | ||
* @return The created Query. | ||
*/ | ||
limitToLast(limit: number): Query; | ||
|
||
/** | ||
* Creates and returns a new Query that starts at the provided document | ||
* (inclusive). The starting position is relative to the order of the query. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ import { | |
validateOptionalArgType, | ||
validateOptionalArrayElements, | ||
validateOptionNames, | ||
validatePositiveNumber, | ||
validateStringEnum, | ||
valueDescription | ||
} from '../util/input_validation'; | ||
|
@@ -1539,14 +1540,15 @@ export class Query implements firestore.Query { | |
limit(n: number): firestore.Query { | ||
validateExactNumberOfArgs('Query.limit', arguments, 1); | ||
validateArgType('Query.limit', 'number', 1, n); | ||
if (n <= 0) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
`Invalid Query. Query limit (${n}) is invalid. Limit must be ` + | ||
'positive.' | ||
); | ||
} | ||
return new Query(this._query.withLimit(n), this.firestore); | ||
validatePositiveNumber('Query.limit', 1, n); | ||
return new Query(this._query.withLimitToFirst(n), this.firestore); | ||
} | ||
|
||
limitToLast(n: number): firestore.Query { | ||
validateExactNumberOfArgs('Query.limitToLast', arguments, 1); | ||
validateArgType('Query.limitToLast', 'number', 1, n); | ||
validatePositiveNumber('Query.limitToLast', 1, n); | ||
return new Query(this._query.withLimitToLast(n), this.firestore); | ||
} | ||
|
||
startAt( | ||
|
@@ -1826,6 +1828,7 @@ export class Query implements firestore.Query { | |
complete: args[currArg + 2] as CompleteFn | ||
}; | ||
} | ||
this.validateHasExplicitOrderByForLimitToLast(this._query); | ||
return this.onSnapshotInternal(options, observer); | ||
} | ||
|
||
|
@@ -1861,9 +1864,19 @@ export class Query implements firestore.Query { | |
}; | ||
} | ||
|
||
private validateHasExplicitOrderByForLimitToLast(query: InternalQuery): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is certainly up for debate, but at least from an implementation standpoint it would be easier to do this validation in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not aware there was a precedent already, otherwise I'd say the current way is better because it makes user's job easier. I'll raise this in chat to see how others feel about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per our standup discussion, let's stay consistent with the rest of the Query methods (and explore the option to allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan to address this later: b/143499666 and keep this PR as-is for now, is that OK? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that is ok. Please do make sure that this is feasible before we expose limitToLast() publicly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. |
||
if (query.hasLimitToLast() && query.explicitOrderBy.length === 0) { | ||
throw new FirestoreError( | ||
Code.UNIMPLEMENTED, | ||
'limitToLast() queries require specifying at least one orderBy() clause' | ||
); | ||
} | ||
} | ||
|
||
get(options?: firestore.GetOptions): Promise<firestore.QuerySnapshot> { | ||
validateBetweenNumberOfArgs('Query.get', arguments, 0, 1); | ||
validateGetOptions('Query.get', options); | ||
this.validateHasExplicitOrderByForLimitToLast(this._query); | ||
return new Promise( | ||
(resolve: Resolver<firestore.QuerySnapshot>, reject: Rejecter) => { | ||
if (options && options.source === 'cache') { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original API proposal suggests to make this method as deprecated. Should we do that and introduce
limitToFirst()
? In that case, I would also leave the comments forlimit
as they were.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the decision was to keep
limit
and addlimitToLast
only.See the comments in go/limitToLast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Do you mind updating the proposed API so this is immediately apparent? The API proposal should be the source of truth and we should in general update the proposal to match the decision from the API council.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made some edit suggestions there.