scalaphantom-dsl

Phantom Mapping Java 8 LocalDateTime to Joda Time


I am using Phantom 1.28 with Cassandra 3.7.

I have a case class with Java 8 LocalDateTime:

case class User(
   verifiedAt: Option[LocalDateTime] = None
)

In phantom model:

class UserModel extends CassandraTable[ConcreteUserModel, User]{

object verified_at extends OptionalDateTimeColumn(this)

override def fromRow(r: Row): User = User(
    verified_at(r),  // <- compile error
)
}

Compile error:

error: type mismatch;
[ERROR]  found   : Option[com.websudos.phantom.dsl.DateTime]
[ERROR]     (which expands to)  Option[org.joda.time.DateTime]
[ERROR]  required: Option[java.time.LocalDateTime]
[ERROR]     verified_at(r),

I like to stick to Java 8 LocalDateTime, how can I resolve OptionDateTimeColumn requires joda time? Or is there a better approach?


Solution

  • Phantom is still JDK7 compatible, and that's been important to us, that's why Java 8 time is not directly supported, but there's separate module for it. In your build.

    libraryDependencies ++= Seq(
      "com.websudos" %% "phantom-jdk8" % phantomVersion
    )
    

    Then you need to:

    import com.websudos.phantom.dsl._
    import com.websudos.phantom.jdk8.dsl._
    
    class UserModel extends CassandraTable[ConcreteUserModel, User]{
    
      // This will not return Option[LocalDate], it will return simply LocalDate
      object verified_at extends JdkLocalDateColumn(this)
    
      override def fromRow(r: Row): User = User(verified_at(r))
    }
    

    Here are all the columns available for Java 8. For optional columns:

    import com.websudos.phantom.dsl._
    import com.websudos.phantom.jdk8.dsl._
    
    class UserModel extends CassandraTable[ConcreteUserModel, User]{
    
      object verified_at extends OptionalPrimitiveColumn[ConcreteUserModel, User, JdkLocalDate](this)
    
      override def fromRow(r: Row): User = User(verified_at(r))
    }