Spring Boot on GCP
  • Introduction
  • Getting Started
    • Google Cloud Platform
    • Cloud Shell
    • gcloud CLI
    • Hello World!
      • Cloud Shell
      • App Engine
      • Cloud Run
      • Kubernetes Engine
      • Compute Engine
      • Cloud Functions
  • Application Development
    • Development Tools
    • Spring Cloud GCP
    • Cloud Services
      • Databases
        • Cloud SQL
        • Cloud Spanner
        • Cloud Firestore
          • Datastore Mode
          • Native Mode
      • Messaging
        • Cloud Pub/Sub
        • Kafka
      • Secret Management
      • Storage
      • Cache
        • Memorystore Redis
        • Memorystore Memcached (beta)
      • Other Services
    • Observability
      • Trace
      • Logging
      • Metrics
      • Profiling
      • Debugging
    • DevOps
      • Artifact Repository
  • Deployment
    • Runtime Environments
    • Container
      • Container Image
      • Secure Container Image
      • Container Awareness
      • Vulnerability Scanning
      • Attestation
    • Kubernetes
      • Kubernetes Cluster
      • Deployment
      • Resources
      • Service
      • Health Checks
      • Load Balancing
        • External Load Balancing
        • Internal Load Balancing
      • Scheduling
      • Workload Identity
      • Binary Authorization
    • Istio
      • Getting Started
      • Sidecar Proxy
  • Additional Resources
    • Code Labs
    • Presentations / Videos
    • Cheat Sheets
Powered by GitBook
On this page
  • Cloud Firestore Datastore Instance
  • Enable API
  • Data Schema
  • Spring Data Datastore
  • Dependency
  • Configuration
  • ORM
  • Repository
  • Rest Repository
  • Samples

Was this helpful?

  1. Application Development
  2. Cloud Services
  3. Databases
  4. Cloud Firestore

Datastore Mode

PreviousCloud FirestoreNextNative Mode

Last updated 4 years ago

Was this helpful?

Cloud Firestore Datastore Instance

There can only be one Cloud Firestore instance associated with a single project. The Datastore instance is automatically created when you enable the API:

There can only be one Datastore instance associated with a single project. The Cloud Firestore in Datastore instance is automatically created when you enable the API:

Enable API

gcloud services enable datastore.googleapis.com

Data Schema

Because Cloud Firestore is a NoSQL database, you do not need to explicitly create tables, define data schema, etc. Simply use the API to store new documents, and perform CRUD operations.

Spring Data Datastore

The easiest way to access Datastore is using Spring Cloud GCP's . This starter provides full Spring Data support for Datastore while implementing idiomatic access patterns.

Spring Data Feature

Supported

ORM

✅

Declarative Transaction

✅

Repository

✅

REST Repository

✅

Query methods

✅

Query annotation

✅

Pagination

✅

Events

✅

Auditing

✅

Dependency

Add the Spring Data Datastore starter:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-data-datastore</artifactId>
</dependency>
compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-data-datastore'

Configuration

There is no explicit configuration required if you use the automatic authentication and project ID detection. I.e., if you already logged in locally with gcloud command line, then it'll automatically use Datastore from the project you configured in gcloud.

Notice that there is no explicit configuration for username/password. Cloud Firestore authentication uses the GCP credential (either your user credential, or Service Account credential), and authorization is configured via Identity Access Management (IAM).

ORM

@Entity
class Order {
    @Id
    private Long id;
    private String description;
    private LocalDateTime timestamp;
    private List<OrderItem> items;

    // Getters and setters ...
}

@Entity
class OrderItem {
    private String description;
    private Long quantity;

    // Getters and setters ...
}

Because Datastore is a document-oriented NoSQL database, you can have nested structure, you can establish parent-children relationships without complicated foreign keys.

Repository

Use Spring Data repository to quickly get CRUD access to the Datastore.

@Repository
interface OrderRepository extends DatastoreRepository<Order, Long> {
}

In a business logic service you can utilize the repositories:

@Service
class OrderService {
    private final OrderRepository orderRepository;

    OrderService(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Transactional
    Order createOrder(Order order) {
        // Set the creation time
        order.setTimestamp(LocalDateTime.now());

        // Children are saved in cascade.
        return orderRepository.save(order);
    }
}

Rest Repository

Add Spring Data Rest starter:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
@RepositoryRestResource
interface OrderRepository extends DatastoreRepository<Order, String> {
}

To access the endpoint for Order:

curl http://localhost:8080/orders

Samples

Spring Data Cloud Datastore allows you to map domain POJOs to Datastore documents via annotations. Read the for details

Read the for more details.

can expose a Spring Data repository directly on a RESTful endpoint, and rendering the payload as JSON with format. It supports common access patterns like pagination.

Spring Data Datastore starter
Spring Data Datastore reference documentation
Spring Data Datastore reference documentation
Spring Data Rest
HATEOAS
Spring Boot with Datastore sample