-
Notifications
You must be signed in to change notification settings - Fork 246
Kotlin
Nikhil Purushe edited this page May 10, 2017
·
16 revisions
The kotlin module provides an alternate query and storage interface that takes advantage of Kotlin specific language features. These include property references, infix functions, operator functions and more.
To use these features use KotlinEntityDataStore
instead of EntityDataStore
.
val configuration = KotlinConfiguration(dataSource = dataSource, model = Models.DEFAULT)
val data = KotlinEntityDataStore(configuration)
data.invoke {
val result = select(Person::class) where (Person::name eq "Bob") limit 5
val first = result().first()
}
In Kotlin entity definitions can be created from interface classes with properties or from immutable data classes. Example: (note the get prefix on annotations)
@Entity
interface Person : Persistable {
@get:Key
@get:Generated
var id: Int
var name: String
var email: String
var birthday: Date
var age: Int
@get:ForeignKey
@get:OneToOne
var address: Address
@get:ManyToMany
val groups: Set<Group>
var about: String
@get:Column(unique = true)
var uuid: UUID
var homepage: URL
}
Data class example:
@Entity
data class Person constructor (
@get:Key
var id: Int,
var name: String,
var email: String,
var birthday: Date,
var age: Int,
var about: String,
@get:Column(unique = true)
val uuid: UUID,
val homepage: URL,
val picture: String
)
Note data classes are subject to same restrictions as immutable types
- Add the apt gradle plug-in to your build as described here
- Add the requery dependencies to your app build.gradle file:
dependencies {
compile 'io.requery:requery:<latestVersion>'
compile 'io.requery:requery-kotlin:<latestVersion>'
compile 'io.requery:requery-android:<latestVersion>' // optional for android support
apt 'io.requery:requery-processor:<latestVersion>'
}
A full android example project is available here