Skip to content

Model Contexts

A ModelContext is the store for a Data Model. In its simplest abstract form, a ModelContext has a generic type argument that represents the Class that is stored. The base classes for ModelContext live in the vasat-core module.

scala
class ArticleReport extends ModelContext[Article]

The vasat-slick module provides implementations that map a ModelContext to a relational database using the slick library.

The Basics

A ModelContext has 3 basic functions

  • read an object with its ID
  • write an object with its ID
  • delete and object with its ID
scala
val articleRepo = new ArticleRepository // sample context

val a:Article = ???
articleRepo.writeObject("abc-123",a).andThenMap(_ => 
    println("Saved")
)

articleRepo.readObject("abc-123").andThenMap(o => 
    println("Read " + o)
)

articleRepo.deleteObject("abc-123").andThenMap(o => 
    println("Deleted")
)

By default a ModelContext will not have the ability to search, a trait SearchableContext[A] is used for that

scala
class ArticleReport extends ModelContext[Article] with SearchableContext[A]

The Model Repository

ModelContext's are registered in a ModelRepository which is a collection of ModelContexts and an optional 'scope'.

scala
val articleRepo = new ArticleRepository // sample context
val accountRepo = new AccountRepository // sample context

val repo = new SimpleModelRepository(
    Set(articleRepo),   // for all
    Map("website" -> Set(accountRepo)) // limit to 'website' scope
)

A model context's scope is a string identifier that is used by the vasat-auth module to map ModelContexts to OAuth clients.

Implicit ModelRepository

The ModelRepository when declared as an implicit, powers the convenience functions .save() and .load() on a Ref[_] object.

scala
implicit repo:ModelRepository = ???

val myRef = Ref[Article]("123")

// Load will search the repo for the type 'Article' and
// use its .readObject method
myRef.load().andThenMap(a => 
    println("Article object for id=123 " + a.item)
)