Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nomisrev/kotlin-jdbc-dsl
A simple Kotlin DSL for `javax.sql.DataSource`
https://github.com/nomisrev/kotlin-jdbc-dsl
datasource javax jvm kotlin kotlin-dsl sql
Last synced: 21 days ago
JSON representation
A simple Kotlin DSL for `javax.sql.DataSource`
- Host: GitHub
- URL: https://github.com/nomisrev/kotlin-jdbc-dsl
- Owner: nomisRev
- License: apache-2.0
- Created: 2023-09-28T14:34:05.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-18T13:30:37.000Z (10 months ago)
- Last Synced: 2024-01-18T16:52:24.369Z (10 months ago)
- Topics: datasource, javax, jvm, kotlin, kotlin-dsl, sql
- Language: Kotlin
- Homepage:
- Size: 86.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Module Kotlin JDBC DSL
A tiny and simple wrapper around `javax.sql.DataSource` to make working directly with it a bit more convenient in Kotlin.
```text
implementation("io.github.nomisrev.jdbc:latest")
```## Why?
In a lot of cases I've needed very little functionality, and simply wanted to run some simple queries against a database.
Using an ORM was unnecessary for my use-cases, and I've from time-to-time implemented small wrappers around JDBC like this.So now I'm exposing it as a micro-lib, so I (and you) can depend on it from a common place and benefit from this DSL style.
## How
The DSL is exposes a `connection` DSL function on `javax.sql.DataSource`.
```kotlin
private val createUserTable: String =
"""CREATE TABLE IF NOT EXISTS users(
id BIGSERIAL PRIMARY KEY,
email VARCHAR(200) NOT NULL UNIQUE,
username VARCHAR(100) NOT NULL UNIQUE
)""".trimIndent()fun DataSource.createTable(): Int =
connection { update(createUserTable) }
``````kotlin
private val selectUser: String =
"""SELECT email, username
FROM users
WHERE id = ?;""".trimIndent()fun DataSource.getUser(id: Long): User? =
connection {
queryOrNull(selectUser, { bind(id) }) {
User(id = id, email= string(), username = string())
}
}
```