package dispatch.couch
import json._
import Js._
trait Id extends Js {
val _id = Symbol("_id") ? str
val _rev = Symbol("_rev") ? str
}
object Id extends Id
case class Couch(hostname: String, port: Int) extends Request(:/(hostname, port))
object Couch {
def apply(): Couch = this("127.0.0.1")
def apply(hostname: String): Couch = Couch(hostname, 5984)
}
case class Db(couch: Couch, name: String) extends Request(couch / name) with Js {
val all_docs = this / "_all_docs" ># ('rows ! list andThen { _ map 'id ! str })
val create = this <<< Nil >|
val delete = DELETE >|
}
import java.net.URLEncoder.encode
case class Doc(val db: Db, val id: String) extends Request(db / encode(id)) with Js {
def update(js: JsValue) = this <<< js ># {
case Updated.rev(rev) => (Id._rev << rev)(js)
}
private object Updated { val rev = 'rev ? str }
def delete(rev: String) = DELETE <<? Map("rev" -> rev) >|
}