Skip to content

Latest commit

 

History

History
49 lines (42 loc) · 1.29 KB

README.md

File metadata and controls

49 lines (42 loc) · 1.29 KB

RSQL QueryDSL

RSQL-QueryDSL is a lightweight library that helps to build restful APIs using SQL like operators with QueryDSL.

Pet Shop Example:

http://localhost:8080/pets?id.in=1,3&born.btw=2000-10-01,2020-01-01&nikname.like=Br

How to use

Add dependency to your POM

<dependency>
    <groupId>io.github.apulbere</groupId>
    <artifactId>rsql-querydsl</artifactId>
    <version>1.0</version>
</dependency>

Define a DTO that will represent your search criteria

@Setter
@Getter
public class PetCriteria {
    LongCriteria id = LongCriteria.empty();
    LocalDateCriteria born = LocalDateCriteria.empty();
    StringCriteria petType = StringCriteria.empty();
    StringCriteria nickname = StringCriteria.empty();
}

Use your model in controller

@GetMapping("/pets")
List<PetRecord> search(PetCriteria criteria, Pageable page) {
    var predicate = criteria.id.match(pet.id)
            .and(criteria.born.match(pet.birthdate))
            .and(criteria.nickname.match(pet.name))
            .and(criteria.petType.match(pet.type));
    return petRepository.findAll(predicate, page)
            .stream()
            .map(petMapper::map)
            .toList();
}

License

The Apache License, Version 2.0