Skip to content

Commit 77d3d06

Browse files
New Query API
1 parent 28438f6 commit 77d3d06

38 files changed

+1469
-1081
lines changed

packages/firestore/exp/index.d.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,28 +273,50 @@ export type WhereFilterOp =
273273
export class Query<T = DocumentData> {
274274
protected constructor();
275275
readonly firestore: FirebaseFirestore;
276-
where(
277-
fieldPath: string | FieldPath,
278-
opStr: WhereFilterOp,
279-
value: any
280-
): Query<T>;
281-
orderBy(
282-
fieldPath: string | FieldPath,
283-
directionStr?: OrderByDirection
284-
): Query<T>;
285-
limit(limit: number): Query<T>;
286-
limitToLast(limit: number): Query<T>;
287-
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
288-
startAt(...fieldValues: any[]): Query<T>;
289-
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
290-
startAfter(...fieldValues: any[]): Query<T>;
291-
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
292-
endBefore(...fieldValues: any[]): Query<T>;
293-
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
294-
endAt(...fieldValues: any[]): Query<T>;
276+
295277
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
296278
}
297279

280+
export type QueryConstraintType =
281+
| 'where'
282+
| 'orderBy'
283+
| 'limit'
284+
| 'limitToLast'
285+
| 'startAt'
286+
| 'startAfter'
287+
| 'endAt'
288+
| 'endBefore';
289+
290+
export class QueryConstraint {
291+
private constructor();
292+
readonly type: QueryConstraintType;
293+
}
294+
295+
export function query<T>(
296+
query: CollectionReference<T> | Query<T>,
297+
...constraints: QueryConstraint[]
298+
): Query<T>;
299+
300+
export function where(
301+
fieldPath: string | FieldPath,
302+
opStr: WhereFilterOp,
303+
value: any
304+
): QueryConstraint;
305+
export function orderBy(
306+
fieldPath: string | FieldPath,
307+
directionStr?: OrderByDirection
308+
): QueryConstraint;
309+
export function limit(limit: number): QueryConstraint;
310+
export function limitToLast(limit: number): QueryConstraint;
311+
export function startAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
312+
export function startAt(...fieldValues: any[]): QueryConstraint;
313+
export function startAfter(snapshot: DocumentSnapshot<any>): QueryConstraint;
314+
export function startAfter(...fieldValues: any[]): QueryConstraint;
315+
export function endBefore(snapshot: DocumentSnapshot<any>): QueryConstraint;
316+
export function endBefore(...fieldValues: any[]): QueryConstraint;
317+
export function endAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
318+
export function endAt(...fieldValues: any[]): QueryConstraint;
319+
298320
export class QuerySnapshot<T = DocumentData> {
299321
private constructor();
300322
readonly query: Query<T>;

packages/firestore/exp/index.node.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ export { SnapshotMetadata } from '../src/api/database';
4747
export {
4848
DocumentReference,
4949
CollectionReference,
50+
QueryConstraint,
5051
Query,
5152
doc,
5253
collection,
5354
collectionGroup,
54-
parent
55+
parent,
56+
startAt,
57+
startAfter,
58+
endAt,
59+
endBefore,
60+
query,
61+
limit,
62+
limitToLast,
63+
where,
64+
orderBy
5565
} from '../lite/src/api/reference';
5666

5767
export { runTransaction, Transaction } from './src/api/transaction';

packages/firestore/exp/test/shim.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
onSnapshot,
4646
onSnapshotsInSync,
4747
parent,
48+
query,
4849
queryEqual,
4950
refEqual,
5051
runTransaction,
@@ -54,7 +55,15 @@ import {
5455
terminate,
5556
updateDoc,
5657
waitForPendingWrites,
57-
writeBatch
58+
writeBatch,
59+
limit,
60+
where,
61+
orderBy,
62+
startAt,
63+
startAfter,
64+
endAt,
65+
endBefore,
66+
limitToLast
5867
} from '../../exp/index.node';
5968
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
6069
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
@@ -265,7 +274,9 @@ export class DocumentReference<T = legacy.DocumentData>
265274
readonly path = this._delegate.path;
266275

267276
get parent(): legacy.CollectionReference<T> {
268-
return new CollectionReference<T>(parent(this._delegate));
277+
// TODO(firestoreexp): This bang should be removed one CollectionReference
278+
// has a `type` property.
279+
return new CollectionReference<T>(parent(this._delegate)!);
269280
}
270281

271282
collection(
@@ -417,7 +428,7 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
417428
value: any
418429
): Query<T> {
419430
return new Query<T>(
420-
this._delegate.where(unwrap(fieldPath), opStr, unwrap(value))
431+
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
421432
);
422433
}
423434

@@ -426,48 +437,32 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
426437
directionStr?: legacy.OrderByDirection
427438
): Query<T> {
428439
return new Query<T>(
429-
this._delegate.orderBy(unwrap(fieldPath), directionStr)
440+
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
430441
);
431442
}
432443

