kossnocorp / typesaurus

🦕 Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
412 stars 34 forks source link

Timestamp not maintained with all() #91

Closed skwny closed 3 years ago

skwny commented 3 years ago

When retrieving data via all(collection), what should be a Firestore timestamp is actually a Date object. This is unexpected as my schema is a Timestamp, and expects it to have all the associated methods when it comes out of firestore.

For example, the following is my schema:

interface Thing {
  created_on: Timetsamp
}

And while saving data:

import firebase from 'firebase/app'

const thing: Thing = {
  created_on: firebase.firestore.FieldValue.serverTimestamp()
}

And when retrieving and using the data:

thing.createdOn.toDate()  <-- error - toDate is not a function

The above errors because thing is actually Date object, not a Firestore Timestamp.

Did I miss this in the documentation somewhere? Is this supposed to be how it works?

kossnocorp commented 3 years ago

Hey, Typesaurus convert dates to timestamp and vise versa, so you can always use native Date:

import { value, collection, get, set } from 'typesaurus'

interface Thing {
  created_on: Date
}

const things = collection<Thing>('things')

await set(things, 'qwe', {
  created_on: value('serverDate') // or new Date()
})

const thing = await get(things, 'qwe')

thing.data.created_on.getFullYear()
// => 2021