Bruckwald Bruckwald - 1 month ago 35
Scala Question

Phantom DSL and Cassandra custom column type

I've defined a custom Cassandra type and a table,e.g:

CREATE TYPE my.usertype (
id text,
firstname text,
lastname text
);


CREATE TABLE mytable (
user frozen <usertype>,
...,
PRIMARY KEY(user)
);


How can I define this user type in the Cassandra table definition in Scala?

class MyTable extends CassandraTable[X, Y] {
object user extends UserColumn(this) with PartitionKey[User]
^^^^^??? ^^^???


How can I implement a custom
UserColumn
for the
UserType
? I checked the Phantom code for the column implementations, but any example and/or explanation would be great.

Answer

In phantom pro only, which will be available to subscribe to in 2 weeks time:

@Udt case class User(
  id: String,
  firstname: String,
  lastname: String
)

And then you use UDTColumn:

class MyTable extends CassandraTable[MyTable , Y] {
    object user extends UDTColumn[MyTable, Y, User](this) with PartitionKey[User]
}

This will give you automated schema generation and whatever else, including automated initialisation of your UDT.

Comments