Mehedi Hassan Piash [Sr. Software Engineer]

October 23, 2021

Kotlin Exposed create an entity with reference for Ktor part-2

October 23, 2021 Posted by Piash No comments

 

PostgreSql UserTable

UserTable Entity for Ktor

import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column

object UsersTable : IdTable<String>("users") {
override val id: Column<EntityID<String>> = text("user_id").uniqueIndex().entityId()
val user_name = text("user_name")
val email = text("email")
val password = text("password")
val mobile_number = text("mobile_number").nullable()
val email_verified_at = text("email_verified_at").nullable() // so far unkmown
val remember_token = text("remember_token").nullable()
val verification_code = text("verification_code").nullable() // verification_code
val created_at = text("created_at").nullable()
val updated_at = text("updated_at").nullable()
val is_verified = text("is_verified").nullable() // email verified by validation code
override val primaryKey = PrimaryKey(id)
}

class UsersEntity(id: EntityID<String>) : Entity<String>(id) {
companion object : EntityClass<String, UsersEntity>(UsersTable)
var userId by UsersTable.id
var user_name by UsersTable.user_name
var email by UsersTable.email
var password by UsersTable.password
var mobile_number by UsersTable.mobile_number
var email_verified_at by UsersTable.email_verified_at
var remember_token by UsersTable.remember_token
var verification_code by UsersTable.verification_code
var created_at by UsersTable.created_at
var updated_at by UsersTable.updated_at
var is_verified by UsersTable.is_verified
var userType by UserTypeEntity via UserTypeTable
fun userResponse() = UsersResponse(userId.value, user_name,email, mobile_number, email_verified_at, remember_token, created_at, updated_at, is_verified)
}
data class UsersResponse( val userId :String,
val userName :String,
val email :String,
val mobileNumber:String?,
val emailVerifiedAt :String?,
val rememberToken:String?,
val createdAt:String?,
val updatedAt :String?,
val isVerified :String?)

UserHasTypeTable Entity

import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column

object UserHasTypeTable : IdTable<String>("user_has_type") {
override val id: Column<EntityID<String>> = text("user_has_type_id").uniqueIndex().entityId()
val user_id = text("user_id").references(UsersTable.id)
val user_type_id = text("user_type_id")
val created_at = text("created_at")
val updated_at = text("updated_at")
override val primaryKey = PrimaryKey(id)
}

class UserHasTypeEntity(id: EntityID<String>) : Entity<String>(id) {
companion object : EntityClass<String, UserHasTypeEntity>(UserHasTypeTable)
var id_ by UserHasTypeTable.id
var user_id by UserHasTypeTable.user_id
var user_type_id by UserHasTypeTable.user_type_id
var created_at by UserHasTypeTable.created_at
var updated_at by UserHasTypeTable.updated_at
var users by UsersEntity referencedOn UserHasTypeTable.user_id
}

Ref: https://piashcse.medium.com/kotlin-exposed-create-entity-with-reference-for-ktor-part-2-1905836c400c

October 20, 2021

PostgreSQL database connection in Ktor part-1

October 20, 2021 Posted by Piash , No comments
PostgreSQL database connection in Ktor. 

 build.gradle dependency

// Exposed ORM library
implementation "org.jetbrains.exposed:exposed-core:0.35.1"
implementation "org.jetbrains.exposed:exposed-dao:0.35.1"
implementation "org.jetbrains.exposed:exposed-jdbc:0.35.1"
implementation "org.postgresql:postgresql:42.2.2"
implementation 'com.zaxxer:HikariCP:3.4.2'

resource/hikari.properties

dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=postgres
dataSource.password=p123
dataSource.databaseName=db_ktor
dataSource.portNumber=5432
dataSource.serverName=localh

Database helper class

object DatabaseFactory {
fun init() {
// Database.connect(hikari())
initDB()
transaction {
//create(Fruits, UserTable, ProductTable)
}
}

private fun initDB() {
// database connection is handled from hikari properties
val config = HikariConfig("/hikari.properties")
val ds = HikariDataSource(config)
Database.connect(ds)
}

// database connection for h2 d
private fun hikari(): HikariDataSource {
val config = HikariConfig()
config.driverClassName = "org.h2.Driver"
config.jdbcUrl = "jdbc:h2:file:~/documents/db/h2db"
config.maximumPoolSize = 3
config.isAutoCommit = false
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
config.validate()
return HikariDataSource(config)
}
}

Application class

fun main() {
//val environment = System.getenv("KTOR_ENVIRONMENT") ?: "development"
val configName = "application.conf"
val appEngineEnv = applicationEngineEnvironment {
config = HoconApplicationConfig(ConfigFactory.load(configName))
log = LoggerFactory.getLogger("ktor.application")
module {
DatabaseFactory.init()
}
connector {
host = config.property("ktor.deployment.host").getString()
port = config.property("ktor.deployment.port").getString().toInt()
}
}

embeddedServer(Netty, appEngineEnv).start(wait = true)
}