activate
activate copied to clipboard
Allow users define custom functions
My limited understanding of the Activate under the hood and some free time over the weekend resulted in this pull request. The idea is to allow users define context specific query functions and operators.
@fwbrasil could you please review and let me know what you think? Is this a worthwhile approach? Should I extend it to allow custom operators?
Hi Anton. Thanks for digging into this! :)
I'm working on a project using Activate and I also need to use custom functions, but in my case it should be used to store/recover an entity property. For instance:
class MyEntity(var updatedAt: Long) extends Entity
The "updatedAt" is a date value in the mysql database. To store it I need to use the from_unixtime function and to recover, unix_timestamp. I'm using a really hacky solution for this overriding the escape method for the mysql dialect.
I think we could work on something that works for queries and entity properties.
My idea is to have this class in Activate:
abstract class MappedValue[T](implicit tval: (=> T) => StatementSelectValue) {
val value: T
}
And use it:
case class UpdatedAt(value: Long) extends MappedValue[Long] {
def toStorage(column: String) = s"from_unixtime($column)"
def fromStorage(column: String) = s"unix_timestamp($column)"
}
class MyEntity(var updatedAt: UpdatedAt) extends Entity
object Main extends App {
transactional {
new MyEntity(UpdatedAt(112221))
}
transactional {
val entity = select[MyEntity].where(_.updatedAt :== UpdatedAt(112221)).head
}
}
wdyt?
So it's definitely useful to be able to run storage specific functions on either parts of the select/update/insert statements, e.g. SELECT unix_timestamp(date_time_column) FROM a_table WHERE abs(int_column) > 10
or INSERT INTO a_table VALUES (from_timestamp(21344323432), pow(3, 4))
It's fairly straightforward to plug them into SQL based backends, and probably not very challenging for Mongo as well. But the memory context (LiveCache) seems to be more challenging. Unless we make these features available only for contexts with some sort of backend.
Hey, sorry for taking so long to answer.
Good catch about the in-memory queries. We could have an abstract method in the MappedValue class to return the value for in-memory queries.
I will work to cut the 1.5 version and should be able to work with you on this following.
Cheers
@fwbrasil will you have time to tackle this issue? Which approach do you think makes the most sense to use?