Home > database >  Firestore duration type
Firestore duration type

Time:03-07

It seems one can write Firestore rules that check that a value is of the duration type, like this:

allow write: if request.resource.data.bla is duration;

However, it's totally cryptic how to produce such a value, at least from the web APIs.

The Firestore Rules reference has a section for the duration namespace and the Duration interface.

It is even mentioned in this official Firebase video (link points to the exact time that the duration type is being talked about).

I had success testing in the Rules Playground writing a doc which had the value set to a <number>s pattern, e.g. 100s or 5s and even 0.5s, so I presumed that would map to an amount of seconds. I tried some other units, e.g. 1w (1 week) or 3d (3 days) or 5m (5 minutes), but no success.
Furthermore, none of these work locally in the Firestore emulator.

I understand that the duration type can be useful to validate e.g. timestamp inputs, but I wouldn't expect it to be accepted on its own (so there's no reason to have is duration compile) as it stands.
Am I missing something? Is this type meant to be hidden, unused, or...?

CodePudding user response:

From the documentation,

Valid data types for the is operator are bool, bytes, float, int, list, latlng, number, path, map, string, and timestamp. The is operator also supports constraint, duration, set, and map_diff data types, but since these are generated by the security rules language itself and not generated by clients, you rarely use them in most practical applications.

So is duration mostly can be used to validate if result of any operation in security rules (mostly timestamp related calculations) is of type duration or not.

The video you shared explains what expressions return duration:

Expression Result
timestamp ± duration timestamp
timestamp - timestamp duration
duration ± duration duration
  • Related