-
Notifications
You must be signed in to change notification settings - Fork 46
Allow users define custom functions #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
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. 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fwbrasil what is the purpose of entityValue in this context? I was trying to trace it, but couldn't find any use of it for FunctionApply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akhodakivskiy It is used to run the in-memory queries.
|
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? |
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?