Search code examples
google-cloud-firestore

Is there any problem using ISO string in Firestore instead of their Timestamp object?


The recommended type to represent timestamps in Firestore is Firestore Timestamp, but this is not the same as the Date object in JS.

The problem with this is that I need to convert/stringify these objects when we get them from the DB, and maintain 2 TypeScripts interfaces if we want to be strict.

These are not big problems, but I was thinking on saving timestamps using ISO strings (e.g.: '2024-04-16T20:37:22.711Z'), and having into account that these strings are safe to compare, I can safely do queries like:

const firstDayOfThisYear = '2024-01-01'
// createdAt contains an ISO string
const q = query(collectionRef, where("createdAt", ">", firstDayOfThisYear));

Is there any problem with this? Is this query using strings less performant than using Firestore Timestamp?


Solution

  • Is there any problem with this?

    No, not until you need something from these strings that you didn't design for.

    Is this query using strings less performant than using Firestore Timestamp?

    No. But consider benchmarking this yourself anyway. It's a good practice if you're really concerned about performance.