NoSQL database query and access library for Kotlin

Overview

Kotlin NoSQL

Kotlin NoSQL is a reactive and type-safe DSL for working with NoSQL databases.

Status

Under development (POC). The following NoSQL databases are supported now:

Feedback is welcome.

Download

To use it with Maven insert the following code in your pom.xml file:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-nosql-mongodb</artifactId>
    <version>0.1-SNAPSHOT</version>
 </dependency>

 <repositories>
     <repository>
       <id>kotlin-nosql</id>
       <url>http://repository.jetbrains.com/kotlin-nosql</url>
     </repository>
</repositories>

To use it with Gradle insert the following code in your build.gradle:

repositories {
    maven {
        url "http://repository.jetbrains.com/kotlin-nosql"
    }
}

dependencies {
    compile 'org.jetbrains.kotlin:kotlin-nosql-mongodb:0.1-SNAPSHOT'
}

Getting Started

Demo: http://www.youtube.com/watch?v=80xgl3KThvM

Basics

Define a schema

object Comments: DocumentSchema<Comment>("comments", Comment::class) {
    val discussionId = id("discussion_id", Discussions)
    val slug = string("slug")
    val fullSlug = string("full_slug")
    val posted = dateTime("posted")
    val text = string("text")

    val AuthorInfo = AuthorInfoColumn()

    class AuthorInfoColumn() : Column<AuthorInfo, Comments>("author", AuthorInfo::class) {
        val authorId = id("id", Authors)
        val name = string("name")
    }
}

class Comment(val discussionId: Id<String, Discussions>, val slug: String,
              val fullSlug: String, posted: DateTime, text: String, authorInfo: AuthorInfo) {
    val id: Id<String, Comments>? = null
}

class AuthorInfo(val authorId: Id<String, Authors>, val name: String)

Define a database

val db = MongoDB(database = "test", schemas = arrayOf(Comments), action = CreateDrop(onCreate = {
    // ...
}))

db.withSession {
    // ...
}

Insert a document

Comments.insert(Comment(DiscussionId, slug, fullSlug, posted, text, AuthorInfo(author.id, author.name)))

Get a document by id

val comment = Comments.find { id.equal(commentId) }.single()

Get a list of documents by a filter expression

val comments = Comments.find { authorInfo.id.equal(authorId) }.sortBy { posted }.skip(10).take(5).toList()

Get selected fields by document id

val authorInfo = Comments.find { id.equal(commentId) }.projection { authorInfo }.single()

Get selected fields by a filter expression

Comments.find { discussionId.equal(id) }).projection { slug + fullSlug + posted + text + authorInfo }.forEach {
    val (slug, fullSlug, posted, text, authorInfo) = it
}

Update selected fields by document id

Comments.find { id.equal(commentId) }.projection { posted }.update(newDate)
Comments.find { id.equal(commentId) }.projection { posted + text }.update(newDate, newText)

Inheritance

Define a base schema

open class ProductSchema<D, S : DocumentSchema<D>(javaClass: Class<V>, discriminator: String) : DocumentSchema<V>("products",
            discriminator = Discriminator(string("type"), discriminator)) {
    val sku = string<S>("sku")
    val title = string<S>("title")
    val description = string<S>("description")
    val asin = string<S>("asin")

    val Shipping = ShippingColumn<S>()
    val Pricing = PricingColumn<S>()

    inner class ShippingColumn<S : DocumentSchema<D>>() : Column<Shipping, S>("shipping", Shipping::class) {
        val weight = integer<S>("weight")
        val dimensions = DimensionsColumn<S>()
    }

    inner class DimensionsColumn<S : DocumentSchema<D>>() : Column<Dimensions, S>("dimensions", Dimensions::class) {
        val width = integer<S>("width")
        val height = integer<S>("height")
        val depth = integer<S>("depth")
    }

    inner class PricingColumn<S : DocumentSchema<D>>() : Column<Pricing, S>("pricing", Pricing::class) {
        val list = integer<S>("list")
        val retail = integer<S>("retail")
        val savings = integer<S>("savings")
        val ptcSavings = integer<S>("pct_savings")
    }
}

object Products : ProductSchema<Product, Products>(Product::class, "")

abstract class Product(val id: Id<String, Products>? = null, val sku: String, val title: String, val description: String,
                       val asin: String, val shipping: Shipping, val pricing: Pricing) {
    val id: Id<String, Products>? = null
}

