Skip to content

Commit

Permalink
v.1.0.4: Handle Dates in filters properly
Browse files Browse the repository at this point in the history
  • Loading branch information
olegkorol committed Dec 2, 2024
1 parent 96d7696 commit 76051a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,19 @@ const documents = await firestore.getDocumentsInCollection("my-collection", {
const document = await firestore.getDocument("my-collection", {
where: {
filters: [
["name", FirestoreOperator.IN, ["John Doe", "Max Mustermann"]],
["name", FirestoreOperator.IN, ["John Doe", "Max Mustermann"]], // example of an IN filter
],
},
});

// e.g.
// e.g. a more complex query
const documents = await firestore.getDocumentsInCollection("my-collection", {
where: {
filters: [
["name", FirestoreOperator.EQUAL, "Ivan Petrov"],
["age", FirestoreOperator.GREATER_THAN, 20],
["height", FirestoreOperator.LESS_THAN, 200],
["address.city", FirestoreOperator.EQUAL, "Moscow"], // example of a nested field
["bornAt", FirestoreOperator.GREATER_THAN, new Date("1990-01-01T12:50:00.000Z")], // example of a timestamp filter
],
},
orderBy: [{ field: "createdAt", direction: "DESCENDING" }], // you can sort the results
Expand Down Expand Up @@ -182,3 +183,13 @@ await firestore.createDocument("my-collection", {
```

The above will be converted to a Firestore timestamp automatically.

When filtering results by timestamp, make sure to use `Date` objects as well, e.g.:

```typescript
const documents = await firestore.getDocumentsInCollection("my-collection", {
where: {
filters: [["createdAt", FirestoreOperator.GREATER_THAN, new Date("2024-12-02")]],
},
});
```
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koiztech/firestore-admin",
"version": "1.0.3",
"version": "1.0.4",
"exports": "./mod.ts",
"tasks": {
"dev": "deno run --watch --env mod.ts",
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ export class FirestoreAdminClient {
return { nullValue: "NULL_VALUE" };
} else if (Array.isArray(value)) {
return { arrayValue: { values: value.map((v) => this.encodeValue(v)) } };
} else if (value instanceof Date) {
return { timestampValue: value.toJSON() };
} else {
throw new Error(`Unsupported value type: ${typeof value}`);
}
Expand Down

0 comments on commit 76051a2

Please sign in to comment.