JAKO: Just Another Kotlin Orm (PostgreSQL)

Related tags

O/R Mapping kotlin orm
Overview

JAKO: Just Another Kotlin Orm (PostgreSQL)

JAKO

JAKO is a simple, minimal, no-dependency library to build and execute postgresql statements using a fluent dsl.

Main features:

  • Easy to use
  • Statement builders totally independent of execution
  • Easy to use custom connectors (like HikariCP or others)
  • No need to define table structures
  • RawStatement class in order to execute not yet supported SQL syntax
  • fluent transactions

Add JAKO to your project

Gradle

  • Add JitPack in your root build.gradle at the end of repositories:
repositories {
    maven { url 'https://jitpack.io' }
}
  • Add the dependency along with postgresql driver
dependencies {
    implementation 'org.postgresql:postgresql:42.3.3'
    implementation 'com.github.AlessioCoser:jako:0.0.9'
}

Maven

  • Add the JitPack repository to your build file
<repositories>
    <repository>
        <id>jitpack.ioid>
        <url>https://jitpack.iourl>
    repository>
repositories>
  • Add the dependency along with postgresql driver
<dependency>
    <groupId>org.postgresqlgroupId>
    <artifactId>postgresqlartifactId>
    <version>42.3.3version>
dependency>
<dependency>
    <groupId>com.github.AlessioCosergroupId>
    <artifactId>jakoartifactId>
    <version>0.0.9version>
dependency>

Getting Started

Build

Query

?) println(query.params()) // [Milano, 3]">
val query = Query.from("users")
    .join("pets" ON "pets.owner" EQ "users.id")
    .where(("city" EQ "Milano") AND ("age" GT 3))

println(query.toString())
// SELECT * FROM "users" INNER JOIN "pets" ON "pets"."owner" = "users"."id" WHERE ("city" = ? AND "age" > ?)
println(query.params())
// [Milano, 3]

Insert

val insert = Insert.into("users")
    .set("id", 1)
    .set("name", "Mario")
    .set("city", "Milano")
    .set("age", 30)

println(insert.toString())
// INSERT INTO "users" ("id", "name", "city", "age") VALUES (?, ?, ?, ?)
println(insert.params())
// [1, Mario, Milano, 30]

Update

val update = Update.table("users")
    .set("age", 31)
    .where("id" EQ 1)

println(update.toString())
// UPDATE "users" SET "age" = ? WHERE "id" = ?
println(update.params())
// [31, 1]

Delete

val delete = Delete.from("users")
    .where("id" EQ 1)

println(insert.toString())
// DELETE FROM "users" WHERE "id" = ?
println(insert.params())
// [1]

Execute

Query

Select all id fields from users as Ints.

= db.select(query).all { int("id") }">
val db = Database.connect("jdbc:postgresql://localhost:5432/database?user=user&password=password")
val query = Query.from("users")

val tableIds: List<Int> = db.select(query).all { int("id") }

Select first id as Int from users.

val db = Database.connect("jdbc:postgresql://localhost:5432/database?user=user&password=password")
val query = Query.from("users")

val tableIds: Int? = db.select(query).first { int("id") }

Another Statement

val db = Database.connect("jdbc:postgresql://localhost:5432/database?user=user&password=password")
val insert = Insert
    .into("customers")
    .set("name", "Carlo")
    .set("age", 18)

db.execute(insert)

Raw Statement

Use RawStatement for SQL syntax not yet supported by the library.

val db = Database.connect("jdbc:postgresql://localhost:5432/database?user=user&password=password")
val query = RawStatement("""SELECT "id" FROM "users" WHERE "city" = ?""", listOf("Milano"))

val tableIds: Int? = db.select(query).first { int("id") }

Execute statements in Transaction

Using useTransaction method you can run all db execute safely. When something goes wrong and an execution throws an exception the changes are automatically rollbacked.

val db = Database.connect("jdbc:postgresql://localhost:5432/database?user=user&password=password")

db.useTransaction {
    db.execute(Insert.into("users").set("name", "Mario"))
    db.execute(Insert.into("users").set("name", "Paolo"))
    db.execute(Insert.into("users").set("name", "Carlo"))
}

Custom Connectors

If you create a database instance without a custom connector the library use a SimpleConnector which adopt the standard DriverManager.getConnection() method to get a new database connection.

val db = Database.connect("jdbc:postgresql://localhost:5432/tests?user=user&password=password")

If you want to use this library in production we recommend to use a CustomConnector so you can use your connection pool/cache library.

So you have to create a database instance in this way:

val customConnector: DatabaseConnector = MyCustomConnector("jdbc:postgresql://localhost:5432/tests?user=user&password=password")
val db = Database.connect(customConnector)

