ropensci / sofa

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

Writing compatibility with jsonlite #80

Closed ColinFay closed 2 years ago

ColinFay commented 2 years ago
Session Info ```r ```

Hey,

Maybe I'm missing something, but is there a way to make doc_create compatible with jsonlite::toJSON()?

The example show

doc2 <- '{"name": "samson", "color": "red", "furry": false, "size": 3}'
doc_create(x, dbname = "cats", doc2)

But it feels weird to build a JSON object like this.

Though, I have errors when I try to upload JSON object created with {jsonlite} :

docker run --rm --name couchdbank -e COUCHDB_USER=bebop -e COUCHDB_PASSWORD=aloula -p 5984:5984 couchdb
library(sofa)

x <- Cushion$new(
  host = "localhost",
  transport = "http",
  port = 5984,
  user = "bebop",
  pwd = "aloula"
)

> x$ping()$couchdb
[1] "Welcome"

> db_create(x, "bankrcache")
$ok
[1] TRUE

> doc_create(x, "bankrcache", jsonlite::toJSON(iris), docid = "pouet")
Error in UseMethod("doc_create_") : 
  no applicable method for 'doc_create_' applied to an object of class "json"

> doc_create(x, "bankrcache", jsonlite::toJSON(iris) |> as.character(), docid = "pouet")
$error
[1] "bad_request"

$reason
[1] "Document must be a JSON object"

Thanks!

maelle commented 2 years ago

This repository is about to be archived.