An open API service indexing awesome lists of open source software.

https://github.com/marthym/juery

Juery is a tiny Java library to manage search and filter query from user to database.
https://github.com/marthym/juery

http-requests java jooq rest

Last synced: about 1 month ago
JSON representation

Juery is a tiny Java library to manage search and filter query from user to database.

Awesome Lists containing this project

README

        

# Juery [![](https://img./github/release/Marthym/juery.svg)](https://GitHub.com/Marthym/juery/releases/) [![GitHub license](https://img.shields.io/github/license/Marthym/juery.svg)](https://github.com/Marthym/juery/blob/master/LICENSE)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Marthym_juery&metric=alert_status)](https://sonarcloud.io/dashboard?id=Marthym_juery)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=Marthym_juery&metric=coverage)](https://sonarcloud.io/dashboard?id=Marthym_juery)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Marthym_juery&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=Marthym_juery)

**Juery** is a tiny Java library to manage search and filter query from user to database. **api** and **basic** packages
have no dependencies. They contain everything you need to use Juery. The **jooq** package contains useful tools for
projects using the jOOQ DSL.

## Installation

Use the package manager [maven](https://maven.apache.org/) to install juery.

```xml

fr.ght1pc9kc
juery-api
VERSION

fr.ght1pc9kc
juery-basic
VERSION

fr.ght1pc9kc
juery-jooq
VERSION

```

for gradle

```groovy
compile "fr.ght1pc9kc:juery-api:VERSION"
compile "fr.ght1pc9kc:juery-basic:VERSION"
compile "fr.ght1pc9kc:juery-jooq:VERSION"
```

## Usage

### Create Criteria and PageRequest manually

```java
import fr.ght1pc9kc.juery.api.Criteria;

Criteria.property("jedi").eq("Obiwan")
.and(Criteria.property("age").gt(40)
.or(Criteria.property("age").lt(20)));
```

```java
import fr.ght1pc9kc.juery.api.PageRequest;
import fr.ght1pc9kc.juery.api.pagination.Direction;
import fr.ght1pc9kc.juery.api.pagination.Order;
import fr.ght1pc9kc.juery.api.pagination.Sort;

PageRequest.builder()
.page(2).size(100)
.filter(Criteria.property("profile").eq("jedi").and(Criteria.property("job").eq("master")))
.sort(Sort.of(new Order(Direction.ASC, "name"), new Order(Direction.DESC, "email")))
.build();
```

### Parsing a query string

Into the controller.

```java
import fr.ght1pc9kc.juery.basic.QueryStringParser;

@GetMapping
public Flux list(@RequestParam Map queryStringParams) {
return feedService.list(QueryStringParser.withDefaultConfig().parse(queryStringParams))
.onErrorMap(BadCriteriaFilter.class, e -> new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getLocalizedMessage()));
}
```

`QueryStringParser` will transform the query string into a` PageRequest` which contains a `Criteria`.
Depending on your architecture, the object will traverse layers until persistence.
It can be enriched in the process by `with` methods which create a new enriched object. **All the API is strictly immutable**.

*Since 1.2.0*: You can now, configure the `QueryStringParser` to customize the querystring parameters used :

```java
import fr.ght1pc9kc.juery.basic.ParserConfiguration;

ParserConfiguration config = ParserConfiguration.builder()
.page("_pg")
.size("_sz")
.from("_fr")
.to("_to")
.sort("_st")
.maxPageSize(20)
.build();

QueryStringParser.withConfig(config).parse(queryStringParams);
```

### Filtering

In the persistence layer we will be able to use `Visitors` as follows.

```java
import fr.ght1pc9kc.juery.jooq.filter.JooqConditionVisitor;

private static final JooqConditionVisitor JOOQ_CONDITION_VISITOR =
new JooqConditionVisitor(PropertiesMappers.FEEDS_PROPERTIES_MAPPING::get);
```

The `JooqConditionVisitor` implementation takes as input a `Function >` which will allow to transform
the properties of your criteria, into jOOQ `Field` objects corresponding to columns of your tables.

```java
import fr.ght1pc9kc.juery.api.Criteria;
import fr.ght1pc9kc.juery.api.PageRequest;

// transforms the Criteria present in pageRequest into Condition
Condition conditions = pageRequest.filter.visit(JOOQ_CONDITION_VISITOR);

// Execute query with the conditions generated by the visitor
Cursor cursor = dsl
.select(FEEDS.fields()).select(FEEDS_USERS.FEUS_TAGS)
.from(FEEDS)
.leftJoin(FEEDS_USERS).on(FEEDS_USERS.FEUS_FEED_ID.eq(FEEDS.FEED_ID))
.where(conditions).fetchLazy();
```

### Pagination

The `PageRequest` object contains the data necessary for pagination. You can implement it yourself,
but for jOOQ users, the `JooqPagination` helper makes it easier.

```java
import fr.ght1pc9kc.juery.api.Criteria;
import fr.ght1pc9kc.juery.api.PageRequest;

// transforms the Criteria present in pageRequest into Condition
Condition conditions = pageRequest.filter.visit(NEWS_CONDITION_VISITOR);

// Apply pagination parameters to the query
final Select query = JooqPagination.apply(pageRequest, PropertiesMappers.FEEDS_PROPERTIES_MAPPING, dsl
.select(FEEDS.fields()).select(FEEDS_USERS.FEUS_TAGS)
.from(FEEDS)
.leftJoin(FEEDS_USERS).on(FEEDS_USERS.FEUS_FEED_ID.eq(FEEDS.FEED_ID))
.where(conditions)
);

Cursor cursor = query.fetchLazy();
```

We find the `PropertiesMappers.FEEDS_PROPERTIES_MAPPING`, optional, which allows to make the link between the sort
criteria and the table fields.

### PropertiesMappers example

As an example to illustrate what is `PropertiesMappers.FEEDS_PROPERTIES_MAPPING` in the context of using jOOQ as DSL.

```java
public static final Map> FEEDS_PROPERTIES_MAPPING = Map.of(
"id", FEEDS.FEED_ID,
"name", FEEDS.FEED_NAME,
...
);
```

`FEEDS` was a jOOQ Table generated before compilation with `jooq-codegen-maven` Maven Plugin. `"id"` and `"name"` was properties
used in `Criteria`.

```java
import fr.ght1pc9kc.juery.api.Criteria;

Criteria.property("id").eq("F0042")
.and(Criteria.property("name").eq("H2G2"));
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License

[MIT](https://choosealicense.com/licenses/mit/)