Created
August 28, 2020 19:57
-
-
Save maxgfr/6ad8f204d83664ed233a588d153f073e to your computer and use it in GitHub Desktop.
Transform Firestore Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const transformFirestoreTypes = (obj: any): any => { | |
| Object.keys(obj).forEach(key => { | |
| if (!obj[key]) return | |
| if (typeof obj[key] === 'object' && 'toDate' in obj[key]) { | |
| obj[key] = obj[key].toDate().getTime() | |
| } else if (obj[key].constructor.name === 'GeoPoint') { | |
| const { latitude, longitude } = obj[key] | |
| obj[key] = { latitude, longitude } | |
| } else if (obj[key].constructor.name === 'DocumentReference') { | |
| const { id, path } = obj[key] | |
| obj[key] = { id, path } | |
| } else if (typeof obj[key] === 'object') { | |
| transformFirestoreTypes(obj[key]) | |
| } | |
| }) | |
| return obj | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment