Can not access field with sorm

50 Views Asked by At

In Scala, I have the following scala case class:

case class Page(url: String)

object Page {
  implicit val personFormat = Json.format[Page]
}

Which is encoded in the database like this:

object Db extends Instance(entities = Seq(Entity[Page]()), url="jdbc:h2:mem:test")

Afterwards, I retrieve one instance from the database like this:

val page = Db.query[Page].whereEqual("id", pageId).fetch
val content: String = new URL(page.url).getContent().toString

However, on the last line I am getting.

value url is not a member of Stream[models.Page with sorm.Persisted]

Why is url not a member?

I created a database representation for Page. Shouldn't that include all its fields?

1

There are 1 best solutions below

0
Kordi On

It should be like this

package models

import sorm._
import play.api.libs.json.{JsValue, Writes, Json}

case class Page(url: String)

object Page {
  implicit val writes = Json.writes[Page]
  implicit val reads = Json.reads[Page]
}

object DB extends Instance(Set(Entity[Page]()), "jdbc:h2:mem:test")


def pages = Action {
   val pages = DB.query[Page].fetch()
   Ok(Json.toJson(pages))
}

def addPage = Action(parse.json) { request =>
   val page = DB.save(request.body.as[Page])
   Ok(Json.toJson(page))
}