Skip to content

Instantly share code, notes, and snippets.

@JorritPosthuma
Created April 20, 2012 13:20
Show Gist options
  • Save JorritPosthuma/2428506 to your computer and use it in GitHub Desktop.
Save JorritPosthuma/2428506 to your computer and use it in GitHub Desktop.
import com.googlecode.objectify.annotation
import scala.annotation.target.field
object Annotations {
type AlsoLoad = annotation.AlsoLoad @field
type Embed = annotation.Embed @field
type Id = annotation.Id @field
type Ignore = annotation.Ignore @field
type IgnoreLoad = annotation.IgnoreLoad @field
type IgnoreSave = annotation.IgnoreSave @field
type Index = annotation.Index @field
type Load = annotation.Load @field
type Mapify = annotation.Mapify @field
type Parent = annotation.Parent @field
type Serialize = annotation.Serialize @field
type Translate = annotation.Translate @field
type Unindex = annotation.Unindex @field
}
// Create the company
val company = Company("Apple", Address("Infinite Loop", "1", "Cupertino", "United States"))
// Save it synchronous (by adding .now)
val company_key = Ofy.save.entity(company).now
// Create the User (with reference to the company)
val user = new User("steve.jobs@apple.com", "iPassword", "Steve Jobs", Ref.create(company_key))
// Save it synchronous
Ofy.save.entity(user).now
// Retrieve based on the email-adress
val get = Ofy.load.`type`(classOf[User]).id(user.email).get
val get = Ofy.load.kind(classOf[User]).id(user.email).get
LoaderWrapper
ObjectifyWrapper
QueryResultIteratorWrapper
QueryWrapper
SimpleQueryWrapper
TransactionWrapper
ResultWrapper
SimpleFutureWrapper
object Ofy extends Ofy {
ObjectifyService.register(classOf[User])
ObjectifyService.register(classOf[Company])
protected val _conn = new DynamicVariable[Objectify](null)
def ofy: Objectify = {
if (_conn.value == null) {
_conn.value = ObjectifyService.factory.begin()
_conn.value.setWrapper(this)
}
_conn.value
}
}
trait Ofy extends Objectify {
def ofy: Objectify
def load: ScalaLoader = new ScalaLoader(ofy.load)
def save: Saver = ofy.save
def delete: Deleter = ofy.delete
def getTxn: Transaction = ofy.getTxn
def getFactory: ObjectifyFactory = ofy.getFactory
def consistency(policy: Consistency): Ofy = new OfyWrapper(ofy.consistency(policy))
def deadline(value: java.lang.Double): Ofy = new OfyWrapper(ofy.deadline(value))
def cache(value: Boolean): Ofy = new OfyWrapper(ofy.cache(value))
def transaction(): Ofy = new OfyWrapper(ofy.transaction())
def transactionless(): Ofy = new OfyWrapper(ofy.transactionless())
def transact[O <: Objectify, R](work: TxnWork[O, R]): R = ofy.transact(work)
def transact[O <: Objectify, R](limitTries: Int, work: TxnWork[O, R]): R = ofy.transact(limitTries, work)
def clear = ofy.clear
def setWrapper(ofy: Objectify) = ofy.setWrapper(ofy)
def toEntity(pojo: Object): Entity = ofy.toEntity(pojo)
def toPojo[T](entity: Entity) : T = ofy.toPojo(entity)
}
class OfyWrapper(val base: Objectify) extends Ofy {
base.setWrapper(this)
override def ofy: Objectify = base
}
class ScalaLoader(val loader: Loader) extends LoaderWrapper[ScalaLoader](loader) {
def kind[E](kind: Class[E]): LoadType[E] = loader.`type`(kind)
}
@Entity
case class User(
@Id var email: String,
@Index var password: String,
var name: String,
var company: Ref[Company]
) {
// Only for Objectify creation
private def this() { this(null, null, null, null) }
}
@JorritPosthuma
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment