1

I have just started using slick and have found myself writing duplicate code for common queries. What techniques have people used to share common queries across Models? For example, in the MyModel object that extends Table[SomeOtherType], I might have something like:

def all : List[SomeOtherType] = DB.withSession { implicit session : Session =>  
  (for(record <- MyModel) yield record).list
}

so that I can just write MyModel.all. How can I extend all of my tables with this behavior?

mushroom
  • 6,201
  • 5
  • 36
  • 63

1 Answers1

0

We talk about this in our Scala Days 2013 talk http://slick.typesafe.com/docs/#20130612_slick_vs_orm_scaladays_2013 .

You have to define functions (or method extensions/implicit classes) that take a query as a parameter.

def all[T, E](q: Query[Any, E]) = db.withSession { implicit session: Session =>
  q.list
}

// usage in Slick 1.0
all(Query(MyModel))

In case you are thinking about making it a member of your table object, I would recommend you don't. It will make migration to 2.0 easier (and makes sense in my eyes).

cvogt
  • 11,260
  • 30
  • 46