How to use SQLSyntaxSupport with a case class containing other case classes

113 Views Asked by At

I'm trying to use Scalikejbdc's ORM with SQLSyntaxSupport

The table layout is as follows:

create table samples (
    id text
    event_name text
    event_data text
    created_at timestamp
    scope text
)

The classes in Scala are as follows

case class SampleData(id: String, eventName: String, eventData: String)
case class SampleMetadata(createdAt: OffsetDateTime, scope: String)
case class Sample(data: SampleData, metadata: SampleMetadata)

I would like to use SQLSyntaxSupport like so

object SampleSchema extends SQLSyntaxSupport[Sample] {
  override val tableName = "sources"

  override lazy val columns = Seq(
    "id",
    "event_name",
    "event_data",
    "created_at",
    "scope"
  )

  val insertColumns = Seq(
    column.id,
    column.eventName,
    column.eventData,
    column.createdAt,
    column.scope
  )
}

and later be able to do:

  def insert(source: Source): Unit = DB localTx { implicit session =>
      applyUpdate {
        insertInto(SampleSchema)
          .columns(SampleSchema.insertColumns: _*)
          .values(SampleSchema.toInsertValues(session, source): _*)
      }
  }

Code doesn't compile; with errors like

 Sample#eventName not found. Expected fields are #data, #metadata
 column.eventName 

Is there a way to make it work without flattening the case class Sample ?

0

There are 0 best solutions below