Exposed Overview Kotlin
Exposed is a lightweight SQL library written in Kotlin, which provides a DSL (Domain Specific Language) for querying and manipulating SQL databases. It aims to simplify database interactions by providing a type-safe and fluent API.
In this tutorial, we will explore the history, features, and examples of Exposed Kotlin Framework.
History
Exposed was created by JetBrains as an alternative to existing ORM (Object-Relational Mapping) libraries for Kotlin. It was designed to be simple, lightweight, and easy to use, while still providing powerful database manipulation capabilities.
Feature
Type-safe DSL: Exposed provides a type-safe DSL for building SQL queries. This ensures that queries are checked at compile-time, reducing the chance of runtime errors.
val query = Users.select { Users.id eq 1 }Fluent API: The API provided by Exposed is fluent and expressive, allowing for easy construction of complex queries.
val query = Users.select { (Users.id eq 1) and (Users.name like "%John%") }Schema Definition: Exposed allows you to define database schemas using Kotlin code. This eliminates the need for separate SQL scripts and provides better type-safety.
object Users : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
}Transactions: Exposed supports transaction management, allowing you to group multiple database operations into a single transaction.
transaction {
Users.insert { it[name] = "John" }
Users.insert { it[name] = "Jane" }
}Querying: Exposed provides various methods for querying the database, including filtering, sorting, and joining.
val query = Users.select { Users.age greaterEq 18 }.orderBy(Users.name)Updating and Deleting: Exposed allows you to update and delete records in the database using a simple syntax.
Users.update({ Users.id eq 1 }) {
it[name] = "John Doe"
}
Users.deleteWhere { Users.age less 18 }Database Support: Exposed supports multiple database backends, including SQLite, MySQL, PostgreSQL, and H2.
Examples of Exposed Kotlin Framework
Example 1: Creating a User Table
object Users : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
val age = integer("age")
}
Example 2: Inserting Records
transaction {
Users.insert {
it[name] = "John"
it[age] = 25
}
Users.insert {
it[name] = "Jane"
it[age] = 30
}
}
Example 3: Querying Records
val query = Users.select { Users.age greaterEq 18 }.orderBy(Users.name)
for (row in query) {
val name = row[Users.name]
val age = row[Users.age]
println("$name, $age")
}
Example 4: Updating Records
Users.update({ Users.id eq 1 }) {
it[name] = "John Doe"
}
Example 5: Deleting Records
Users.deleteWhere { Users.age less 18 }
For more information, you can visit the official Exposed website: https://github.com/JetBrains/Exposed
Exposed Kotlin Framework provides a powerful and intuitive way to interact with SQL databases in Kotlin. Its type-safe DSL, fluent API, and support for various database backends make it a great choice for Kotlin developers.