Mehedi Hassan Piash [Sr. Software Engineer]

September 03, 2022

Exposed BaseTable for generating createdAt and updatedAt in Ktor part-4

September 03, 2022 Posted by Piash , No comments

 ktor BaseTable in terms of IntIdTable

import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.sql.ReferenceOption
import org.joda.time.DateTime
import org.joda.time.DateTimeZone

fun currentUtc(): DateTime = DateTime.now(DateTimeZone.UTC)

abstract class BaseIntIdTable(name: String) : IntIdTable(name) {
val createdAt = datetime("createdAt").clientDefault { currentUtc() }
val updatedAt = datetime("updatedAt").nullable()
}

abstract class BaseIntEntity(id: EntityID<Int>, table: BaseIntIdTable) : IntEntity(id) {
val createdAt by table.createdAt
var updatedAt by table.updatedAt
}

abstract class BaseIntEntityClass<E : BaseIntEntity>(table: BaseIntIdTable) : IntEntityClass<E>(table) {

init {
EntityHook.subscribe { action ->
if (action.changeType == EntityChangeType.Updated) {
try {
action.toEntity(this)?.updatedAt = currentUtc()
} catch (e: Exception) {
//nothing much to do here
}
}
}
}
}

val BaseIntEntity.idValue: Int
get() = this.id.value

object Users : BaseIntIdTable("users") {
val name = varchar("name", length = 60)
val role = reference("roleId", Roles, onDelete = ReferenceOption.NO_ACTION).nullable()
}

class User(id: EntityID<Int>) : BaseIntEntity(id, Users) {
companion object : BaseIntEntityClass<User>(Users)

var name by Users.name
var role by Role optionalReferencedOn Users.role
}
import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.javatime.datetime
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.UUID
// generating utc time
fun currentUtc(): LocalDateTime = LocalDateTime.now(ZoneOffset.UTC)
abstract class BaseIntIdTable(name: String) : IdTable<String>(name) {
override val id: Column<EntityID<String>> =varchar("id", 50).clientDefault { UUID.randomUUID().toString() }.entityId()
val createdAt = datetime("created_at").clientDefault { currentUtc() }
val updatedAt = datetime("updated_at").nullable()
}

abstract class BaseIntEntity(id: EntityID<String>, table: BaseIntIdTable) : Entity<String>(id) {
val createdAt by table.createdAt
var updatedAt by table.updatedAt
}
abstract class BaseIntEntityClass<E : BaseIntEntity>(table: BaseIntIdTable) : EntityClass<String, E>(table){
init {
EntityHook.subscribe { action ->
if (action.changeType == EntityChangeType.Updated) {
try {
action.toEntity(this)?.updatedAt = currentUtc()
} catch (e: Exception) {
//nothing much to do here
}
}
}
}
}

object Users : BaseIntIdTable("users") {
val name = varchar("name", length = 60)
val role = reference("roleId", Roles, onDelete = ReferenceOption.NO_ACTION).nullable()
}

class User(id: EntityID<Int>) : BaseIntEntity(id, Users) {
companion object : BaseIntEntityClass<User>(Users)

var name by Users.name
var role by Role optionalReferencedOn Users.role
}

Ref:
https://piashcse.medium.com/exposed-basetable-for-generating-createdat-and-updatedat-in-ktor-part-3-581511fdfa38