github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/couchdb/local.go (about)

     1  package couchdb
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	"github.com/cozy/cozy-stack/pkg/prefixer"
     8  )
     9  
    10  // GetLocal fetch a local document from CouchDB
    11  // http://docs.couchdb.org/en/stable/api/local.html#get--db-_local-docid
    12  func GetLocal(db prefixer.Prefixer, doctype, id string) (map[string]interface{}, error) {
    13  	var out map[string]interface{}
    14  	u := "_local/" + url.PathEscape(id)
    15  	if err := makeRequest(db, doctype, http.MethodGet, u, nil, &out); err != nil {
    16  		return nil, err
    17  	}
    18  	return out, nil
    19  }
    20  
    21  // PutLocal will put a local document in CouchDB.
    22  // Note that you should put the last revision in `doc` to avoid conflicts.
    23  func PutLocal(db prefixer.Prefixer, doctype, id string, doc map[string]interface{}) error {
    24  	u := "_local/" + url.PathEscape(id)
    25  	var out UpdateResponse
    26  	if err := makeRequest(db, doctype, http.MethodPut, u, doc, &out); err != nil {
    27  		return err
    28  	}
    29  	doc["_rev"] = out.Rev
    30  	return nil
    31  }
    32  
    33  // DeleteLocal will delete a local document in CouchDB.
    34  func DeleteLocal(db prefixer.Prefixer, doctype, id string) error {
    35  	u := "_local/" + url.PathEscape(id)
    36  	return makeRequest(db, doctype, http.MethodDelete, u, nil, nil)
    37  }