Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 0513b87

Browse files
authored
ReactiveCrudRepository implementation (#1846)
ReactiveCrudRepository implementation ; fixes #1831
1 parent 6cfbba5 commit 0513b87

File tree

5 files changed

+469
-50
lines changed

5 files changed

+469
-50
lines changed

spring-cloud-gcp-data-firestore/src/main/java/org/springframework/cloud/gcp/data/firestore/FirestoreReactiveOperations.java

+62-4
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,89 @@ public interface FirestoreReactiveOperations {
3232
* update).
3333
* @param <T> the type of the object to save.
3434
* @param instance the instance to save.
35-
* @return the instance that was saved.
35+
* @return {@link Mono} emitting the saved entity.
3636
*/
3737
<T> Mono<T> save(T instance);
3838

3939
/**
4040
* Saves multiple objects to Cloud Firestore. Not atomic. Behaves as insert or update.
4141
* @param instances the objects to save.
4242
* @param <T> the type of the objects to save.
43-
* @return a {@link Flux} of the instances that were saved
43+
* @return {@link Flux} emitting the saved entities.
4444
*/
4545
<T> Flux<T> saveAll(Publisher<T> instances);
4646

4747
/**
4848
* Get all the entities of the given domain type.
4949
* @param <T> the type param of the domain type.
5050
* @param entityClass the domain type to get.
51-
* @return the entities that were found.
51+
* @return {@link Mono} emitting the found entities.
5252
*/
5353
<T> Flux<T> findAll(Class<T> entityClass);
5454

5555
/**
5656
* Delete all entities of a given domain type.
5757
* @param <T> the type param of the domain type.
5858
* @param entityClass the domain type to delete from Cloud Datastore.
59-
* @return the number of entities that were deleted.
59+
* @return {@link Mono} emitting the number of deleted entities.
6060
*/
6161
<T> Mono<Long> deleteAll(Class<T> entityClass);
62+
63+
/**
64+
* Test if the entity of the given domain type with a given id exists.
65+
* Uses the first emitted element to perform the query.
66+
* @param <T> the type param of the domain type.
67+
* @param entityClass the domain type of the entity.
68+
* @param idPublisher publisher that provides an id.
69+
* @return {@link Mono} emitting {@code true} if an entity with the given id exists, {@code false} otherwise.
70+
*/
71+
<T> Mono<Boolean> existsById(Publisher<String> idPublisher, Class<T> entityClass);
72+
73+
/**
74+
* Get an entity of the given domain type by id.
75+
* Uses the first emitted element to perform the query.
76+
* @param <T> the type param of the domain type.
77+
* @param idPublisher publisher that provides an id.
78+
* @param entityClass the domain type of the entity.
79+
* @return {@link Mono} emitting the found entity.
80+
*/
81+
<T> Mono<T> findById(Publisher<String> idPublisher, Class<T> entityClass);
82+
83+
/**
84+
* Get an entity of the given domain type by id.
85+
* @param <T> the type param of the domain type.
86+
* @param idPublisher publisher that provides ids.
87+
* @param entityClass the domain type of the entity.
88+
* @return {@link Flux} emitting the found entities.
89+
*/
90+
<T> Flux<T> findAllById(Publisher<String> idPublisher, Class<T> entityClass);
91+
92+
/**
93+
* Count entities of the given domain.
94+
* Note that Firestore doesn't support "count" operation natively,
95+
* so id query will be executed and all ids will be retrieved so they could be counted.
96+
* @param <T> the type param of the domain type.
97+
* @param entityClass the domain type of entities.
98+
* @return {@link Mono} emitting the number of entities.
99+
*/
100+
<T> Mono<Long> count(Class<T> entityClass);
101+
102+
/**
103+
* Delete entities provided by publisher.
104+
* @param <T> the type param of the domain type.
105+
* @param entityPublisher publisher that provides entities to be removed.
106+
* @return {@link Mono} signaling when operation has completed.
107+
*/
108+
<T> Mono<Void> delete(Publisher<T> entityPublisher);
109+
110+
/**
111+
* Delete entities of a given domain type using ids published by producer.
112+
* @param <T> the type param of the domain type.
113+
* @param idPublisher publisher that provides ids of entities to be removed.
114+
* @param entityClass the domain type of entities.
115+
* @return {@link Mono} signaling when operation has completed.
116+
*/
117+
<T> Mono<Void> deleteById(Publisher<String> idPublisher, Class entityClass);
118+
119+
62120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2019-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.gcp.data.firestore;
18+
19+
import org.reactivestreams.Publisher;
20+
import reactor.core.publisher.Flux;
21+
import reactor.core.publisher.Mono;
22+
23+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
24+
25+
/**
26+
* @author Dmitry Solomakha
27+
*
28+
* @since 1.2
29+
*/
30+
public class FirestoreReactiveRepository<T> implements ReactiveCrudRepository<T, String> {
31+
32+
private FirestoreTemplate firestoreTemplate;
33+
34+
private Class type;
35+
36+
public FirestoreReactiveRepository(FirestoreTemplate firestoreTemplate, Class type) {
37+
this.firestoreTemplate = firestoreTemplate;
38+
this.type = type;
39+
}
40+
41+
@Override
42+
public <S extends T> Mono<S> save(S entity) {
43+
return this.firestoreTemplate.save(entity);
44+
}
45+
46+
@Override
47+
public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
48+
return this.firestoreTemplate.saveAll(Flux.fromIterable(entities));
49+
}
50+
51+
@Override
52+
public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
53+
return this.firestoreTemplate.saveAll(entityStream);
54+
}
55+
56+
@Override
57+
public Mono<T> findById(String id) {
58+
return findById(Mono.just(id));
59+
}
60+
61+
@Override
62+
public Mono<T> findById(Publisher<String> idPublisher) {
63+
return this.firestoreTemplate.findById(idPublisher, this.type);
64+
}
65+
66+
@Override
67+
public Mono<Boolean> existsById(String id) {
68+
return existsById(Mono.just(id));
69+
}
70+
71+
@Override
72+
public Mono<Boolean> existsById(Publisher idPublisher) {
73+
return this.firestoreTemplate.existsById(idPublisher, this.type);
74+
}
75+
76+
@Override
77+
public Flux<T> findAll() {
78+
return this.firestoreTemplate.findAll(this.type);
79+
}
80+
81+
@Override
82+
public Flux<T> findAllById(Iterable<String> iterable) {
83+
return findAllById(Flux.fromIterable(iterable));
84+
}
85+
86+
@Override
87+
public Flux<T> findAllById(Publisher<String> idStream) {
88+
return this.firestoreTemplate.findAllById(idStream, this.type);
89+
}
90+
91+
@Override
92+
public Mono<Long> count() {
93+
return this.firestoreTemplate.count(this.type);
94+
}
95+
96+
@Override
97+
public Mono<Void> deleteById(String id) {
98+
return deleteById(Mono.just(id));
99+
}
100+
101+
@Override
102+
public Mono<Void> deleteById(Publisher idPublisher) {
103+
return this.firestoreTemplate.deleteById(idPublisher, this.type);
104+
}
105+
106+
@Override
107+
public Mono<Void> delete(Object entity) {
108+
return this.firestoreTemplate.delete(Mono.just(entity));
109+
}
110+
111+
@Override
112+
public Mono<Void> deleteAll(Iterable entities) {
113+
return this.firestoreTemplate.delete(Flux.fromIterable(entities));
114+
}
115+
116+
@Override
117+
public Mono<Void> deleteAll(Publisher entityStream) {
118+
return this.firestoreTemplate.delete(entityStream);
119+
}
120+
121+
@Override
122+
public Mono<Void> deleteAll() {
123+
return this.firestoreTemplate.deleteAll(this.type);
124+
}
125+
}

0 commit comments

Comments
 (0)