Documentation
¶
Index ¶
- Constants
- Variables
- func CreateDB(db Database, doctype string) error
- func CreateDoc(db Database, doc Doc) error
- func CreateNamedDoc(db Database, doc Doc) error
- func CreateNamedDocWithDB(db Database, doc Doc) error
- func DefineIndex(db Database, doctype string, index mango.Index) error
- func DefineViews(db Database, doctype string, views Views) error
- func Delete(db Database, doctype, id, rev string) (string, error)
- func DeleteAllDBs(db Database) error
- func DeleteDB(db Database, doctype string) error
- func DeleteDoc(db Database, doc Doc) error
- func ExecView(db Database, doctype, view string, results interface{}) error
- func FindDocs(db Database, doctype string, req *FindRequest, results interface{}) error
- func FindDocsRaw(db Database, doctype string, req interface{}, results interface{}) error
- func GetAllDocs(db Database, doctype string, req *AllDocsRequest, results interface{}) error
- func GetDoc(db Database, doctype, id string, out Doc) error
- func IsConflictError(err error) bool
- func IsNoDatabaseError(err error) bool
- func IsNotFoundError(err error) bool
- func Proxy(db Database, doctype, path string) *httputil.ReverseProxy
- func ResetDB(db Database, doctype string) error
- func UpdateDoc(db Database, doc Doc) error
- type AllDocsRequest
- type AllDocsResponse
- type Change
- type ChangesFeedMode
- type ChangesFeedStyle
- type ChangesRequest
- type ChangesResponse
- type DBStatusResponse
- type Database
- type Doc
- type Error
- type FindRequest
- type IndexCreationResponse
- type JSONDoc
- func (j JSONDoc) DocType() string
- func (j JSONDoc) Get(key string) interface{}
- func (j JSONDoc) ID() string
- func (j JSONDoc) MarshalJSON() ([]byte, error)
- func (j JSONDoc) Rev() string
- func (j JSONDoc) SetID(id string)
- func (j JSONDoc) SetRev(rev string)
- func (j *JSONDoc) ToMapWithType() map[string]interface{}
- func (j *JSONDoc) UnmarshalJSON(bytes []byte) error
- func (j JSONDoc) Valid(field, value string) bool
- type View
- type ViewResponse
- type Views
Constants ¶
const ( // ChangesModeNormal is the only mode supported by cozy-stack ChangesModeNormal ChangesFeedMode = "normal" // ChangesStyleAllDocs pass all revisions including conflicts ChangesStyleAllDocs ChangesFeedStyle = "all_docs" // ChangesStyleMainOnly only pass the winning revision ChangesStyleMainOnly ChangesFeedStyle = "main_only" )
Variables ¶
var GlobalDB = SimpleDatabasePrefix("global")
GlobalDB is the prefix used for stack-scoped db
Functions ¶
func CreateDoc ¶
CreateDoc is used to persist the given document in the couchdb database. The document's SetRev and SetID function will be called with the document's new ID and Rev. This function creates a database if this is the first document of its type
func CreateNamedDoc ¶
CreateNamedDoc persist a document with an ID. if the document already exist, it will return a 409 error. The document ID should be fillled. The doc SetRev function will be called with the new rev.
func CreateNamedDocWithDB ¶
CreateNamedDocWithDB is equivalent to CreateNamedDoc but creates the database if it does not exist
func DefineIndex ¶
DefineIndex define the index on the doctype database see query package on how to define an index
func DefineViews ¶
DefineViews creates a design doc with some views
func Delete ¶
Delete destroy a document by its doctype and ID . If the document's current rev does not match the one passed, a CouchdbError(409 conflict) will be returned. This functions returns the tombstone revision as string
func DeleteAllDBs ¶
DeleteAllDBs will remove all the couchdb doctype databases for a couchdb.DB.
func DeleteDoc ¶
DeleteDoc deletes a struct implementing the couchb.Doc interface The document's SetRev will be called with tombstone revision
func FindDocs ¶
func FindDocs(db Database, doctype string, req *FindRequest, results interface{}) error
FindDocs returns all documents matching the passed FindRequest documents will be unmarshalled in the provided results slice.
func FindDocsRaw ¶
FindDocsRaw find documents TODO: pagination
func GetAllDocs ¶
func GetAllDocs(db Database, doctype string, req *AllDocsRequest, results interface{}) error
GetAllDocs returns all documents of a specified doctype. It filters out the possible _design document. TODO: pagination
func GetDoc ¶
GetDoc fetch a document by its docType and ID, out is filled with the document by json.Unmarshal-ing
func IsConflictError ¶
IsConflictError checks if the given error is a couch conflict error
func IsNoDatabaseError ¶
IsNoDatabaseError checks if the given error is a couch no_db_file error
func IsNotFoundError ¶
IsNotFoundError checks if the given error is a couch not_found error
func Proxy ¶
func Proxy(db Database, doctype, path string) *httputil.ReverseProxy
Proxy generate a httputil.ReverseProxy which forwards the request to the correct route.
Types ¶
type AllDocsRequest ¶
type AllDocsRequest struct {
Descending bool `url:"descending,omitempty"`
Keys []string `url:"keys,omitempty"`
Limit int `url:"limit,omitempty"`
Skip int `url:"skip,omitempty"`
StartKey string `url:"start_key,omitempty"`
EndKey string `url:"end_key,omitempty"`
}
AllDocsRequest is used to build a _all_docs request
type AllDocsResponse ¶
type AllDocsResponse struct {
Offset int `json:"offset"`
TotalRows int `json:"total_rows"`
Rows []struct {
ID string `json:"id"`
Doc json.RawMessage `json:"doc"`
} `json:"rows"`
}
AllDocsResponse is the response we receive from an _all_docs request
type Change ¶
type Change struct {
DocID string `json:"id"`
Seq string `json:"seq"`
Doc JSONDoc `json:"doc"`
Changes []struct {
Rev string `json:"rev"`
} `json:"changes"`
}
A Change is an atomic change in couchdb
type ChangesFeedMode ¶
type ChangesFeedMode string
ChangesFeedMode is a value for the feed parameter of a ChangesRequest
func ValidChangesMode ¶
func ValidChangesMode(feed string) (ChangesFeedMode, error)
ValidChangesMode convert any string into a ChangesFeedMode or gives an error if the string is invalid.
type ChangesFeedStyle ¶
type ChangesFeedStyle string
ChangesFeedStyle is a value for the style parameter of a ChangesRequest
func ValidChangesStyle ¶
func ValidChangesStyle(style string) (ChangesFeedStyle, error)
ValidChangesStyle convert any string into a ChangesFeedStyle or gives an error if the string is invalid.
type ChangesRequest ¶
type ChangesRequest struct {
DocType string `url:"-"`
// see Changes Feeds. Default is normal.
Feed ChangesFeedMode `url:"feed,omitempty"`
// Maximum period in milliseconds to wait for a change before the response
// is sent, even if there are no results. Only applicable for longpoll or
// continuous feeds. Default value is specified by httpd/changes_timeout
// configuration option. Note that 60000 value is also the default maximum
// timeout to prevent undetected dead connections.
Timeout int `url:"timeout,omitempty"`
// Period in milliseconds after which an empty line is sent in the results.
// Only applicable for longpoll, continuous, and eventsource feeds. Overrides
// any timeout to keep the feed alive indefinitely. Default is 60000. May be
// true to use default value.
Heartbeat int `url:"heartbeat,omitempty"`
// Includes conflicts information in response. Ignored if include_docs isn’t
// true. Default is false.
Conflicts bool `url:"conflicts,omitempty"`
// Return the change results in descending sequence order (most recent change
// first). Default is false.
Descending bool `url:"descending,omitempty"`
// Include the associated document with each result. If there are conflicts,
// only the winning revision is returned. Default is false.
IncludeDocs bool `url:"include_docs,omitempty"`
// Include the Base64-encoded content of attachments in the documents that
// are included if include_docs is true. Ignored if include_docs isn’t true.
// Default is false.
Attachments bool `url:"attachments,omitempty"`
// Include encoding information in attachment stubs if include_docs is true
// and the particular attachment is compressed. Ignored if include_docs isn’t
// true. Default is false.
AttEncodingInfo bool `url:"att_encoding_info,omitempty"`
// Alias of Last-Event-ID header.
LastEventID int `url:"last,omitempty"`
// Limit number of result rows to the specified value (note that using 0 here
// has the same effect as 1).
Limit int `url:"limit,omitempty"`
// Start the results from the change immediately after the given update
// sequence. Can be valid update sequence or now value. Default is 0.
Since string `url:"since,omitempty"`
// Specifies how many revisions are returned in the changes array. The
// default, main_only, will only return the current “winning” revision;
// all_docs will return all leaf revisions (including conflicts and deleted
// former conflicts).
Style ChangesFeedStyle `url:"style,omitempty"`
// Reference to a filter function from a design document that will filter
// whole stream emitting only filtered events. See the section Change
// Notifications in the book CouchDB The Definitive Guide for more
// information.
Filter string `url:"filter,omitempty"`
// Allows to use view functions as filters. Documents counted as “passed” for
// view filter in case if map function emits at least one record for them.
// See _view for more info.
View string `url:"view,omitempty"`
}
A ChangesRequest are all parameters than can be passed to a changes feed
type ChangesResponse ¶
type ChangesResponse struct {
LastSeq string `json:"last_seq"` // Last change update sequence
Pending int `json:"pending"` // Count of remaining items in the feed
Results []Change `json:"results"` // Changes made to a database
}
A ChangesResponse is the response provided by a GetChanges call
func GetChanges ¶
func GetChanges(db Database, req *ChangesRequest) (*ChangesResponse, error)
GetChanges returns a list of change in couchdb
type DBStatusResponse ¶
type DBStatusResponse struct {
DBName string `json:"db_name"`
UpdateSeq string `json:"update_seq"`
Sizes struct {
File int `json:"file"`
External int `json:"external"`
Active int `json:"active"`
} `json:"sizes"`
PurgeSeq int `json:"purge_seq"`
Other struct {
DataSize int `json:"data_size"`
} `json:"other"`
DocDelCount int `json:"doc_del_count"`
DocCount int `json:"doc_count"`
DiskSize int `json:"disk_size"`
DiskFormatVersion int `json:"disk_format_version"`
DataSize int `json:"data_size"`
CompactRunning bool `json:"compact_running"`
InstanceStartTime string `json:"instance_start_time"`
}
DBStatusResponse is the response from DBStatus
type Database ¶
type Database interface {
Prefix() string
}
Database is the type passed to every function in couchdb package for now it is just a string with the database prefix.
func SimpleDatabasePrefix ¶
SimpleDatabasePrefix returns a Database from a prefix, useful for test
type Doc ¶
type Doc interface {
ID() string
Rev() string
DocType() string
SetID(id string)
SetRev(rev string)
}
Doc is the interface that encapsulate a couchdb document, of any serializable type. This interface defines method to set and get the ID of the document.
type Error ¶
type Error struct {
StatusCode int
CouchdbJSON []byte `json:"-"`
Name string `json:"error"`
Reason string `json:"reason"`
Original error
}
Error represent an error from couchdb
type FindRequest ¶
type FindRequest struct {
Selector mango.Filter `json:"selector"`
UseIndex string `json:"use_index,omitempty"`
Limit int `json:"limit,omitempty"`
Skip int `json:"skip,omitempty"`
Sort *mango.SortBy `json:"sort,omitempty"`
Fields []string `json:"fields,omitempty"`
}
FindRequest is used to build a find request
type IndexCreationResponse ¶
type IndexCreationResponse struct {
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Reason string `json:"reason,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
IndexCreationResponse is the response from couchdb when we create an Index
func DefineIndexRaw ¶
func DefineIndexRaw(db Database, doctype string, index interface{}) (*IndexCreationResponse, error)
DefineIndexRaw defines a index
type JSONDoc ¶
JSONDoc is a map representing a simple json object that implements the Doc interface.
func (JSONDoc) DocType ¶
DocType returns the document type of the document
"io.cozy.event" == doc.Doctype()
func (JSONDoc) ID ¶
ID returns the identifier field of the document
"io.cozy.event/123abc123" == doc.ID()
func (JSONDoc) MarshalJSON ¶
MarshalJSON implements json.Marshaller by proxying to internal map
func (*JSONDoc) ToMapWithType ¶
ToMapWithType returns the JSONDoc internal map including its DocType its used in request response.
func (*JSONDoc) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaller by proxying to internal map
type ViewResponse ¶
type ViewResponse struct {
Rows []struct {
ID string `json:"id"`
Key string `json:"key"`
Value int64 `json:"value"`
} `json:"rows"`
}
ViewResponse is the response we receive when executing a view