https://kotlinlang.org logo
#exposed
Title
# exposed
m

mitch

03/25/2022, 3:01 PM
Hi, expanding on DAO#auto-fill-created-and-updated-columns-on-entity-change which uses
clientDefault
, are there any recommendations on what to do if I want
updatedAt
to be set to the same
datetime
as
insertedAt
when we do a
new()
? https://github.com/JetBrains/Exposed/issues/342#issuecomment-540487409 gives a nice server-side answer,
.defaultExpression(CurrentDateTime())
, but what if we want it client side so that the caller has the value set without committing the transaction? I noticed that the `clientDefault`s are `invoke`d in the order the columns are registered. Is is acceptable to rely on that behavior? If so, I could do something like:
Copy code
private data class ControlledUtc(var currentTime: LocalDateTime = utcNow()) {
    fun getRefreshed(): LocalDateTime = utcNow().also { currentTime = it }
    fun get(): LocalDateTime = currentTime
}

private val controlledUtc = ControlledUtc()

val insertedAt = datetime("inserted_at").clientDefault { controlledUtc.getRefreshed() }

var updatedAt = datetime("updated_at").clientDefault { controlledUtc.get() }
Thoughts? Other ideas?
s

spand

03/28/2022, 11:32 AM
I think you requirements could be solved by something like this
Copy code
private class ControlledUtc {
    val currentTime by lazy { utcNow() }
}

private val controlledUtc = ControlledUtc()

val insertedAt = datetime("inserted_at").clientDefault { controlledUtc.get() }

var updatedAt = datetime("updated_at").clientDefault { controlledUtc.get() }
but I cannot see how you would use something like this while still being thread safe and such.
m

mitch

03/28/2022, 1:30 PM
Thanks. The use of
lazy
is a nice improvement, but I still think we need the
getRefreshed
to handle multiple calls to
new
. Yeah, I'm concerned about thread safety.
6 Views