/xodus-dnq

Data definition and queries Kotlin DSL over Xodus

Primary LanguageKotlinApache License 2.0Apache-2.0

Xodus-DNQ

official JetBrains project Maven Central Build Status License Pure Java + Kotlin Stack Overflow

Xodus-DNQ is a Kotlin library that contains the data definition language and queries for Xodus, a transactional schema-less embedded database. Xodus-DNQ provides the same support for Xodus that ORM frameworks provide for SQL databases. With Xodus-DNQ, you can define your persistent meta-model using Kotlin classes.

JetBrains team tools YouTrack and Hub use Xodus-DNQ for persistent layer definition.

More documentation https://jetbrains.github.io/xodus-dnq.

Quick Start Guide

Install to your project

Maven Central

List of released versions is available at https://github.com/JetBrains/xodus-dnq/releases.

Gradle

repositories {
    mavenCentral()
}
compile 'org.jetbrains.xodus:dnq:${version}'

Maven

<dependency>
    <groupId>org.jetbrains.xodus</groupId>
    <artifactId>dnq</artifactId>
    <version>$version</version>
</dependency>

Use Xodus-DNQ

See code in repository.

// Define persistent class. It should extend XdEntity
class XdPost(entity: Entity) : XdEntity(entity) {
    //  and have component object of type XdEntityType
    companion object : XdNaturalEntityType<XdPost>()

    // Define persistent property of type org.joda.time.DateTime?
    var publishedAt by xdDateTimeProp()

    // Define required persistent property of type String
    var text by xdRequiredStringProp()
}

class XdBlog(entity: Entity) : XdEntity(entity) {
    companion object : XdNaturalEntityType<XdBlog>()

    // Define multi-value link to XdPost
    val posts by xdLink0_N(XdPost)
}

fun main(args: Array<String>) {
    // Register persistent classes
    XdModel.registerNodes(XdPost, XdBlog)

    // Initialize Xodus persistent storage
    val xodusStore = StaticStoreContainer.init(
            dbFolder = File(System.getProperty("user.home"), ".xodus-dnq-blog-db"),
            environmentName = "db"
    )

    // Initialize Xodus-DNQ metadata
    initMetaData(XdModel.hierarchy, xodusStore)

    // Do in transaction
    val blog = xodusStore.transactional {
        // Find an existing blog in database
        XdBlog.all().firstOrNull()
                // or create a new one if there are no blogs yet
                ?: XdBlog.new()
    }

    xodusStore.transactional {
        // Create new post
        val post = XdPost.new {
            this.publishedAt = DateTime.now()
            this.text = args.firstOrNull() ?: "Empty post"
        }

        // Add new post to blog
        blog.posts.add(post)
    }

    // Do in read-only transaction
    xodusStore.transactional(readonly = true) {
        // Print all blog posts
        for (post in blog.posts) {
            println("${post.publishedAt}: ${post.text}")
        }
    }
}

Find out more