Skip to content

Commit 6064804

Browse files
committed
sample mono / flux handling
1 parent 7367b0f commit 6064804

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.xunfos.r2dbpointissue
2+
3+
import com.xunfos.r2dbpointissue.repository.PointyTable
4+
import com.xunfos.r2dbpointissue.repository.PointyTableRepository
5+
import io.r2dbc.postgresql.codec.Point
6+
import kotlinx.coroutines.flow.Flow
7+
import kotlinx.coroutines.flow.collect
8+
import kotlinx.coroutines.flow.flowOf
9+
import kotlinx.coroutines.reactive.asFlow
10+
import kotlinx.coroutines.reactive.asPublisher
11+
import kotlinx.coroutines.reactive.awaitFirstOrNull
12+
import kotlinx.coroutines.reactive.awaitSingle
13+
import kotlinx.coroutines.runBlocking
14+
import org.junit.jupiter.api.Test
15+
import org.springframework.boot.test.context.SpringBootTest
16+
import java.util.UUID
17+
18+
@SpringBootTest
19+
class HandlingTests(
20+
private val repository: PointyTableRepository,
21+
) {
22+
@Test
23+
fun `simple showcase of dealing with futures and fluxes`() = runBlocking {
24+
// Value already exists in the db, my entity is called PointyTable. Don't ask
25+
val find: PointyTable = repository.findById(
26+
UUID.fromString("0679a290-8015-473d-b4f4-3bbff0c35fdb")
27+
).awaitSingle()
28+
29+
val findOrNull: PointyTable = repository.findById(
30+
UUID.randomUUID()
31+
).awaitFirstOrNull() ?: getRandomValue() // you can write custom behavior here!
32+
33+
val findMany: Flow<PointyTable> = repository.findAll().asFlow()
34+
35+
val flowOfEntities: Flow<PointyTable> = flowOf(getRandomValue(), getRandomValue())
36+
val savedValues = repository.saveAll(flowOfEntities.asPublisher()).asFlow()
37+
38+
//operating / collecting
39+
savedValues.collect {
40+
println("My Saved Values: $it")
41+
}
42+
}
43+
44+
private fun getRandomValue(): PointyTable = PointyTable(
45+
id = UUID.randomUUID(),
46+
description = "Some description",
47+
location = Point.of(1.0, 2.2),
48+
)
49+
}

0 commit comments

Comments
 (0)