This. Slick might be the one true way that typesafe and other purveyors of Scala might believe we should be using to interacting with the database. But not all of us can start there. We'd like a simple ORM that can provide some abstractions and facilities (auto-generated findBy, relational mapping). Finally we went with scala-activerecord which seems to have hit the sweet spot between providing just enough functionality to be an ORM but not blowing up to be a complex beast with secret incantations (ala Hibernate).
All the derision that Rod Johnson got for his keynote where he mentioned the lack of ORM (among other things) as being intimidating to a beginner seems unfair.
This looks great. Seriously all of the answers you give seem simple and convenient ways of doing things.
You could reduce your first example to def findById(userId: Int)(implicit s: Session) = Users.filter(_.id === userId).firstOption
Re:Akka, Spray and Play to come Together
by terhechte on Friday October 18, 2013 @01:28PM
(#6572600)
Certainly true, but when you're just starting out, that kind of thinking may not be there yet. You need some time with Slick to understand this, and it is difficult to get into it because it lacks the simple facilities. I actually spend a lot of time searching for these simple facitilies until I realized that they're really not there yet.
def findById(userId: Int)(implicit session: Session): Option[User] = { val query = for{ u <- Users if u.id === userId } yield u query.firstOption }
http://stackoverflow.com/questions/16461260/select-single-ro...
However, that's not even the brink of the iceberg. Try finding out how to update multiple fields in an object. Say you retrieve a User object, and you want to set a new email, zip, and address. I'd suppose, this would work with simple getters and setters, i.e.:
user = Users.get(user_id) user.email = new_mail user.zip = new_zip user.address = new_address
Instead, you have to do this:
val map = Query(User) .filter(_.id === user_id) .map(ab => ab.email ~ ab.zip ~ ab.address)
map.update((new_mail, new_zip, new_address))
And even that only works with updateable result sets. See: http://stackoverflow.com/questions/16757368/how-do-you-updat...
https://groups.google.com/forum/#!msg/scalaquery/ML56aZAfy3g...
Or, to quote from the stackoverflow answer above: "Typesafe, why your documentation is so bad ? I have to Google pretty much every silly thing or dig through unit-tests for hours. Please improve it. Thanks."
Now, after a lot of searching, I found solutions to all of my problems, but it took a long time, lots of Google, and almost nothing came out of their awful documentation.