-
Notifications
You must be signed in to change notification settings - Fork 348
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
Easy CRUD #30
Comments
It's the first draft and i'll be updating it as soon i can with more details.So let's start a discuss about the idea.I'd like to proposal an implementation based on Spring Data but that use macro to create and implement the repository in compile time Exampleimport io.getquill.data.Repository
// Define the model
case class UserId(value:Long) extend AnyVal
case class User(id:UserId, name:String, age:Int, email:String, password:String, createdAt:DateTime)
// Define the repository trait
trait UserRepository extends Repository[User] {
def insert(name:String, age:Int, email:String, password:String):UserId
def updateById(id:UserId, name:String, age:String): Long
def deleteById(id:UserId): Boolean
def findById(id:UserId):Option[User]
def findByEmail(email:String):List[User]
def findByAgeGreaterThan(age:Int):List[User]
}
// Macro to generate the implementation
val userRepository = repository[UserRepository](db) MacroMacro will expand and implement the val userRepositoy = repository[UserRepositoy](db) val userRepository = new UserRepository {
protected val db = db
def insert(name:String, age:Int, email:String, password:String):UserId = {
val q = quote {
query[User].insert(
_.name -> name,
_.email -> email,
_.password -> password
}
db.run(returningId(q))
}
def updateById(id:UserId, name:String, age:Int): Long = {
val q = quote {
query[User].filter(_.id == id).update(_.name -> name, _.age -> age)
}
db.run(q)
}
def deleteById(id:UserId): Boolean = {
val q = quote {
query[User].filter(_.email == email).delete
}
db.run(q)
}
def findById(id:UserId):Option[User] = {
val q = quote {
query[User].filter(_.id == id)
}
db.run(q).headOption
}
def findByEmail(email:String):List[User] = {
val q = quote {
query[User].filter(_.email == email)
}
db.run(q)
}
def findByAgeGreaterThan(age:Int):List[User] = {
val query = quoute {
query[User].filter(_.age > age)
}
db.run(query)
}
} Find Method |
Sounds great, @rfranco! Let me know if you need help with it. |
I vote for the Rich Domain Model implementation, i.e., something like Active Record pattern. It is basically just a shortcut for current API, so I do not see a point to design it as a repository. For inspiration see scala-activerecord |
Just checking since this is a fairly old issue. The only way to really achieve this is through macros, right? Has anything changed with Quill since this issue was created that could open up a different approach? To use Quill in a bigger project, I think I'd really need a feature like this to reduce the copy/paste with common CRUD operations. If nobody is working on this, I might be able to take a look. But to be honest, getting into the weeds with macros scares me a little... 😅 |
You might want to look @ https://github.com/ajozwik/quill-generic |
* remove deprecated dotty support - remove deprecated dotty supporting features - introduce scalafmt plugin - show warning on compile * remove checkbox because plugin automatically do format * rebase change duplicated #17 * replace interim _2.13 deps with 3 deps * use dotty latest version Co-authored-by: Alexander Ioffe <deusaquilus@gmail.com>
Provide a simple and clean API for CRUD operations.
For inspiration:
https://spring.io/guides/gs/accessing-data-jpa/
https://github.com/strongtyped/active-slick
The text was updated successfully, but these errors were encountered: