如何使用 Scala 更新 CouchDB 中的文档
How to update document in CouchDB with Scala
我想使用出色的 Scala 客户端更改我的 CouchDB 数据库中的文档 CouchDB-Scala。
我知道文档的 ID,并且准备好新数据。例如,假设我的数据是这种情况 class:
case class Person (name: String, age: Int)
我想做一些类似
的事情
val newData = Person("new name", 56)
val docId = "4e5720837e3c057facbaa4e68b01a787"
update(newData, docId)
这对我有用:
import com.ibm.couchdb._
import scala.concurrent.duration._
def update(newData: Person, docId: String) = {
val docs = dbApi.docs
val updateActions = for {
oldDoc <- docs.get[Person](docId)
newDoc = oldDoc.copy(doc = newData)
updateResult <- docs.update(newDoc)
} yield updateResult
val timeoutLength = Duration.create(15, SECONDS)
updateActions.unsafePerformSyncAttemptFor(timeoutLength)
}
这里 dbApi
也执行数据库设置:
import scalaz.concurrent.Task
import scalaz.{-\/, \/, \/-}
val dbApi = {
val couchDbHostName = "couchDbForMyApp"
val couch = CouchDb(couchDbHostName, 5984)
// Define a type mapping used to transform class names into the doc kind
val typeMapping = TypeMapping(classOf[Person] -> "Person")
val dbName = "myApp-database"
// Trying to create a database with the same name as an existing one results in an error
createDatabaseIfItDoesntExistSync(dbName, couch)
// Return an instance of the DB API by name and type mapping
couch.db(dbName, typeMapping)
}
def await[T](future: Task[T]): Throwable \/ T = future.unsafePerformSyncAttempt
def createDatabaseIfItDoesntExistSync(dbName: String, couch: CouchDb) = {
val db = couch.dbs.get(dbName)
await(db) match {
case -\/(e) =>
println(s"Creating database $dbName")
await(couch.dbs.create(dbName))
case \/-(existingDb) =>
println(s"Database with name $dbName already existed")
}
}
我总是很高兴收到有关代码改进的建议!
我想使用出色的 Scala 客户端更改我的 CouchDB 数据库中的文档 CouchDB-Scala。
我知道文档的 ID,并且准备好新数据。例如,假设我的数据是这种情况 class:
case class Person (name: String, age: Int)
我想做一些类似
的事情val newData = Person("new name", 56)
val docId = "4e5720837e3c057facbaa4e68b01a787"
update(newData, docId)
这对我有用:
import com.ibm.couchdb._
import scala.concurrent.duration._
def update(newData: Person, docId: String) = {
val docs = dbApi.docs
val updateActions = for {
oldDoc <- docs.get[Person](docId)
newDoc = oldDoc.copy(doc = newData)
updateResult <- docs.update(newDoc)
} yield updateResult
val timeoutLength = Duration.create(15, SECONDS)
updateActions.unsafePerformSyncAttemptFor(timeoutLength)
}
这里 dbApi
也执行数据库设置:
import scalaz.concurrent.Task
import scalaz.{-\/, \/, \/-}
val dbApi = {
val couchDbHostName = "couchDbForMyApp"
val couch = CouchDb(couchDbHostName, 5984)
// Define a type mapping used to transform class names into the doc kind
val typeMapping = TypeMapping(classOf[Person] -> "Person")
val dbName = "myApp-database"
// Trying to create a database with the same name as an existing one results in an error
createDatabaseIfItDoesntExistSync(dbName, couch)
// Return an instance of the DB API by name and type mapping
couch.db(dbName, typeMapping)
}
def await[T](future: Task[T]): Throwable \/ T = future.unsafePerformSyncAttempt
def createDatabaseIfItDoesntExistSync(dbName: String, couch: CouchDb) = {
val db = couch.dbs.get(dbName)
await(db) match {
case -\/(e) =>
println(s"Creating database $dbName")
await(couch.dbs.create(dbName))
case \/-(existingDb) =>
println(s"Database with name $dbName already existed")
}
}
我总是很高兴收到有关代码改进的建议!