Critter

Critter will look at your source code and generate type safe criteria builders for each model object. To use it, you simply need to add a plugin to your maven pom:

<plugin>
    <groupId>dev.morphia.critter</groupId>
    <artifactId>critter-maven</artifactId>
    <version>4.2.0-SNAPSHOT</version>
    <executions>
        <execution>
            <id>critter</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <force>false</force>
                <criteriaPackage>com.bob.bar</criteriaPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

This will generate your criteria classes in target/generated-sources/critter and add the directory as a source directory of your maven project.

If you use gradle, you would configure your file like so (using the kotlin dsl):

plugins {
    id("dev.morphia.critter") version "4.2.0-SNAPSHOT"
}


tasks {
    critter {
        force = true
        outputType = "java"
    }
}

This will generate your criteria classes in build/generated-src/critter and add the directory as a source directory of your gradle project.

criteriaPackage If the criteriaPackage option is omitted, the code will be generated using the package of your entities with .criteria appended. If a value is given, every criteria generated will live in the named package.

force

This defaults to false but if it’s set to true, critter will always regenerate your criteria classes.

outputType

This defaults to kotlin but also accepts java as well. Setting this changes the language used in the generated criteria classes.

Critter requires Morphia 2.0.0 and is built for Java 11.

What difference does it make?

Before critter, your criteria might look something like this:

Query<Book> query = ds.find(Book.class)
    .filter(and(
        eq("bookmark",bookmark),
        eq("database",database)));

But using critter, it would look like this:

Query<Book> query = ds.find(Book.class)
    .filter(and(
        Person.bookmark().eq(bookmark),
        Person.database().eq(database)));

Notice how bookmark() and database() methods were created based on the model object Book’s fields. The comparison methods you’re familiar with from Morphia’s filters API are all there but now only take the type of the field itself. With this code in place if the model object changes, the code above runs the risk of failing to compile allowing you to catch model/query conflicts at compile time rather than waiting for things to fail at runtime (or in your tests if you’re lucky enough to have those).

You can see a working example in the [tests](https://github.com/MorphiaOrg/critter/tree/master/tests).

IDEA Users

IDEA users will need to enable the plugin registry in the maven configuration options for IDEA to pick up the plugin.

Issues and support

Please check here if you find an issue or need help.