Requery — My favorite Android ORM library

Adrián Bukros
MAKERS channel
Published in
2 min readMar 4, 2016

About Requery

There are lot of libraries that help you store your data in SQLite database on Android, but Requery makes it a lot more simple. It’s a light but powerful ORM and SQL query generator that supports RxJava, and helps you keep boilerplate code to a minimum. The library itself was built by Nikhil Purushe.

Using Requery

The follwing example is written in Kotlin.

First step is to add the lib to you project by modifying build.gradle:

dependencies {
compile ‘io.requery:requery:1.0-SNAPSHOT’
compile ‘io.requery:requery-android:1.0-SNAPSHOT’
kapt ‘io.requery:requery-processor:1.0-SNAPSHOT’
}

Setup the Kotling Annotation Processor so you can use the generated classes in your code:

kapt {
generateStubs = true
}

You also need to add a custom repository location. Requery is in development phase so only snapshot version is available:

repositories {
jcenter()
maven { url ‘http://oss.jfrog.org/artifactory/oss-snapshot-local'}
}

Now you can use Requery. Add these lines to your Application class or your dependency injection code to access the SingleEntityStore anywhere from your app.

val source = DatabaseSource(context, entityModel, dbVersion);
val dataStore = RxSupport.toReactiveStore(
EntityDataStore<Persistable>(source.configuration)
);

You can use the auto generated Model.DEFAULT for the entityModel parameter, which is created after you’ve annotated a class or interface with the necessary requery annotations. Eg. for a simple user class:

@Entity
public abstract class User {
@Key @Generated
int id;
@Index(name=”name”)
String name;
}

(Currently only Java classes supported for model definition.)

This makes the framework generate the UserEntity class which can be used for further actions, like insert:

val user = UserEntity()
user.setName(“XY”)
dataStore.insert(user)
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe{ user ->
Timber.d(“User inserted: “+user.getId()+” “+user .getName())
}

Or select:

dataStore.select(UserEntity::class.java)
.get()
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe{ user ->
Timber.d(“User from DB: “+user.getId()+” “+user .getName())
}

Summary

So Requery is a promising solution for persisting your data on Android, taking a lot of weight off of developers. Checkout the project GitHub repo here, and my example app here.

--

--