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 3 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/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,28 +275,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.node.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
47 changes: 20 additions & 27 deletions packages/firestore/exp/test/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
onSnapshot,
onSnapshotsInSync,
parent,
query,
queryEqual,
refEqual,
runTransaction,
Expand All @@ -54,7 +55,15 @@ import {
terminate,
updateDoc,
waitForPendingWrites,
writeBatch
writeBatch,
limit,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these imports be ordered alphabetically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to defer this to the day when we have a lint rule for this. We have tried to keep imports sorted, but it is hard to maintain without tooling. I have since given up on this completely and life has been fabulous :)

where,
orderBy,
startAt,
startAfter,
endAt,
endBefore,
limitToLast
} from '../../exp/index.node';
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
Expand Down Expand Up @@ -417,7 +426,7 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
value: any
): Query<T> {
return new Query<T>(
this._delegate.where(unwrap(fieldPath), opStr, unwrap(value))
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
);
}

Expand All @@ -426,48 +435,32 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
directionStr?: legacy.OrderByDirection
): Query<T> {
return new Query<T>(
this._delegate.orderBy(unwrap(fieldPath), directionStr)
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
);
}

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

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

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

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

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

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

isEqual(other: legacy.Query<T>): boolean {
Expand Down
60 changes: 41 additions & 19 deletions packages/firestore/lite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,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.node.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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here... are imports typically sorted alphabetically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Summary: Yes, but no :)

startAt,
startAfter,
endAt,
endBefore,
query,
limit,
limitToLast,
where,
orderBy
} from './src/api/reference';

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