In the example below we will create a Connector for HikariCP

HikariCP Custom Connector Example

1. Add HikariCP to the project dependencies

Add to dependencies:

"com.zaxxer:HikariCP:4.0.3"
// for java 11 compatibility use the version 5.0.1

2. Create the custom Connector

class HikariConnector(jdbcUrl: String, poolSize: Int = 10) : DatabaseConnector {
    private val dataSource = HikariDataSource().also {
        it.driverClassName = "org.postgresql.Driver"
        it.jdbcUrl = jdbcUrl
        it.maximumPoolSize = poolSize 
        // start with this: ((2 * core_count) + number_of_disks)
    }

    override fun connection(): Connection {
        return dataSource.connection
    }
}

3. Use it

val customConnector: DatabaseConnector = HikariConnector("jdbc:postgresql://localhost:5432/tests?user=user&password=password")
val db = Database.connect(customConnector)
You might also like...
lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

An ORM for Android with type-safety and painless smart migrations
An ORM for Android with type-safety and painless smart migrations

Android Orma Orma is a ORM (Object-Relation Mapper) for Android SQLiteDatabase. Because it generates helper classes at compile time with annotation pr

Performance comparison of Android ORM Frameworks
Performance comparison of Android ORM Frameworks

Performance comparison of Android ORM Frameworks At the moment there are a lot of ORM-libraries for the Android OS. We reviewed the most popular ones

Kotlin-Exposed-SQL - Example of using Exposed with Kotlin for the consumption of relational SQL Databases
Kotlin-Exposed-SQL - Example of using Exposed with Kotlin for the consumption of relational SQL Databases

Kotlin Exposed SQL Sencillo ejemplo sobre el uso y abuso de Exposed ORM de Jetbr

requery - modern SQL based query & persistence for Java / Kotlin / Android
requery - modern SQL based query & persistence for Java / Kotlin / Android

A light but powerful object mapping and SQL generator for Java/Kotlin/Android with RxJava and Java 8 support. Easily map to or create databases, perfo

A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.

Thanks to JetBrains for support Kripton Persistence Library project! Kripton Persistence Library Kripton is a java library, for Android platform, that

Upsert DSL extension for Exposed, Kotlin SQL framework

Exposed Upsert Upsert DSL extension for Exposed, Kotlin SQL framework. Project bases on various solutions provided by community in the official "Expos

Samples demonstrating the usage of Realm-Kotlin SDK
Samples demonstrating the usage of Realm-Kotlin SDK

Realm-Kotlin Samples This repository contains a set of projects to help you learn about using Realm-Kotlin SDK Each sample demonstrates different use

Collection of Kotlin APIs/tools to make using Realm Mobile database easier

Compass Kotlin API and tools to make working with Realm easier Components Compass is designed to make working with Realm easier through collection of

Owner
Alessio
I'm an XP Software Developer, I am keen at exploring new technologies and architectures. I love coding with Kotlin, Ruby and JavaScript
Alessio
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Check out ObjectBox Check out our new mobile database ObjectBox (GitHub repo). ObjectBox is a superfast object-oriented database with strong relation

Markus Junginger 12.6k Jan 3, 2023
Android ORM

Shillelagh Shillelagh is an sqlite library. It was built to make life easier. The entire library was built around simplicity when using sqlite in Andr

Andrew Reitz 49 Sep 11, 2020
Compile-time active record ORM for Android

Ollie Compile-time active record ORM for Android. Multiple mapping methods. SQLiteDatabase-like interface (QueryUtils.java). Lightweight query builder

Michael Pardo 423 Dec 30, 2022
a 3d database ORM experiment. (used in two commercial projects)

Android-TriOrm a 3d database ORM experiment for Android. (used in two commercial projects). based around small tables concept and JVM Serialization. H

Tomer Shalev 19 Nov 24, 2021
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

Ahmet Alp Balkan 246 Nov 20, 2022
ORMDroid is a simple ORM persistence framework for your Android applications.

ORMDroid is a simple ORM persistence framework for your Android applications, providing an easy to use, almost-zero-config way to handle model persist

Ross Bamford 87 Nov 10, 2022
AndroidQuery is an Android ORM for SQLite and ContentProvider which focuses on easy of use and performances thanks to annotation processing and code generation

WARNING: now that Room is out, I no longer maintain that library. If you need a library to easy access to default android ContentProvider, I would may

Frédéric Julian 19 Dec 11, 2021
LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.

#LiteOrm:Android高性能数据库框架 A fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line

马天宇 1.5k Nov 19, 2022