Server dates
Server date is a special type Typesaurus.ServerDate
that denotes that the given field can only be assigned by the database itself.
It extends the built-in behavior of Firestore server timestamps, resolves to Date
, and adds type-safety.
If you set a field to a server date, assigning it on the client with anything but the special server date value will show an error:
The $.serverDate
helper is available in all write methods: add
, set
, update
, and upset
.
Nullable on web
When a client sets a server date, and you’re subscribed to the document, the value of the field for a brief moment will be null
, which can lead to unhandled exceptions.
That’s why the document data, by default, will have all server date fields optional so that you can handle this case properly in your UI code.
On server
When working on the server, you can pass { as: "server" }
as the operation option to let Typesaurus know that dates are safe to use:
Normalizing server dates
When working with the database types, you might want to normalize server dates to Date
so you can use them in your code so that you don’t get a type error:
To do that, you can use the Typesaurus.NormalizeServerDates
helper type:
That will allow you to reuse database types in your code and keep the type safety.