0

For the past few days I've been conducting some experiments related to storing Firestore Timestamp objects in Firestore and I wanted to share my findings and get some opinions.

For the experiments I used Node.js 14 with Typescript and compared Callable Functions with HTTP Functions.

Why did I conduct those experiments? I am working on a project where I noticed a difference in how Timestamps are stored in either of those function types.

First of all, I am aware that objects are serialized as JSON strings before stored in the Firestore Database.

With this in mind, what does not make sense is that when using HTTP Functions timestamps are actually stored as Timestamp and not as Map. When using Callable Functions on the other hand, timestamps are stored as Map with seconds and nanoseconds as keys (because of the JSON serialization).

In either of those cases, the Timestamp object is created by the function itself. Meaning, they are not sent by the client.

Why is this a problem? Because I decided to use Callable Functions for write requests only. The reason for this is, because Firestore Rules can get quite complex and really cumbersome to debug. So all the reads happen directly from the Firestore Database, with the help of the cloud_firestore Flutter plugin. Storing Timestamp objects with this plugin is straightforward, because they are actually stored as Timestamp and not as Map. And the problem lies exactly here. I would have to implement custom read logic in the client and the functions, to actually convert the Map to a Timestamp.

This is not what I want ...

My test object that is stored in my Firestore Database:

export interface Purchase {
  product: string;
  user: string;
  timeOfPurchase: firestore.Timestamp;
}

How do I create the object and Timestamp instance?

const purchase = {
  product: "My super product",
  user: "John Doe",
  timeOfPurchase: Timestamp.now(),
};

How does it look like in Firestore?

Callable Function:

Callable Function

HTTP Function:

HTTP Function

Fun fact: When you actually store the object in Firestore with a Callable Function and you have your Firebase Console open in your browser, you can see the Timestamp being stored as a Timestamp object, but within a second or so, this Timestamp is changed to a Map. So you can actually see it changing. This looks a little bit weird to me and not like a wanted behavior.

I could not figure out why yet. Did anyone notice this behavior? What are your opinions?

schmidi000
  • 308
  • 3
  • 14
  • Researching about this issue I found related cases where is said that, as you said, there is something lost when the JSON is serialized. It seems that for now there are just some workaround to be able to get the data as a Timestamp, [reconstitute it as a new Timestamp object](https://stackoverflow.com/questions/56420690/firestore-timestamp-passed-through-callable-functions?rq=1) or using [date.toJSON](https://stackoverflow.com/questions/53520674/returning-dates-server-timestamps-in-cloud-callable-functions), let me know if it helps somehow. – Vicky Oct 04 '21 at 13:51

0 Answers0