Skip to content

New Query API #3390

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

Merged
merged 10 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions packages/firestore/exp-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,28 +284,50 @@ export class Query<T = DocumentData> {
protected constructor();
readonly type: 'query' | 'collection';
readonly firestore: FirebaseFirestore;
where(
fieldPath: string | FieldPath,
opStr: WhereFilterOp,
value: any
): Query<T>;
orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection
): Query<T>;
limit(limit: number): Query<T>;
limitToLast(limit: number): Query<T>;
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
startAt(...fieldValues: any[]): Query<T>;
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
startAfter(...fieldValues: any[]): Query<T>;
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
endBefore(...fieldValues: any[]): Query<T>;
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
endAt(...fieldValues: any[]): Query<T>;

withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
}

export type QueryConstraintType =
| 'where'
| 'orderBy'
| 'limit'
| 'limitToLast'
| 'startAt'
| 'startAfter'
| 'endAt'
| 'endBefore';

export class QueryConstraint {
private constructor();
readonly type: QueryConstraintType;
}

export function query<T>(
query: CollectionReference<T> | Query<T>,
...constraints: QueryConstraint[]
): Query<T>;

export function where(
fieldPath: string | FieldPath,
opStr: WhereFilterOp,
value: any
): QueryConstraint;
export function orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection
): QueryConstraint;
export function limit(limit: number): QueryConstraint;
export function limitToLast(limit: number): QueryConstraint;
export function startAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function startAt(...fieldValues: any[]): QueryConstraint;
export function startAfter(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function startAfter(...fieldValues: any[]): QueryConstraint;
export function endBefore(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function endBefore(...fieldValues: any[]): QueryConstraint;
export function endAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function endAt(...fieldValues: any[]): QueryConstraint;

export class QuerySnapshot<T = DocumentData> {
private constructor();
readonly query: Query<T>;
Expand Down
12 changes: 11 additions & 1 deletion packages/firestore/exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ export { SnapshotMetadata } from '../src/api/database';
export {
DocumentReference,
CollectionReference,
QueryConstraint,
Query,
doc,
collection,
collectionGroup,
parent
parent,
startAt,
startAfter,
endAt,
endBefore,
query,
limit,
limitToLast,
where,
orderBy
} from '../lite/src/api/reference';

export { runTransaction, Transaction } from './src/api/transaction';
Expand Down
78 changes: 36 additions & 42 deletions packages/firestore/exp/test/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
onSnapshot,
onSnapshotsInSync,
parent,
query,
queryEqual,
refEqual,
runTransaction,
Expand All @@ -57,7 +58,15 @@ import {
terminate,
updateDoc,
waitForPendingWrites,
writeBatch
writeBatch,
endAt,
endBefore,
startAfter,
startAt,
limitToLast,
limit,
orderBy,
where
} from '../../exp/index';
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
Expand Down Expand Up @@ -453,7 +462,7 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
): Query<T> {
return new Query<T>(
this.firestore,
this._delegate.where(unwrap(fieldPath), opStr, unwrap(value))
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
);
}

Expand All @@ -463,63 +472,44 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
): Query<T> {
return new Query<T>(
this.firestore,
this._delegate.orderBy(unwrap(fieldPath), directionStr)
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
);
}

limit(limit: number): Query<T> {
return new Query<T>(this.firestore, this._delegate.limit(limit));
limit(n: number): Query<T> {
return new Query<T>(this.firestore, query(this._delegate, limit(n)));
}

limitToLast(limit: number): Query<T> {
return new Query<T>(this.firestore, this._delegate.limitToLast(limit));
limitToLast(n: number): Query<T> {
return new Query<T>(this.firestore, query(this._delegate, limitToLast(n)));
}

startAt(...args: any[]): Query<T> {
if (args[0] instanceof DocumentSnapshot) {
return new Query(
this.firestore,
this._delegate.startAt(args[0]._delegate)
);
} else {
return new Query(this.firestore, this._delegate.startAt(...unwrap(args)));
}
return new Query(
this.firestore,
query(this._delegate, startAt(...unwrap(args)))
);
}

startAfter(...args: any[]): Query<T> {
if (args[0] instanceof DocumentSnapshot) {
return new Query(
this.firestore,
this._delegate.startAfter(args[0]._delegate)
);
} else {
return new Query(
this.firestore,
this._delegate.startAfter(...unwrap(args))
);
}
return new Query(
this.firestore,
query(this._delegate, startAfter(...unwrap(args)))
);
}

endBefore(...args: any[]): Query<T> {
if (args[0] instanceof DocumentSnapshot) {
return new Query(
this.firestore,
this._delegate.endBefore(args[0]._delegate)
);
} else {
return new Query(
this.firestore,
this._delegate.endBefore(...unwrap(args))
);
}
return new Query(
this.firestore,
query(this._delegate, endBefore(...unwrap(args)))
);
}

endAt(...args: any[]): Query<T> {
if (args[0] instanceof DocumentSnapshot) {
return new Query(this.firestore, this._delegate.endAt(args[0]._delegate));
} else {
return new Query(this.firestore, this._delegate.endAt(...unwrap(args)));
}
return new Query(
this.firestore,
query(this._delegate, endAt(...unwrap(args)))
);
}

isEqual(other: legacy.Query<T>): boolean {
Expand Down Expand Up @@ -776,6 +766,10 @@ function unwrap(value: any): any {
return value._delegate;
} else if (value instanceof DocumentReference) {
return value._delegate;
} else if (value instanceof DocumentSnapshot) {
return value._delegate;
} else if (value instanceof QueryDocumentSnapshot) {
return value._delegate;
} else if (isPlainObject(value)) {
const obj: any = {};
for (const key in value) {
Expand Down
60 changes: 41 additions & 19 deletions packages/firestore/lite-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,50 @@ export class Query<T = DocumentData> {
protected constructor();
readonly type: 'query' | 'collection';
readonly firestore: FirebaseFirestore;
where(
fieldPath: string | FieldPath,
opStr: WhereFilterOp,
value: any
): Query<T>;
orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection
): Query<T>;
limit(limit: number): Query<T>;
limitToLast(limit: number): Query<T>;
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
startAt(...fieldValues: any[]): Query<T>;
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
startAfter(...fieldValues: any[]): Query<T>;
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
endBefore(...fieldValues: any[]): Query<T>;
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
endAt(...fieldValues: any[]): Query<T>;

withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
}

export type QueryConstraintType =
| 'where'
| 'orderBy'
| 'limit'
| 'limitToLast'
| 'startAt'
| 'startAfter'
| 'endAt'
| 'endBefore';

export class QueryConstraint {
private constructor();
readonly type: QueryConstraintType;
}

export function query<T>(
query: CollectionReference<T> | Query<T>,
...constraints: QueryConstraint[]
): Query<T>;

export function where(
fieldPath: string | FieldPath,
opStr: WhereFilterOp,
value: any
): QueryConstraint;
export function orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection
): QueryConstraint;
export function limit(limit: number): QueryConstraint;
export function limitToLast(limit: number): QueryConstraint;
export function startAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function startAt(...fieldValues: any[]): QueryConstraint;
export function startAfter(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function startAfter(...fieldValues: any[]): QueryConstraint;
export function endBefore(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function endBefore(...fieldValues: any[]): QueryConstraint;
export function endAt(snapshot: DocumentSnapshot<any>): QueryConstraint;
export function endAt(...fieldValues: any[]): QueryConstraint;

export class QuerySnapshot<T = DocumentData> {
private constructor();
readonly query: Query<T>;
Expand Down
12 changes: 11 additions & 1 deletion packages/firestore/lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
export {
DocumentReference,
Query,
QueryConstraint,
CollectionReference,
collection,
collectionGroup,
Expand All @@ -42,7 +43,16 @@ export {
updateDoc,
addDoc,
refEqual,
queryEqual
queryEqual,
startAt,
startAfter,
endAt,
endBefore,
query,
limit,
limitToLast,
where,
orderBy
} from './src/api/reference';

// TOOD(firestorelite): Add tests when Queries are usable
Expand Down
Loading