Native Mode

Cloud Firestore Native Instance

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

1
gcloud services enable firestore.googleapis.com
Copied!

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 Firestore

The easiest way to access Cloud Firestore is using Spring Cloud GCP's Spring Data Firestore starter. This starter provides full Spring Data support for Cloud Firestore while implementing idiomatic access patterns.
Spring Data Feature
Supported
Reactive Repository
ORM
Declarative Transaction
Repository
REST Repository
Query methods
Query annotation
Pagination
Events
Auditing

Dependency

Add the Spring Data Firestore starter:
Maven
Gradle
1
<dependency>
2
<groupId>org.springframework.cloud</groupId>
3
<artifactId>spring-cloud-gcp-starter-data-firestore</artifactId>
4
</dependency>
Copied!
1
compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-data-firestore'
Copied!

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

Spring Data Cloud Firestore allows you to map domain POJOs to Datastore documents via annotations.
1
@Document
2
class Order {
3
@DocumentId
4
private String id;
5
private String description;
6
private LocalDateTime timestamp;
7
private List<OrderItem> items;
8
9
// Getters and setters ...
10
}
11
12
class OrderItem {
13
private String description;
14
private Long quantity;
15
16
// Getters and setters ...
17
}
Copied!
Because Firestore is a document-oriented NoSQL database, you can have nested structure and can establish parent-children relationships without complicated foreign keys.

Repository

Use Spring Data Reactive repository to quickly get CRUD access to the Cloud Firestore.
1
@Repository
2
interface OrderRepository extends FirestoreReactiveRepository<Order> {
3
}
Copied!
In a business logic service, you can utilize the repositories:
1
@Service
2
class OrderService {
3
private final OrderRepository orderRepository;
4
5
OrderService(OrderRepository orderRepository) {
6
this.orderRepository = orderRepository;
7
}
8
9
@Transactional
10
Mono<Order> createOrder(Order order) {
11
// Set the creation time
12
order.setTimestamp(Timestamp.of(new Date()));
13
14
// Children are saved in cascade.
15
return orderRepository.save(order);
16
}
17
}
Copied!

Samples

Last modified 1yr ago