Help Starting Out With Slick

88 views
Skip to first unread message

Samuel Ako

unread,
Mar 4, 2015, 8:56:54 PM3/4/15
to scala...@googlegroups.com
Hi, I'm new to Scala, Slick and Spray, which I'm using to develop my next application with PostgreSQL.
The documentation on slick is very sparse and hard to understand. Already, I've spent lots of time on this with little progress.
I'm gonna briefly outline what I've done, and then ask for help with next steps.


1. I have added Postgres and Slick to my app in build.sbt with the following lines: 

"com.typesafe.slick"  %%  "slick"       % "3.0.0.M1"
"postgresql"           %   "postgresql" % "9.1-901-1.jdbc4"


2. I connect to my postgres database as follows
 
val db = Database.forURL(
 url    
= s"jdbc:postgresql://localhost:5432/mytestdb",
 driver
= "org.postgresql.Driver"
)
implicit val session: Session = db.createSession()


3. I have a package named models where I plan on putting all models. Each model is as follows
class Student(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "Students") {
     
def name: Column[String] = column[String]("Stud_Name", O.PrimaryKey)
     
def name: Column[String] = column[String]("Stud_Name", O.PrimaryKey)

I wondering if It's wise to have each model in it's own class file, or if I should but all class defs in one file? FYI: I would have lots of models

3. I would also like to know what goes into the model class? Aside defining Columns, can I add utility methods to each class to simplify querying?


Thanks :)
Message has been deleted

Pedro Furlanetto

unread,
Mar 5, 2015, 7:34:46 AM3/5/15
to scala...@googlegroups.com
The model organisation is completely up to you. For example, I like to
have a some model classes in different files, each file representing a
"context", for example, Person.scala, would have Person, Address,
HairColor etc.

You can definitely have utility methods in the model classes. For
example, suppose you have a column `yearOfBirth` your Student class,
you could have a method `isAdult`, like:

def yearOfBirth: Column[Int] = column[String]("Stud_yob")
def isAdult: Column[Boolean] = (2015 - yearOfBirth) >= 21

Now you can query `students filter { _.isAdult }`.

Cheers
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Slick / ScalaQuery" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scalaquery+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/scalaquery/79fcc255-8c20-4ca7-9e20-d7687aa41c32%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sam A.

unread,
Mar 5, 2015, 9:40:04 AM3/5/15
to scala...@googlegroups.com
I've seen people use case classes in relation to slick models or so, not entirely sure what role those classes play.
But do case classes play any special role, or are they used to accomplish specific thing when using slick?
Thanks :)

Pedro Furlanetto

unread,
Mar 5, 2015, 8:06:08 PM3/5/15
to scala...@googlegroups.com
It's a common view that case classes are how Scala encodes ADTs [1].
In a more implementation-wise view, case classes are classes that
automatically implements hashCode, equals, unapply methods and an
apply method (constructor like) in a companion object [2]. (by
automatically I mean compiler magic).

Case classes aren't necessarily needed to make full use of Slick, but
arguably it gives you the necessary tools to make the most convenient
use of Slick the in form of the above methods.

[1] Not sure it's the best explanation:
http://en.wikipedia.org/wiki/Algebraic_data_type
[2] http://daily-scala.blogspot.com.br/2009/09/companion-object.html
> https://groups.google.com/d/msgid/scalaquery/3dfb330d-b65a-4214-920b-55574e4514a6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages