Skip to content

Commit

Permalink
feat: add implicit ordering for startAt(DocumentReference) calls (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 20, 2020
1 parent fdf5462 commit e9afa38
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
32 changes: 13 additions & 19 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,23 +1115,6 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
.ignoreUndefinedProperties;
}

/**
* Detects the argument type for Firestore cursors.
*
* @private
* @param fieldValuesOrDocumentSnapshot A snapshot of the document or a set
* of field values.
* @returns 'true' if the input is a single DocumentSnapshot..
*/
static _isDocumentSnapshot(
fieldValuesOrDocumentSnapshot: Array<DocumentSnapshot<unknown> | unknown>
): boolean {
return (
fieldValuesOrDocumentSnapshot.length === 1 &&
fieldValuesOrDocumentSnapshot[0] instanceof DocumentSnapshot
);
}

/**
* Extracts field values from the DocumentSnapshot based on the provided
* field order.
Expand Down Expand Up @@ -1471,7 +1454,15 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
private createImplicitOrderBy(
cursorValuesOrDocumentSnapshot: Array<DocumentSnapshot<unknown> | unknown>
): FieldOrder[] {
if (!Query._isDocumentSnapshot(cursorValuesOrDocumentSnapshot)) {
// Add an implicit orderBy if the only cursor value is a DocumentSnapshot
// or a DocumentReference.
if (
cursorValuesOrDocumentSnapshot.length !== 1 ||
!(
cursorValuesOrDocumentSnapshot[0] instanceof DocumentSnapshot ||
cursorValuesOrDocumentSnapshot[0] instanceof DocumentReference
)
) {
return this._queryOptions.fieldOrders;
}

Expand Down Expand Up @@ -1527,7 +1518,10 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
): QueryCursor {
let fieldValues;

if (Query._isDocumentSnapshot(cursorValuesOrDocumentSnapshot)) {
if (
cursorValuesOrDocumentSnapshot.length === 1 &&
cursorValuesOrDocumentSnapshot[0] instanceof DocumentSnapshot
) {
fieldValues = Query._extractFieldValues(
cursorValuesOrDocumentSnapshot[0] as DocumentSnapshot,
fieldOrders
Expand Down
9 changes: 7 additions & 2 deletions dev/system-test/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ describe('CollectionGroup class', () => {

const documents: QueryDocumentSnapshot<DocumentData>[] = [];
for (const partition of partitions) {
// TODO(mrschmidt); Remove the need to add an `orderBy` here
let partitionedQuery = collectionGroup.orderBy(FieldPath.documentId());
let partitionedQuery: Query = collectionGroup;
if (partition.startAt) {
partitionedQuery = partitionedQuery.startAt(...partition.startAt);
}
Expand Down Expand Up @@ -1623,6 +1622,12 @@ describe('Query class', () => {
expectDocs(res, {foo: 'b'});
});

it('startAt() adds implicit order by for DocumentReference', async () => {
const references = await addDocs({foo: 'a'}, {foo: 'b'});
const res = await randomCol.startAt(references[1]).get();
expectDocs(res, {foo: 'b'});
});

it('has startAfter() method', async () => {
await addDocs({foo: 'a'}, {foo: 'b'});
const res = await randomCol.orderBy('foo').startAfter('a').get();
Expand Down
26 changes: 26 additions & 0 deletions dev/test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,32 @@ describe('startAt() interface', () => {
});
});

it('appends orderBy for DocumentReference cursors', () => {
const overrides: ApiOverride = {
runQuery: request => {
queryEquals(
request,
orderBy('__name__', 'ASCENDING'),
startAt(true, {
referenceValue:
`projects/${PROJECT_ID}/databases/(default)/` +
'documents/collectionId/doc',
})
);

return stream();
},
};

return createInstance(overrides).then(firestore => {
return snapshot('collectionId/doc', {foo: 'bar'}).then(doc => {
let query: Query = firestore.collection('collectionId');
query = query.startAt(doc.ref);
return query.get();
});
});
});

it('can extract implicit direction for document snapshot', () => {
const overrides: ApiOverride = {
runQuery: request => {
Expand Down

0 comments on commit e9afa38

Please sign in to comment.