Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maif/jooq-async
Use jooq to build queries and run it a reactive way
https://github.com/maif/jooq-async
jooq reactive vertx
Last synced: 3 months ago
JSON representation
Use jooq to build queries and run it a reactive way
- Host: GitHub
- URL: https://github.com/maif/jooq-async
- Owner: MAIF
- License: apache-2.0
- Created: 2020-09-21T08:09:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-17T16:31:17.000Z (4 months ago)
- Last Synced: 2024-09-27T07:42:32.338Z (3 months ago)
- Topics: jooq, reactive, vertx
- Language: Java
- Homepage:
- Size: 202 KB
- Stars: 6
- Watchers: 8
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Reactive jooq API [![ga-badge][]][ga] [![jar-badge][]][jar]
[ga]: https://github.com/MAIF/jooq-async/actions?query=workflow%3ABuild
[ga-badge]: https://github.com/MAIF/jooq-async/workflows/Build/badge.svg
[jar]: https://maven-badges.herokuapp.com/maven-central/fr.maif/jooq-async-api
[jar-badge]: https://maven-badges.herokuapp.com/maven-central/fr.maif/jooq-async-api/badge.svgThis API is a solution to use jooq with reactive clients for RDBMS.
## Implementations
At the moment there are 2 implementations:
* a blocking jdbc implementation
* a vertx reactive implementation for postgresql only## Import
Jcenter hosts this library.
### Maven
```xml
fr.maif
jooq-async-jdbc
${version}```
OR
```xml
fr.maif
jooq-async-reactive
${version}```
### Gradle
```gradle
implementation "fr.maif:jooq-async-api:${version}"
```OR
```gradle
implementation "fr.maif:jooq-async-reactive:${version}"
```## The API
### Create a pool
The JDBC one :
```java
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
PgAsyncPool jdbcPgAsyncPool = new JdbcPgAsyncPool(SQLDialect.POSTGRES, dataSource, Executors.newFixedThreadPool(5));
```The reactive one :
```java
DefaultConfiguration jooqConfig = new DefaultConfiguration();
jooqConfig.setSQLDialect(SQLDialect.POSTGRES);
PgConnectOptions options = new PgConnectOptions()
.setPort(port)
.setHost(host)
.setDatabase(database)
.setUser(user)
.setPassword(password);
PoolOptions poolOptions = new PoolOptions().setMaxSize(10);
Vertx vertx = Vertx.vertx();
Pool client = PgBuilder.pool()
.using(vertx)
.connectingTo(options)
.with(poolOptions)
.build();PgAsyncPool reactivePgAsyncPool = new ReactivePgAsyncPool(client, jooqConfig);
```### Perform query
The idea is to use the jooq DSL as a builder to write the query. The query is then run against the underlying library.
#### Query one :
```java
CompletionStage> futureResult = reactivePgAsyncPool
.queryOne(dsl -> dsl.select(name).from(table).where(name.eq("Ragnar")))
.map(mayBeResult -> mayBeResult.map(row -> row.get(name)));
```#### Query many :
```java
CompletionStage> futureResult = reactivePgAsyncPool
.query(dsl -> dsl.select(name).from(table)))
.map(results -> results.map(row -> row.get(name)));
```#### Stream data
```java
Publisher stream = reactivePgAsyncPool
.stream(500 /*fetch size*/, dsl -> dsl.select(name).from(table))
.map(q -> q.get(name));
```The publisher comes from the reactive streams API.
#### Execute statement
```java
CompletionStage insertResult = reactivePgAsyncPool.inTransaction(t ->
t.execute(dsl -> dsl.insertInto(table).set(name, "test"))
);
```#### Batch statements
With this version you can send a statement once and then send all parameters.
This version is the most performant if you have one statement with multiple values.```java
List names = List.range(0, 10).map(i -> "name-" + i);
CompletionStage batchResult = reactivePgAsyncPool.executeBatch(
dsl -> dslContext.insertInto(table).columns(name).values((String) null),
names.map(List::of)
);
```With this version, you can batch a set of statements. You should use this version if your statements are all different.
```java
List names = List.range(0, 10).map(i -> "name-" + i);
CompletionStage batchResult = reactivePgAsyncPool.executeBatch(dsl ->
names.map(n -> dslContext.insertInto(table).set(name, n))
);
```## Spring reactor :
The `jooq-async-reactive` module expose operations with the `Mono` / `Flux` API.
```java
PgAsyncPool pgAsyncPool = PgAsyncPool.create(client, jooqConfig);Mono> result = pgAsyncPool.queryOneOne(dsl -> dsl
.select(name)
.from(table)
.where(name.eq("Ragnar"))
)
.map(mayBeResult -> mayBeResult.map(row -> row.get(name)));
```