scalacassandraphantom-dsl

Scala + Cassandra + Phantom. Modeling Multiple Tables for Same Entity


In my Cassandra keyspace i have a main offer table and three more copies of same table, oriented to different query arguments, as follow:

offer (primary key offer_id) (... some attributes)
offer_by_product (primary key product_id, offer_id) (... some attributes)
offer_by_seller (primary key seller_id, offer_id) (... some attributes)
offer_by_sku (primary key sku_id, offer_id) (... some attributes)

All columns are exact the same, changing only the partition key and clustering key, but i need to duplicate a lot of code to implement CRUD operations for all four tables using Scala+Phantom.

There are any way to implement my repository, using Scala+Phantom, without code duplication for CRUD operations or some good pratice with less duplication?


Solution

  • Well, as @flavian already said, you cannot do that, nonetheless I could at least extract the objects into a trait like this.

    trait MyCommonModel[O <: CassandraTable[O, R], R] {
    
      var cassandra: O = _
    
      object commonField extends StringColumn(cassandra)
      ...
    
    }
    

    Then you can use like this in your model:

    sealed class MyModel extends CassandraTable[MyModel, Model] with MyCommonModel[MyModel, Model]
    

    Thus you will inherit all objects from the CommonModel.

    UPDATE

    I've created a github project to show how to model cassandra tables in scala using phantom-dsl following the documentation. Check out here.

    https://github.com/iamthiago/cassandra-phantom