class Shipping(val weight: Int, val dimensions: Dimensions)

class Dimensions(val width: Int, val height: Int, val depth: Int)

class Pricing(val list: Int, val retail: Int, val savings: Int, val pctSavings: Int)

Define an inherited schema

object Albums : ProductSchema<Album, Albums>(Album::class, discriminator = "Audio Album") {
    val details = DetailsColumn()

    class DetailsColumn() : Column<Details, Albums>("details", Details::class) {
        val title = string("title")
        val artistId = id("artistId", Artists)
        val genre = setOfString("genre")

        val tracks = TracksColumn()
    }

    class TracksColumn() : ListColumn<Track, Albums>("tracks", Track::class) {
        val title = string("title")
        val duration = integer("duration")
    }
}

class Album(sku: String, title: String, description: String, asin: String, shipping: Shipping,
    pricing: Pricing, val details: Details) : Product(sku, title, description, asin, shipping, pricing)

class Details(val title: String, val artistId: Id<String, Artists>, val genre: Set<String>, val tracks: List<Track>)

Insert a document

val productId = Products.insert(Album(sku = "00e8da9b", title = "A Love Supreme", description = "by John Coltrane",
    asin = "B0000A118M", shipping = Shipping(weight = 6, dimensions = Dimensions(10, 10, 1)),
    pricing = Pricing(list = 1200, retail = 1100, savings = 100, pctSavings = 8),
    details = Details(title = "A Love Supreme [Original Recording Reissued]",
            artistId = artistId, genre = setOf("Jazz", "General"),
            tracks = listOf(Track("A Love Supreme Part I: Acknowledgement", 100),
                    Track("A Love Supreme Part II: Resolution", 200),
                    Track("A Love Supreme, Part III: Pursuance", 300)))))
}

Access documents via an abstract schema

for (product in Products.find { id.equal(productId) }) {
    if (product is Album) {
    }
}

Access documents via an inherited schema

val album = Albums.find { details.artistId.equal(artistId) }.single()

Examples

Comments
  • Can't get objectId column

    Can't get objectId column

    The following code throws java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to kotlinx.nosql.Id:

    ownerId = Pets.select {OwnerId}.get(Id(id))
    
    opened by cheptsov 0
  • Seems this project is inactive, anybody knows an active alternative for kotlin-sql?

    Seems this project is inactive, anybody knows an active alternative for kotlin-sql?

    Seems this project is inactive, anybody knows an active alternative for kotlin-sql? since the last commit was on Latest commit 58ec5e5 on Feb 15, 2016

    Regards.

    opened by eriknyk 1
  • petclinic demo app not working

    petclinic demo app not working

    Hi ,

    I got the below link in the bottom of kotlin-nosql github page.

    http://kotlin-nosql-mongodb-petclinic.herokuapp.com/

    But, the above link isn't working. Could you please fix it?

    opened by harshavmb 0
  • DateTimeHandling Incorrect in MongoDBSession::getObject(doc: DBObject, column: AbstractColumn<*, *, *>)

    DateTimeHandling Incorrect in MongoDBSession::getObject(doc: DBObject, column: AbstractColumn<*, *, *>)

    Hi,

    While using this package we've run into an issue with the dateTime() / DATE_TIME column type in MongoDBSession - while getObject(doc, schema) on line 489 properly handles DATE_TIME column type instantiation from the string pulled from Mongo, getObject(doc: DBObject, column: AbstractColumn<*, *, *>) on line 532 does not, and instead just does a basic doc.get which will fail for the DATE_TIME columns with something like:

    java.lang.IllegalArgumentException: Can not set final org.joda.time.DateTime field expirationDate to java.lang.String
    

    Let me know if you need further details or if there's anything else I can do to help. Thanks!

    opened by cojo 0
  • Update to Kotlin 1.0.0-beta-3595

    Update to Kotlin 1.0.0-beta-3595

    I'm super unfamiliar with this library. This PR probably needs a load of review; I just wanted it to compile on the latest version so I can use it in a project, which it does.

    I tried to go through and fix the build warnings that I felt comfortable fixing, but I don't know exactly what the state of the warnings were beforehand since it wouldn't compile. By no means did I remove them all, though :)

    Tests fail, but given the fact that it can't find assertEquals leads me to believe that they weren't always working. I didn't even try to fix them. Here's the output of gradle assemble test. https://gist.github.com/phroa/31c18af5c25c2f29a47a


    Comments appreciated.

    opened by phroa 0
