googleapis / googleapis/google-cloud-node

[FR] Ability to get all descendants

Open
#7,439 6 comments 2 reactions 1 assignee Claimed by @wu-hui View on GitHub
api: firestore library: nodejs-firestore priority: p3 type: feature request
Dominant language
TypeScript
Stars
3.2k
Forks
712
Avg merge
2d 3h
Merged PRs (30d)
99

Description

We would love to have the ability to query all descendants of a document (or collection) even when the intermediate documents do not exist.

For example, let's say `/my-collection/my-doc/my-sub-collection/my-sub-doc` (and many siblings) exists in Firestore, but `/my-collection/my-doc` does not exist. It's then not possible to query those documents from the sub-collection. Most of the pieces seem to exist in `QueryOptions.forKindlessAllDescendants` that is used from `RecursiveDelete.getAllDescendants` but those are all private.

We currently achieve this by getting the `FirestoreClient` from `@google-cloud/firestore/types/v1` by poking in some internal variables of the public Firestore client and then invoking the `runQuery` method on that client:
```typescript
client.runQuery({
parent: `projects/${projectId}/databases/${databaseId}/documents/${rootDoc.path}`,
structuredQuery: {
from: [{ allDescendants: true }],
orderBy: [{ field: { fieldPath: '__name__' } }],
},
});
```

But this is a hassle and we get raw protobuf responses that we need to serialize and all the other good stuff that the public Firestore client normally does.

Another alternative is to manipulate `allDescendants` directly in the `QueryOptions` (which also works):
```typescript
const collection = firestore.collection('my-collection')
assert('_queryOptions' in collection);
const qo = collection._queryOptions;
assert(typeof qo === 'object');
assert(!!qo);
assert('allDescendants' in qo);
qo.allDescendants = true;
const { docs } = await collection.get();
expect(docs.map(d => d.ref.path)).toEqual([nestedDoc.path]);
```

It would be wonderful if a `.allDescendants()` method could be added to `CollectionReference` that sets `allDescendants` in `QueryOptions` to `true`. The rest of the handling seems to be there.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.