kossnocorp / typesaurus

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

Passing SnapshotOptions. #87

Closed Karlinator closed 3 years ago

Karlinator commented 3 years ago

I'd like to be able to pass or set somehow SnapshotOptions.

These can be useful when using serverTimestamps, where you can tell Firebase to use the previous value or an estimate instead of null while a write is pending.

This is passed as data = documentSnapshot.data({serverTimestamps: 'estimate'}).

https://firebase.google.com/docs/reference/js/firebase.firestore.SnapshotOptions (though the docs on this say nothing of usage).

See https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore-types/index.d.ts (ctr+f "SnapshotOptions")

I've been digging around slightly, seems like this could be done by adding it as an optional parameter in each of the getting functions (get, all, etc), and also in typesaurus-react.

As far as I can tell, SnapshotOptions is only a thing in the browser sdk, not in the nodejs sdk. I'm not sure how to handle that.

kossnocorp commented 3 years ago

Hey, you can try 8.0.0-alpha.21 where you can do that: get(collection, id, { serverTimestamps: 'estimate' }). v8 also considers that dates might be undefined unless it's in Node.js environment or serverTimestamps: 'estimate'. I use this version in production for several months already as I didn't have enough time to write documentation and tests for all those changes.

Karlinator commented 3 years ago

Ooo, it didn't occur to me to look at pre-release versions. I'll definitely do that! I see it's available on the alpha for typesaurus-react too.

Thanks!

kossnocorp commented 3 years ago

@Karlinator no worries, ping me if you have any questions.

Karlinator commented 3 years ago

@kossnocorp I can't quite figure out how to actually use this.

I've got this line, using typesaurus-react:

const [messages, messagesState] = useOnAll<Message>(messagesCollection, {serverTimestamps: "estimate"})

How do I pass the options correctly? It wants a second argument next to Message; what?

kossnocorp commented 3 years ago

Sorry, I didn't realize, but serverTimestamps are missing in the Typesaurus React. Let me add those and get back to you.

kossnocorp commented 3 years ago

Please try typesaurus@8.0.0-alpha.22 and @typesaurus/react@5.0.0-alpha.2

Karlinator commented 3 years ago

Thanks! It works!