I'm using Scala, and new to Play and Slick. I'm starting to block out a simple database structure and I'm not sure about the correct way to handle foreign keys and projections. In the example at page bottom it currently doesn't compile because a ForeignKey cannot be lifted directly, so what's the correct way to have query results lift into my type (which is basically this sans methods and additional constructors):
case class UserCompanyPermission(pk: UUID, company: Company, user: User, accessLevel: CompanyPermissionLevel)
Either I'd like to have the type projection return a UserCompanyPermission, otherwise have a way to wrap everything coming into and out of the DAO methods so from the outside I'm just passing my Scala Types in. Basically I want to ensure my business logic is entirely decoupled from state to make it easier to test, so being able to keep all table specifics restricted to this storage package. Is their way to wrap, or is each method I write in the DAO object going to need to make the conversion on its own?
trait CompaniesComponent { this: UsersComponent =>
val Companies: Companies
val UserCompanyPermissions: UserCompanyPermissions
implicit val companyPermissionLevelTypeMapper = MappedTypeMapper.base[CompanyPermissionLevel.Value, Int](
{ level => level.id }, { id => CompanyPermissionLevel(id) }
)
class Companies extends Table[Company]("Company") {
def pk = column[UUID]("pk", O.PrimaryKey)
def subdomain = column[String]("subdomain", O.NotNull)
def name = column[String]("name", O.NotNull)
def * = pk ~ subdomain ~ name <> (Company.apply _, Company.unapply _)
}
class UserCompanyPermissions extends Table[UserCompanyPermission]("UserCompanyPermission") {
def pk = column[UUID]("pk", O.PrimaryKey)
def company_pk = column[UUID]("company_pk", O.NotNull)
def user_pk = column[UUID]("user_pk", O.NotNull)
def accessLevel = column[CompanyPermissionLevel.Value]("access_level", O.NotNull)
def company = foreignKey("company_pk", company_pk, Companies)(_.pk)
def user = foreignKey("user_pk", user_pk, Users)(_.pk)
def * = pk ~ company ~ user ~ accessLevel <> (UserCompanyPermission.apply _, UserCompanyPermission.unapply _)
}
}
object Companies extends DAO {
def insert(company: Company)(implicit session: Session) {
Companies.insert(company)
}
}
object UserCompanyPermissions extends DAO {
def insert(perm: UserCompanyPermission)(implicit session: Session) {
UserCompanyPermissions.insert(perm)
}
}