Skip to content

Instantly share code, notes, and snippets.

@maxgfr
Created August 28, 2020 19:57
Show Gist options
  • Select an option

  • Save maxgfr/6ad8f204d83664ed233a588d153f073e to your computer and use it in GitHub Desktop.

Select an option

Save maxgfr/6ad8f204d83664ed233a588d153f073e to your computer and use it in GitHub Desktop.
Transform Firestore Types
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