googleapis / googleapis/google-cloud-node

FR: Firestore Timestamp should have a comparator method

Open
#7,462 1 comment 0 reactions 0 assignees 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

The JS/TS API for Firestore's Timestamp (both web client [Timestamp](https://firebase.google.com/docs/reference/js/firestore_.timestamp) and node backend [Timestamp](https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/timestamp)) do not include a comparator function. This makes sorting accurately by Timestamp rather painful, as you have to write a function that manually compares both seconds and nanos. An easy workaround is to use `toMillis`, but it's lossy on nanos:

```
array.sort((a, b) => b.ts.toMillis() - a.ts.toMillis())
```

Instead, it would be great if there was a comparator method provided in the same way as the Firestore [Timestamp.compareTo()](https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.Timestamp#com_google_cloud_Timestamp_compareTo_com_google_cloud_Timestamp_) provided by the Java SDK.

```
array.sort((a, b) => b.ts.compareTo(a.ts))
```

Related issue for the Firebase web client SDK: https://github.com/firebase/firebase-js-sdk/issues/7711

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.