attachments: Work with attachments

Description Usage Arguments Details Value Examples

Description

Work with attachments

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
doc_attach_create(
  cushion,
  dbname,
  docid,
  attachment,
  attname,
  as = "list",
  ...
)

doc_attach_info(cushion, dbname, docid, attname, ...)

doc_attach_get(cushion, dbname, docid, attname = NULL, type = "raw", ...)

doc_attach_delete(cushion, dbname, docid, attname, as = "list", ...)

Arguments

cushion

A Cushion object. Required.

dbname

(character) Database name. Required.

docid

(character) Document ID. Required.

attachment

(character) A file name. Required.

attname

(character) Attachment name. Required.

as

(character) One of list (default) or json

...

Curl args passed on to HttpClient

type

(character) one of raw (default) or text. required.

Details

Methods:

Value

JSON as a character string or a list (determined by the as parameter)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## 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")

## End(Not run)

sofa documentation built on July 8, 2020, 6:53 p.m.