Skip to content

queritylib/querity

Repository files navigation

Querity-logo Querity

Build Bugs Vulnerabilities Coverage Quality Gate Status Twitter

Open-source Java query builder for SQL and NoSQL.

Description

Querity is an extensible query builder to create and run database queries in your Java application.

It supports SQL and NoSQL databases technologies, and each support is built into small modules, so you can import the one which fits into your project.

Features

Database support:

  • any SQL database (with the JPA modules)
  • MongoDB
  • Elasticsearch

Query features:

  • filtering
  • sorting
  • pagination
  • textual query language
  • support for REST APIs
  • support for DTO layer
  • and more...

All with ONE SINGLE LANGUAGE!

Documentation

Read the full documentation here.

Demo

Check out the simplest demo application using Querity at querity-demo.

This demo also uses the @queritylib/react library for the frontend. Take a look!

Getting Started

Dependencies

  • Java 17+
  • Spring Framework 6 (optionally Spring Boot 3... makes things a lot simpler)

Installing

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

Available modules:

  • querity-spring-data-jpa: supports Spring Data JPA
  • querity-jpa: supports plain Jakarta Persistence API (Spring not required)
  • querity-spring-data-mongodb: supports Spring Data MongoDB
  • querity-spring-data-elasticsearch: supports Spring Data Elasticsearch
  • querity-spring-web: supports JSON de/serialization of Querity objects in Spring Web MVC
  • querity-parser: enable the parsing of Querity objects from a simple query language

All Spring 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>io.github.queritylib</groupId>
  <artifactId>querity-spring-data-jpa</artifactId>
  <version>${querity.version}</version>
</dependency>

Gradle:

implementation "io.github.queritylib:querity-spring-data-jpa:${querityVersion}"

Usage

@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.

Query language

The querity-parser module provides a simple query language to build a Query object, useful when you need the user to write and understand the query.

It is an alternative approach to the one provided by the module querity-spring-web, which parses JSON.

The following snippet rewrites the previous example using the query language:

import io.github.queritylib.querity.api.Query;
import io.github.queritylib.querity.parser.QuerityParser;

//...

public List<Person> getPeople() {
  Query query = QuerityParser.parseQuery("not(and(lastName=\"Skywalker\", firstName=\"Luke\")) sort by lastName, birthDate desc page 1,10");
  return querity.findAll(Person.class, query);
}

//...

Access to SNAPSHOT builds

Commits to the main branch are automatically built and deployed to Central Portal 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>
    <name>Central Portal Snapshots</name>
    <id>central-portal-snapshots</id>
    <url>https://central.sonatype.com/repository/maven-snapshots/</url>
    <releases>
      <enabled>false</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

Gradle:

repositories {
  // ...
  maven {
    name = 'Central Portal Snapshots'
    url = 'https://central.sonatype.com/repository/maven-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