# Firestore A Firestore database in Native mode, with point-in-time recovery, a scheduled backup, a composite index and the grant a workload needs. - A project's first database is named `(default)`. Any other name creates an additional database, which clients must name when they connect. - `location_id` is a region or a multi-region such as `eur3`, and cannot be changed afterwards. - `type` is `FIRESTORE_NATIVE` or `DATASTORE_MODE`, and cannot be changed once the database holds data. - `delete_protection_state` is the guard in the API; `deletion_policy = "DELETE"` is needed before Terraform will remove a database at all. - `point_in_time_recovery_enablement` keeps seven days of versions. A backup schedule is separate and keeps whole backups for `retention`, in seconds with an `s`. - A workload reads and writes with `roles/datastore.user`, granted on the project. There is no per-database role; restrict with an IAM condition on the database name. ```hcl variable "project_id" { type = string description = "Project to deploy into." } variable "region" { type = string description = "Region for regional resources." } variable "app_service_account_email" { type = string description = "Service account of the workload that uses the database." } resource "google_firestore_database" "app" { project = var.project_id name = "app" location_id = var.region type = "FIRESTORE_NATIVE" concurrency_mode = "OPTIMISTIC" point_in_time_recovery_enablement = "POINT_IN_TIME_RECOVERY_ENABLED" delete_protection_state = "DELETE_PROTECTION_ENABLED" deletion_policy = "ABANDON" } resource "google_firestore_backup_schedule" "daily" { project = var.project_id database = google_firestore_database.app.name retention = "1209600s" daily_recurrence {} } resource "google_firestore_index" "orders_by_customer" { project = var.project_id database = google_firestore_database.app.name collection = "orders" fields { field_path = "customer_id" order = "ASCENDING" } fields { field_path = "created_at" order = "DESCENDING" } } resource "google_project_iam_member" "app_uses_firestore" { project = var.project_id role = "roles/datastore.user" member = "serviceAccount:${var.app_service_account_email}" condition { title = "only-the-app-database" expression = "resource.name == \"${google_firestore_database.app.id}\" || resource.name.startsWith(\"${google_firestore_database.app.id}/\")" } } ```