score:2

Accepted answer

you could try the following:

trait modelops[t] {
  def table: appdb.table // not sure about type
  def order: appdb.orderbypredicate // not sure about type
  def tojson(model: t): jsvalue

  def add(model: t): option[t] = option(appdb.categories.insert(model))
  def remove(id: long) = appdb.categories.delete(id)
  def getall(): list[t] = from(table)(model => select(model) orderby(order)) tolist
  def alltojson() = {
    val json:list[jsvalue] = getall.map{tojson(_)}
    json.tojson(json.toseq)
  }

}

then you can for each model type:

object modeltype extends modelops[modeltype] {
  def table = appdb.examples
  def order = yourpredicate
  def tojson(model:modeltype):jsvalue = {
    json.tojson(map("field" -> json.tojson(model.field)))
  }
  def validate(different values for each model) = // is fairly different for each one. validates the submitted fields from a user
}

update about the true type of appdb.orderbypredicate:

calling select on primitivetypemode returns a selectstate. on this selectstate, you will call orderby which takes a list[basequeryyield#o] (or multiple of those in the same argument list). hence you should define:

def order(model: t): list[basequeryyield#o]

and

def getall() = from(table)(model => select(model) orderby(order(model))) tolist

by the way, basequeryyield#o resolves to expressionnode.


Related Query

More Query from same tag