Spring GCP Data Firestore

Reading Time: 2 minutes

Cloud GCP Firestore

Firestore easily develop applications using a fully managed, scalable, and serverless document database.

Feature

  • Serverless document database (Best for mobile application) that effortlessly scales to meet any demand, with no maintenance
  • Accelerate development of mobile, web, and IoT apps with direct connectivity to the database
  • Built-in live synchronization and offline mode makes it easy to develop real-time applications
  • Fully customizable security and data validation rules to ensure the data is always protected
  • Seamless integration with Firebase and Google Cloud services like Cloud Functions and BigQuery

Benefits

Launch applications and features faster

Firestore offers a great  developer experience with built-in live synchronization, offline support, and ACID transactions. These features are available across a robust set of client and server-side libraries.

Effortlessly scale to meet unpredictable demand

Firestore automatically scales up and down based on demand. It requires no maintenance, and provides high availability of 99.99–99.999% or four9-five9 achieved through strongly consistent data replication.

Simple and flexible with pay as you go

Database you pay only for what you use—no up-front expenditure or underutilised resources. Simplified architecture, apps talk directly to Firestore from your mobile or web clients.

Spring Data Cloud Firestore

Spring Cloud GCP adds Spring Data Reactive Repositories support for Google Cloud Firestore in native mode, providing reactive template and repositories support.
To begin using this library, add the spring-cloud-gcp-data-firestore artifact to your project.

Maven dependency, using Spring Cloud GCP BOM:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-data-firestore</artifactId>
</dependency>

Gradle dependency:

dependencies {
  implementation("org.springframework.cloud:spring-cloud-gcp-data-firestore")
}

Configuration file

application.properties

spring.cloud.gcp.firestore.project-id={PROJECT_ID}
spring.cloud.gcp.firestore.credentials.location=file:{PATH_OF_CREDENTIALS_FILE}

Autoconfiguration

Spring Boot auto-configuration creates the following beans available in the Spring application context:

  • An instance of FirestoreTemplate
  • Instances of all user defined repositories extending FirestoreReactiveRepository (an extension of ReactiveCrudRepository with additional Cloud Firestore features) when repositories are enabled
  • An instance of Firestore from the Google Cloud Java Client for Firestore, for convenience and lower level API access

Reactive Repositories

Spring Data Repositories is an abstraction that can reduce boilerplate code.

public interface UserRepository extends FirestoreReactiveRepository {}

Firestore Operations & Template

FirestoreOperations and its implementation, FirestoreTemplate provides the Template pattern familiar to Spring developers.

Using the auto-configuration provided by Spring Data Cloud Firestore, Spring application context will contain a fully configured FirestoreTemplate object that we can autowire in your application:

Code snippet ::

public class FirestoreTemplateExample {
@Autowired
FirestoreOperations firestoreOperations;

public Mono<User> createUsers() {
    return this.firestoreOperatons.save(new User("Abid", 29))
        .then(this.firestoreOperatons.save(new User("Khan", 60)));
}

public Flux<User> findUsers() {
    return this.firestoreOperatons.findAll(User.class);
}

public Mono<Long> removeAllUsers() {
    return this.firestoreOperatons.deleteAll(User.class);
}
}

Supported data types

Following field types when defining your persistent entities or when binding query parameters:

  • Long, Integer, Double, Float, String, Boolean, Character, Date, Map, List, Enum
  • com.google.cloud.Timestamp
  • com.google.cloud.firestore.GeoPoint
  • com.google.cloud.firestore.Blob

Written by 

Abid Khan is a Lead Consultant at Knoldus Inc., postgraduate (MCA), and having 5+ years of experience in JavaSE, JavaEE, ORM framework, Spring, Spring-boot, RESTful Web Services, Kafka, MQTT, Rabbitmq, Docker, Redis, MySQL, Maven, GIT, etc. He is a well-developed professional with a prolific track record of designing, testing, and monitoring software as well as upgrading the existing programs.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading