Forwarding Database

ForwardingDatabase is a mechanism to perform additional logic before executing a query against a database. An example of using this would be to create a forwarding database that prevents returning cached query results.

To create a forwarding database that returns a non-cached result:

ForwardingDatabase noCacheDatabase = new ForwardingDatabase()
{
    @Override
    protected <T> Query<T> filterQuery(Query<T> query) {
       return query.clone().noCache();
    }
};

The example above can be simplified using a lambda expression:

ForwardingDatabase noCacheDatabase = () -> {
    return query.clone().noCache();
};

To use the no-cache forwarding database, specify the database your query should use via Query#using():

Query.from(User.class).using(noCacheDatabase).selectAll()

Because this query specified that the noCacheDatabase should be used, the filterQuery method will be invoked prior to the query being executed.

Both Dari and Brightspot use ForwardingDatabases for a variety of scenarios. Here are a few examples: