/querity

Generic interface to query a database

Primary LanguageJavaApache License 2.0Apache-2.0

Querity-logo Querity

Build Bugs Vulnerabilities Coverage Quality Gate Status Twitter

Generic interface to query a database.

Description

The aim of this project is to provide a simple generic interface, supporting fluent Java and REST APIs, to query databases in Java applications.

The modules implement the support for different databases and frameworks.

Why you should use Querity?

✔ learn once, use everywhere

✔ zero logic needed apart building a generic Query object

✔ switch database without rewriting the logic of your application

✔ ready to use REST API which implements filtering, sorting and pagination, to be consumed by your UI components

✔ expose the same REST API in all your projects

Getting Started

Dependencies

  • Java 8+

Installing

All releases are published to the Maven Central repository ( see here).

Available modules:

  • querity-spring-data-jpa: supports Spring Data JPA
  • querity-spring-data-mongodb: supports Spring Data MongoDB
  • querity-spring-web: supports JSON serialization and deserialization of Querity objects in Spring Web MVC

All modules are "Spring Boot starters", you just need to add the dependency to your Spring Boot project and start using it, no other configuration needed.

Maven:

<dependency>
  <groupId>net.brunomendola.querity</groupId>
  <artifactId>querity-spring-data-jpa</artifactId>
  <version>${querity.version}</version>
</dependency>

Gradle:

implementation "net.brunomendola.querity:querity-spring-data-jpa:${querityVersion}"

Usage

import static net.brunomendola.querity.api.Querity.*;
import static net.brunomendola.querity.api.Operator.*;
import static net.brunomendola.querity.api.Sort.Direction.*;

@Service
public class MyService {

  @Autowired
  Querity querity;

  public Result<Person> getPeople() {
    Query query = Querity.query()
        // customize filters, pagination, sorting...
        .filter(
            not(and(
                filterBy("lastName", EQUALS, "Skywalker"),
                filterBy("firstName", EQUALS, "Luke")
            ))
        )
        .sort(sortBy("lastName"), sortBy("birthDate", DESC))
        .pagination(1, 10)
        .build();
    List<Person> items = querity.findAll(Person.class, query);
    Long totalCount = querity.count(Person.class, query.getFilter());
    return new Result<>(items, totalCount);
  }

  record Result<T>(List<T> items, Long totalCount) {
  }
}

In the above example, the findAll method returns the first of n pages with max 10 elements of all people NOT named Luke Skywalker, sorted by last name and then birthdate descending.
The count method returns the total filtered items count excluding pagination (the record keyword is implemented from Java 14).

Note the static imports to improve the readability.

Documentation

Read the full documentation here.

Access to SNAPSHOT builds

Commits to the main branch are automatically built and deployed to OSSRH SNAPSHOTs Maven repository.

To use the SNAPSHOTs in your project, add the SNAPSHOTs repository as follows.

Of course using SNAPSHOTs is not recommended, but if you feel brave you can do it to test new not-yet-released features.

Maven:

<repositories>
    <repository>
      <id>ossrh-snapshots-repo</id>
      <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
      <releases><enabled>false</enabled></releases>
      <snapshots><enabled>true</enabled></snapshots>
    </repository>
</repositories>

Gradle:

repositories {
  maven {
    url "https://s01.oss.sonatype.org/content/repositories/snapshots"
    mavenContent { snapshotsOnly() }
  }
}

Browse the repository here to find the latest SNAPSHOT version.

Development

Running tests

Run with Maven (wrapper):

./mvnw test

or just run them with your favourite IDE.

Test dataset

The test dataset is generated with Mockaroo.

If you want to make changes, you don't need to do it manually, please find the schema here.

Authors

Contributors names and contact info

PRs are welcome!

Version History

See Releases.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details