LuGo
LuGo

Reputation: 5045

Convenient way of inserting values to Cassandra with Phantom

Does anyone know of a convenient way of inserting values to Cassandra via phatom-dsl? Currently I'm doing this:

case class Entry(id: UUID, firstName: String, lastName: String)

sealed class EntryTable extends CassandraTable[EntryTable, Entry] {
  override val tableName = "entries"
  object id extends UUIDColumn(this) with Index[UUID]
  object firstName extends StringColumn(this)
  object lastName extends StringColumn(this)

  override def fromRow(r: dsl.Row): Entry = {
    Entry(id(r), firstName(r), firstName(r))
  }
}

object EntryTable extends EntryTable {
   private val connector = CassandraConnector.apply(Set(InetAddress.getByName("localhost")))
   implicit val keySpace = KeySpace("keyspace")

   def insert(e: Entry) = {
     connector.withSessionDo(implicit session => {
       insert().value(_.id, e.id)).value(_.firstName, e.firstName).value(_.lastName, e.lastName).future()
     }
   }
}

But I would like to do:

def insert(e: Entry) = {
  connector.withSessionDo(implicit session => {
    insert().value(e).future()
  }
}

Which would be way more convenient when the case class has many fields. Any ideas?

Upvotes: 1

Views: 664

Answers (1)

flavian
flavian

Reputation: 28511

You are using the API slightly wrong and we are in the process of publishing multiple tutorials to make the "new" way public. In the mean time, a basic version of it is available here and this branch in the activator template is also describing everything you need to know.

Specifically, the way to insert records is described here.

Upvotes: 1

Related Questions