433-
limit(limit: number): Query<T> {
434-
return new Query<T>(this._delegate.limit(limit));
444+
limit(n: number): Query<T> {
445+
return new Query<T>(query(this._delegate, limit(n)));
435446
}
436447

437-
limitToLast(limit: number): Query<T> {
438-
return new Query<T>(this._delegate.limitToLast(limit));
448+
limitToLast(n: number): Query<T> {
449+
return new Query<T>(query(this._delegate, limitToLast(n)));
439450
}
440451

441452
startAt(...args: any[]): Query<T> {
442-
if (args[0] instanceof DocumentSnapshot) {
443-
return new Query(this._delegate.startAt(args[0]._delegate));
444-
} else {
445-
return new Query(this._delegate.startAt(...unwrap(args)));
446-
}
453+
return new Query(query(this._delegate, startAt(...unwrap(args))));
447454
}
448455

449456
startAfter(...args: any[]): Query<T> {
450-
if (args[0] instanceof DocumentSnapshot) {
451-
return new Query(this._delegate.startAfter(args[0]._delegate));
452-
} else {
453-
return new Query(this._delegate.startAfter(...unwrap(args)));
454-
}
457+
return new Query(query(this._delegate, startAfter(...unwrap(args))));
455458
}
456459

457460
endBefore(...args: any[]): Query<T> {
458-
if (args[0] instanceof DocumentSnapshot) {
459-
return new Query(this._delegate.endBefore(args[0]._delegate));
460-
} else {
461-
return new Query(this._delegate.endBefore(...unwrap(args)));
462-
}
461+
return new Query(query(this._delegate, endBefore(...unwrap(args))));
463462
}
464463

465464
endAt(...args: any[]): Query<T> {
466-
if (args[0] instanceof DocumentSnapshot) {
467-
return new Query(this._delegate.endAt(args[0]._delegate));
468-
} else {
469-
return new Query(this._delegate.endAt(...unwrap(args)));
470-
}
465+
return new Query(query(this._delegate, endAt(...unwrap(args))));
471466
}
472467

473468
isEqual(other: legacy.Query<T>): boolean {

packages/firestore/lite/index.d.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -228,28 +228,50 @@ export type WhereFilterOp =
228228
export class Query<T = DocumentData> {
229229
protected constructor();
230230
readonly firestore: FirebaseFirestore;
231-
where(
232-
fieldPath: string | FieldPath,
233-
opStr: WhereFilterOp,
234-
value: any
235-
): Query<T>;
236-
orderBy(
237-
fieldPath: string | FieldPath,
238-
directionStr?: OrderByDirection
239-
): Query<T>;
240-
limit(limit: number): Query<T>;
241-
limitToLast(limit: number): Query<T>;
242-
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
243-
startAt(...fieldValues: any[]): Query<T>;
244-
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
245-
startAfter(...fieldValues: any[]): Query<T>;
246-
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
247-
endBefore(...fieldValues: any[]): Query<T>;
248-
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
249-
endAt(...fieldValues: any[]): Query<T>;
231+
250232
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
251233
}
252234

235+
export type QueryConstraintType =
236+
| 'where'
237+
| 'orderBy'
238+
| 'limit'
239+
| 'limitToLast'
240+
| 'startAt'
241+
| 'startAfter'
242+
| 'endAt'
243+
| 'endBefore';
244+
245+
export class QueryConstraint {
246+
private constructor();
247+
readonly type: QueryConstraintType;
248+
}
249+
250+
export function query<T>(
251+
query: CollectionReference<T> | Query<T>,
252+
...constraints: QueryConstraint[]
253+
): Query<T>;
254+
255+
export function where(
256+
fieldPath: string | FieldPath,
257+
opStr: WhereFilterOp,
258+
value: any
259+
): QueryConstraint;
260+
export function orderBy(
261+
fieldPath: string | FieldPath,
262+
directionStr?: OrderByDirection
263+
): QueryConstraint;
264+
export function limit(limit: number): QueryConstraint;
265+
export function limitToLast(limit: number): QueryConstraint;
266+
export function startAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
267+
export function startAt(...fieldValues: any[]): QueryConstraint;
268+
export function startAfter(snapshot: DocumentSnapshot<any>): QueryConstraint;
269+
export function startAfter(...fieldValues: any[]): QueryConstraint;
270+
export function endBefore(snapshot: DocumentSnapshot<any>): QueryConstraint;
271+
export function endBefore(...fieldValues: any[]): QueryConstraint;
272+
export function endAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
273+
export function endAt(...fieldValues: any[]): QueryConstraint;
274+
253275
export class QuerySnapshot<T = DocumentData> {
254276
private constructor();
255277
readonly query: Query<T>;

packages/firestore/lite/index.node.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {
3030
export {
3131
DocumentReference,
3232
Query,
33+
QueryConstraint,
3334
CollectionReference,
3435
collection,
3536
collectionGroup,
@@ -42,7 +43,16 @@ export {
4243
updateDoc,
4344
addDoc,
4445
refEqual,
45-
queryEqual
46+
queryEqual,
47+
startAt,
48+
startAfter,
49+
endAt,
50+
endBefore,
51+
query,
52+
limit,
53+
limitToLast,
54+
where,
55+
orderBy
4656
} from './src/api/reference';
4757

4858
// TOOD(firestorelite): Add tests when Queries are usable

0 commit comments

Comments
 (0)