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.
- Host: GitHub
- URL: https://github.com/marthym/juery
- Owner: Marthym
- License: mit
- Created: 2021-04-23T18:49:39.000Z (about 4 years ago)
- Default Branch: develop
- Last Pushed: 2024-06-26T09:03:44.000Z (11 months ago)
- Last Synced: 2025-04-10T00:42:48.431Z (about 1 month ago)
- Topics: http-requests, java, jooq, rest
- Language: Java
- Homepage: https://marthym.github.io/juery/
- Size: 270 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Juery [](https://GitHub.com/Marthym/juery/releases/) [](https://github.com/Marthym/juery/blob/master/LICENSE)
[](https://sonarcloud.io/dashboard?id=Marthym_juery)
[](https://sonarcloud.io/dashboard?id=Marthym_juery)
[](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
VERSIONfr.ght1pc9kc
juery-basic
VERSIONfr.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/)