How to create keyspace and insert data using Phantom dsl

127 Views Asked by At

I'm using this library for the first time, and I ran into a problem. I did everything according to the documentation, but nothing is working, and i don't know why. Here is my table model:

trait CassandraModel

object CassandraModel {

  case class TaskData(notifyid: String,
                      notifyType: String)
      extends CassandraModel

  abstract class TaskDataCassandra extends Table[TaskDataCassandra, TaskData] {
    object notifyid       extends StringColumn with PartitionKey
    object notifyType     extends StringColumn


    def store(record: TaskData): InsertQuery.Default[TaskDataCassandra, TaskData] =
      insert
        .value(_.notifyId, record.notifyId)
        .value(_.notifyType, record.notifyType)
  }
}

And DataBase with DatabaseProvider:

class AppDatabase(override val connector: CassandraConnection) extends Database[AppDatabase](connector) {
  object taskDataCassandra extends TaskDataCassandra with Connector
}

trait AppDatabaseProvider extends DatabaseProvider[AppDatabase]

So, when i starting my app, i'm trying to create a keyspace, but nothing is happens

object Boot extends App with AmqpConnector with ServiceRestRoute with JsonSerializer with AppDatabaseProvider {

  override def database: AppDatabase = new AppDatabase(CassandraConnector.createCassandraConnection)

  database.taskDataCassandra.create.ifNotExists()

}

store method also doesn't works

1

There are 1 best solutions below

0
flavian On

Read through the documentation properly and the differences will be obvious. The thing to read is the Database Docs.

You have 2 options. You can call database.create(), which is a blocking creation operation that will create all the tables inside the database.

Option 2 is to call database.taskDataCassandra.create.ifNotExists().future().

If you do not use future(), all you have is a query generated, you are not actually executing anything. If you check the return type of database.taskDataCassandra.create.ifNotExists() it will be a CreateQuery, wheres if you add future() you get a Future[Result].

Hope this makes sense.