# NOT RUN {
user <- Sys.getenv("COUCHDB_TEST_USER")
pwd <- Sys.getenv("COUCHDB_TEST_PWD")
(x <- Cushion$new(user=user, pwd=pwd))
if ("foodb" %in% db_list(x)) {
invisible(db_delete(x, dbname="foodb"))
}
db_create(x, dbname='foodb')
# create an attachment on an existing document
## create a document first
doc <- '{"name":"stuff", "drink":"soda"}'
doc_create(x, dbname="foodb", doc=doc, docid="asoda")
## create a csv attachment
row.names(mtcars) <- NULL
file <- tempfile(fileext = ".csv")
write.csv(mtcars, file = file, row.names = FALSE)
doc_attach_create(x, dbname="foodb", docid="asoda",
attachment=file, attname="mtcarstable.csv")
## create a binary (png) attachment
file <- tempfile(fileext = ".png")
png(file)
plot(1:10)
dev.off()
doc_attach_create(x, dbname="foodb", docid="asoda",
attachment=file, attname="img.png")
## create a binary (pdf) attachment
file <- tempfile(fileext = ".pdf")
pdf(file)
plot(1:10)
dev.off()
doc_attach_create(x, dbname="foodb", docid="asoda",
attachment=file, attname="plot.pdf")
# get info for an attachment (HEAD request)
doc_attach_info(x, "foodb", docid="asoda", attname="mtcarstable.csv")
doc_attach_info(x, "foodb", docid="asoda", attname="img.png")
doc_attach_info(x, "foodb", docid="asoda", attname="plot.pdf")
# get an attachment (GET request)
res <- doc_attach_get(x, "foodb", docid="asoda",
attname="mtcarstable.csv", type = "text")
read.csv(text = res)
doc_attach_get(x, "foodb", docid="asoda", attname="img.png")
doc_attach_get(x, "foodb", docid="asoda", attname="plot.pdf")
## OR, don't specify an attachment and list the attachments
(attchms <- doc_attach_get(x, "foodb", docid="asoda", type="text"))
jsonlite::fromJSON(attchms)
# delete an attachment
doc_attach_delete(x, "foodb", docid="asoda", attname="mtcarstable.csv")
doc_attach_delete(x, "foodb", docid="asoda", attname="img.png")
doc_attach_delete(x, "foodb", docid="asoda", attname="plot.pdf")
# }
Run the code above in your browser using DataLab