outworkers / phantom

Schema safe, type-safe, reactive Scala driver for Cassandra/Datastax Enterprise
http://outworkers.github.io/phantom/
Apache License 2.0
1.05k stars 186 forks source link

Support table inheritance natively in the DSL #770

Open alexflav23 opened 6 years ago

alexflav23 commented 6 years ago

As requested by multiple users, we should natively support table inheritance as many applications seem to require it as a simplification of the DSL code, instead of trying to discourage it.

Draft proposal.

alexflav23 commented 6 years ago

@hsn10 I know you had thoughts here, please feel free to add to the mix.

hsn10 commented 6 years ago

I need ability to override column type including column used for primary key

somename6668 commented 4 years ago

The code below is how I implemented table inheritance to change PartitionKey and ClusteringOrder setting on different table.

import com.outworkers.phantom.dsl._

case class User(userId: String,
                gender: Int,
                birthDate: DateTime)
trait UserTable[Owner <: Table[Owner, User] with UserTable[Owner]] extends Table[Owner, User] {
  def name: StringColumn

  def gender: IntColumn

  def birthDate: DateTimeColumn

  // def query(): Future[ListResult[User]]
}

abstract class UserByGender extends UserTable[UserByGender] {

  object name extends StringColumn

  object birthDate extends DateTimeColumn

  object gender extends IntColumn with PartitionKey

}

abstract class UserByBirthDate extends UserTable[UserByBirthDate] {

  object name extends StringColumn

  object gender extends IntColumn

  object birthDate extends DateTimeColumn with ClusteringOrder with Descending

}