How do I store user case objects with squeryl? I have an Account object with a permission field of type Permission (defined as a sealed trait). I also have 2 case objects (Administrator and NormalUser) extending from Permission. How can I persist the Account class using Squeryl. Example code below:
sealed trait Permission
case object Administrator extends Permission
case object NormalUser extends Permission
case class Account(
id: Long,
email: String,
permission: Permission
) extends KeyedEntity[Long]
Expanding on my comment, if you use a custom type to retrieve the permission type, such that it persists to the database as an integer (in the example below 1 and 0), you can override the unapply method to lookup the case object and pattern matching should work fine. I imagine something like the following should work:
class Permission(identifier:Int) extends org.squeryl.customtypes.IntField(identifier) {
self: CustomType[Int] =>
private lazy val permissions =
List(Administrator, NormalUser).
map(p => p.value -> p).
toMap
def unapply = permissions.get(value)
}
case object Administrator extends Permission(1)
case object NormalUser extends Permission(0)
Then you should be able to store the permission directly in your code, using your entity definition:
case class Account(
id: Long,
email: String,
permission: Permission
) extends KeyedEntity[Long]
you can set the permission
field directly as Administrator
or NormalUser
and you should also be able to pattern match like:
account.permission match {
case Administrator => ..
case NormalUser => ..
}