mitch
03/25/2022, 3:01 PMclientDefault
, 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:
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?spand
03/28/2022, 11:32 AMprivate 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.mitch
03/28/2022, 1:30 PMlazy
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.