Krate

General

Category
Free
Tag
ORM
License
MIT License
Min SDK
2 (Android 1.1)
Registered
Sep 25, 2018
Favorites
0
Link
https://github.com/Lamartio/Krate
See also
DbQuery
ROOM-Dynamic-Dao
Android-ORM
greenDAO
EasyliteOrm

Additional

Language
Kotlin
Version
N/A
Created
Sep 6, 2018
Updated
Oct 2, 2018 (Retired)
Owner
Danny Lamarti (Lamartio)
Contributor
Danny Lamarti (Lamartio)
1
Activity
Badge
Generate
Download
Source code

Krate

Krate is a persistence layer that offer CRUD operations for a key-value store and result is delivered through a RxObservable. Additionally you can sync your offline data with a server through Krate's fetch functions.

class User(val name: String = "Danny", val age: Int = 27)

const val key = "userId"

fun crud(krate: Krate) {
    krate.get<User>(key).subscribe { user -> /* ... */ }
    
    krate.put(key, User()).subscribe { /* ... */ }
    
    krate.remove(key).subscribe { /* ... */ }
}

Apps often rely on a webservice to provide objects. Krate is great for this data centric approach, since it persists the result of a network call before passing it through.

// first gets it from persistence
// next it does the network call and persist the result
// emits 0, 1 or 2 results.

fun network(krate: Krate, getUserFromApi: () -> Single<User>): Flowable<User> = 
        krate.getAndFetch(key, getUserFromApi)

Based on your needs you can decide you persistence location. Images are often not welcome in a database, so Krate can manage a directory for you:

fun krates(context: Context, picture: ByteArray) {
    val dirKrate = DirectoryKrate(context.cacheDir)
    val sqlKrate = DatabaseKrate(context.openOrCreateDatabase("test", Context.MODE_PRIVATE, null))
    
    dirKrate.put("giantPicture", picture).subscribe()
    sqlKrate.put("smallObject", Any()).subscribe()
}

You may have the need of encryption or you want to use an alternative serializer. Krate's implementations provide easy access for this use cases:

fun customKrate(context: Context) =
        DirectoryKrate(
                directory = context.cacheDir,
                serializer = CustomSerializer(), // swap the default Java serialization
                interceptor = CustomInterceptor() // manipulate the bytes
        )

Krate offers control over the threads you want to apply your operations on. Just wrap the your Krate in a SchedulerKrate.

fun schedulerKrate(krate: Krate): Krate =
        SchedulerKrate(
                krate,
                Schedulers.io(), // io and network operations
                AndroidSchedulers.mainThread() // result is dispatched to the UI thread
        )

License

Copyright 2018 Danny Lamarti.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.