Skip to content
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

Closed
fwbrasil opened this issue Dec 8, 2015 · 5 comments
Closed

Easy CRUD #30

fwbrasil opened this issue Dec 8, 2015 · 5 comments

Comments

@fwbrasil
Copy link
Collaborator

fwbrasil commented Dec 8, 2015

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

@rfranco
Copy link
Contributor

rfranco commented Dec 11, 2015

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

Example

import 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)

Macro

Macro will expand and implement the UserRepository as

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

Support keywords inside method name

@fwbrasil
Copy link
Collaborator Author

Sounds great, @rfranco! Let me know if you need help with it.

@KarelCemus
Copy link

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

@reibitto
Copy link

reibitto commented Oct 1, 2020

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... 😅

@b-gyula
Copy link
Contributor

b-gyula commented Apr 12, 2021

You might want to look @ https://github.com/ajozwik/quill-generic

jilen pushed a commit that referenced this issue Jun 11, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants