Skip to content
This repository was archived by the owner on Oct 18, 2025. It is now read-only.

Commit eed57b7

Browse files
committed
v.1.1.1: Enable filtering by documentIds in getDocumentsInCollection
1 parent 6511646 commit eed57b7

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ const firestore = new FirestoreAdminClient();
6363
`firestore-admin` will take care of parsing the data to/from JSON when
6464
fetching/updating documents.
6565

66+
If you work with Firestore Timestamp fields, read the
67+
[Using timestamps](#using-timestamps) section.<br /> If you need to query
68+
documents by their ID, read the [Using document IDs in a filter](#using-document-ids-in-a-filter)
69+
section.
70+
6671
### Create a document
6772

6873
```typescript
@@ -204,7 +209,8 @@ await firestore.createDocument("my-collection", {
204209
});
205210
```
206211

207-
The above will be converted to a Firestore timestamp automatically.
212+
The above `Date` object will be converted to a Firestore timestamp
213+
automatically.
208214

209215
When filtering results by timestamp, make sure to use `Date` objects as well,
210216
e.g.:
@@ -220,3 +226,33 @@ const documents = await firestore.getDocumentsInCollection("my-collection", {
220226
},
221227
});
222228
```
229+
230+
In the query results, timestamp fields will be returned as an ISO Date string,
231+
e.g. `2025-01-01T00:00:00.000Z`.
232+
233+
## Using document IDs in a filter
234+
235+
If you need to filter query results by document IDs in the
236+
`getDocumentsInCollection` method, you can do so with the `documentId` field.
237+
238+
For example:
239+
240+
```typescript
241+
// Will return all documents in the collection with the document IDs `docId1` and `docId2`.
242+
const documents = await firestore.getDocumentsInCollection("my-collection", {
243+
where: {
244+
filters: [["documentId", FirestoreOperator.IN, ["docId1", "docId2"]]],
245+
},
246+
});
247+
248+
// You can still combine it with other filters, e.g.:
249+
250+
const documents = await firestore.getDocumentsInCollection("my-collection", {
251+
where: {
252+
filters: [
253+
["documentId", FirestoreOperator.IN, ["docId1", "docId2"]],
254+
["isActive", FirestoreOperator.EQUAL, true],
255+
],
256+
},
257+
});
258+
```

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@koiztech/firestore-admin",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"exports": "./mod.ts",
55
"tasks": {
66
"dev": "deno run --watch --env mod.ts",

mod.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,40 @@ export class FirestoreAdminClient {
231231
structuredQuery.where = {
232232
compositeFilter: {
233233
op: "AND",
234-
filters: options.where.filters.map(([field, op, value]) => ({
235-
fieldFilter: {
236-
field: { fieldPath: field },
237-
op,
238-
value: this.encodeValue(value),
239-
},
240-
})),
234+
filters: options.where.filters.map(([field, op, value]) => {
235+
if (field === "documentId") {
236+
if (!Array.isArray(value)) {
237+
value = [value];
238+
}
239+
// For documentId filters, we need to construct the full document path
240+
const fullPaths = value.map((id: string) => {
241+
// If ID is already a full path, use it directly
242+
if (id.includes("projects/")) return id;
243+
// Otherwise construct the full path
244+
return `projects/${this.GCP_PROJECT_NAME}/databases/(default)/documents/${path}/${id}`;
245+
});
246+
return {
247+
fieldFilter: {
248+
field: { fieldPath: "__name__" },
249+
op: FirestoreOperator.IN, // Filtering by document ID(s) requires IN operator
250+
value: {
251+
arrayValue: {
252+
values: fullPaths.map((p: any) => ({
253+
referenceValue: p,
254+
})),
255+
},
256+
},
257+
},
258+
};
259+
}
260+
return {
261+
fieldFilter: {
262+
field: { fieldPath: field },
263+
op,
264+
value: this.encodeValue(value),
265+
},
266+
};
267+
}),
241268
},
242269
};
243270
}

0 commit comments

Comments
 (0)