Owner
Andrey Cheptsov
Andrey Cheptsov
Java embedded nosql document store

Nitrite Database NOsql Object (NO2 a.k.a Nitrite) database is an open source nosql embedded document store written in Java. It has MongoDB like API. I

Nitrite Database 713 Dec 23, 2022
Java embedded nosql document store

Nitrite Database NOsql Object (NO2 a.k.a Nitrite) database is an open source nosql embedded document store written in Java. It has MongoDB like API. I

Nitrite Database 713 Dec 23, 2022
A library for reading Shared Preferences and Database values within the application.

AppDataReader A library for reading and writing Shared Preferences and Database values of the application within the device. Advantages of using this

Anshul Jain 124 Nov 25, 2022
A quick and easy database manager plugin library for your DBFlow databases.

DBFlowManager A quick and easy database manager and viewer plugin library for your DBFlow databases to view, insert, delete, update the tables directl

Wajahat Karim 26 Oct 21, 2022
A helper library to help using Room with existing pre-populated database [].

Room now supports using a pre-packaged database out of the box, since version 2.2.0 https://developer.android.com/jetpack/androidx/releases/room#2.2.0

Ibrahim Eid 136 Nov 29, 2022
An Android helper class to manage database creation and version management using an application's raw asset files

THIS PROJECT IS NO LONGER MAINTAINED Android SQLiteAssetHelper An Android helper class to manage database creation and version management using an app

Jeff Gilfelt 2.2k Jan 7, 2023
Realm is a mobile database: a replacement for SQLite & ORMs

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Java version of Realm

Realm 11.4k Jan 9, 2023
A key-value database for Android

SnappyDB SnappyDB is a key-value database for Android it's an alternative for SQLite if you want to use a NoSQL approach. It allows you to store and g

Nabil Hachicha 1.8k Dec 28, 2022
ObjectBox is a superfast lightweight database for objects

ObjectBox Java (Kotlin, Android) ObjectBox is a superfast object-oriented database with strong relation support. ObjectBox is embedded into your Andro

ObjectBox 4.1k Dec 30, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Dec 16, 2022
Android Database Performance Benchmarks

ℹ Check out our new performance test app that includes ObjectBox. Android Database Performance Benchmark This project evaluates Android databases and

Markus Junginger 80 Nov 25, 2022
Test any type of cloud database on Android apps. No need of a dedicated backend.

Test any type of cloud database on Android apps. No need of a dedicated backend.

Arjun 9 May 9, 2022
Android with Real-time Database

Android with Real-time Database It was too much effort to build my own real-time database, but the result really satisfying, so it was worth it. Note

null 4 Jun 14, 2022
Android library for viewing and sharing in app databases.

DbInspector DbInspector provides a simple way to view the contents of the in-app database for debugging purposes. There is no need to pull the databas

Infinum 925 Dec 17, 2022
A small library to help with Realm.IO integration in Android apps

Android Realm Asset Helper A small library of methods to help with Realm.IO integration in Android apps Copies a realm database from a the assets fold

Quality Mobile Puzzle Apps 29 Dec 28, 2021
an android library for debugging what we care about directly in app.

EN | 中文 Pandora is a tool box that allows you to inspect and modify what includes networks, databases, UIs, etc. directly in your application. It is s

linjiang 1.5k Dec 30, 2022
Pref-DB - Android SharedPreferences alternative library

Pref-DB Android SharedPreferences alternative library.

M.Fakhri 5 Sep 14, 2022
SQLDelight - Generates typesafe Kotlin APIs from SQL

SQLDelight See the project website for documentation and APIs SQLDelight generates typesafe kotlin APIs from your SQL statements. It verifies your sch

Cash App 4.9k Jan 5, 2023
Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders

Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders.

Vehement 8 Nov 26, 2022
A simple NoSQL client for Android. Meant as a document store using key/value pairs and some rudimentary querying. Useful for avoiding the hassle of SQL code.

SimpleNoSQL A simple NoSQL client for Android. If you ever wanted to just save some data but didn't really want to worry about where it was going to b

Colin Miller 389 Sep 25, 2022