diff --git a/README.md b/README.md index dc030a5..599e8f1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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")]], + }, +}); +``` diff --git a/deno.json b/deno.json index b176e12..f6946e5 100644 --- a/deno.json +++ b/deno.json @@ -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", diff --git a/mod.ts b/mod.ts index 90a9b56..e14c7c0 100644 --- a/mod.ts +++ b/mod.ts @@ -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}`); }