# Firestore

Terra can store health data directly in your [Google Cloud Firestore](https://cloud.google.com/firestore) database, with full payloads stored inline as documents.

## Setup

[Create a service account](https://developers.google.com/workspace/guides/create-credentials#service-account) with Firestore read/write access to your project. Download the JSON credentials file.

Enter the following in the Terra Dashboard when adding the Firestore destination:

* **GCP Project ID**
* **Service account credentials** (JSON file upload)

## Data Structure

Terra organises documents in a hierarchical collection structure under a `terra/2022-03-16` base path.

#### Collections

| Collection path                 | Contents                                 | Document name                 |
| ------------------------------- | ---------------------------------------- | ----------------------------- |
| `terra/2022-03-16/user_events`  | Auth events (auth success, deauth, etc.) | `{user_id}-{timestamp}.json`  |
| `terra/2022-03-16/activity`     | Activity data                            | `{user_id}-{start_time}.json` |
| `terra/2022-03-16/body`         | Body metrics                             | `{user_id}-{start_time}.json` |
| `terra/2022-03-16/daily`        | Daily summary data                       | `{user_id}-{start_time}.json` |
| `terra/2022-03-16/sleep`        | Sleep data                               | `{user_id}-{start_time}.json` |
| `terra/2022-03-16/nutrition`    | Nutrition data                           | `{user_id}-{start_time}.json` |
| `terra/2022-03-16/menstruation` | Menstruation data                        | `{user_id}-{start_time}.json` |

#### Document format

All documents contain the full data payload inline — there are no external URLs to download. Data event documents include the `user` object alongside the data fields.

Documents are overwritten if a document with the same name already exists (last write wins).
