ropensci / sofa

Easy R interface to CouchDB
https://docs.ropensci.org/sofa/
33 stars 17 forks source link

Upsert #68

Closed critichu closed 5 years ago

critichu commented 5 years ago

Added a new function called doc_upsert() that updates an existing document or creates it if it doesn't yet exist.

Description

doc_upsert() first retrieves the latest revision number for a given document id (docid) -- using db_revisions()
It then updates that document to the given doc -- using doc_update()

If the docid does not exist, then the first step will fail.
This is handled in doc_upsert(), which reverts to creating the document -- using doc_create()

Example

(x <- Cushion$new())

if ("sofadb" %in% db_list(x)) {
  invisible(db_delete(x, dbname="sofadb"))
}
db_create(x, 'sofadb')

# create a document
doc1 <- '{"name": "drink", "beer": "IPA", "score": 5}'
doc_upsert(x, dbname="sofadb", doc1, docid="abeer")

#update the document
doc2 <- '{"name": "drink", "beer": "lager", "score": 6}'
doc_upsert(x, dbname="sofadb", doc2, docid="abeer")

doc_get(x, dbname = "sofadb", docid = "abeer")

Tests

doc_upsert() is using existing sofa functions.
Nevertheless I included unit tests in the tests/testthat folder.

critichu commented 5 years ago

closing this to fix problems with the tests