Search code examples
cassandrainsertphantom-dsl

Scala Phantom Cassandra insert method returns empty ResultSet


I want to insert data to my table in Cassandra and then return value from column "user_id" instead of full ResultSet. Here it is snippet of my code:

   def create(user: User): Future[UUID] = {
     insert
       .value(_.id, user.id)
       .value(_.email, user.email)
       .value(_.name, user.name)
       .consistencyLevel_=(ConsistencyLevel.ALL)
       .future()
       .map(r => fromRow(r.one()).id)
    }

   def fromRow(r: Row): User = {
     User(id(r), email(r), name(r))
   }

So future() returns Future[ResultSet]. After that I try to retrieve Row from ResultSet, modify it to User and get id eventually. Despite the fact that data were saved to my table I got

ResultSet[ exhausted: true, Columns[]]

columns of the ResultSet are empty and consequently r.one() returned null. I haven't found any examples for my purpose. So, can phantom-dsl do something like Quill?

val q = quote {
  query[Product].insert(lift(Product(0L, "My Product", 1011L))).returning(_.id)
}

Solution

  • So in more recent versions of phantom that create method is automatically generated. More details here. The fromRow method is also automatically generated so you don't need to type it manually.

    Long story short, this is what you could use:

    def create(user: User): Future[UUID] = {
      store(user)
        .consistencyLevel_=(ConsistencyLevel.ALL)
        .future()
        .map(_ => user.id)
    }