Documentation ¶
Overview ¶
Package couchdb provides a very powerful as well as convenient client for the CouchDB database.
A connection to the database or at least a server can be established by calling
cdb, err := db.Open(cfg)
The supported operations are the listing, creation, and deleting of databases, the listing of all design documents and data documents, the creation, reading, updating, and deleting of documents, searches, and views.
Index ¶
- Constants
- type ChangeProcessor
- type Changes
- type Database
- func (db *Database) AllDocumentIDs(params ...Parameter) ([]string, error)
- func (db *Database) BulkWriteDocuments(docs []interface{}, params ...Parameter) (Statuses, error)
- func (db *Database) Changes(params ...Parameter) (*Changes, error)
- func (db *Database) CreateDocument(doc interface{}, params ...Parameter) *ResultSet
- func (db *Database) DeleteDocument(doc interface{}, params ...Parameter) *ResultSet
- func (db *Database) DeleteDocumentByID(id, revision string, params ...Parameter) *ResultSet
- func (db *Database) Designs() *Designs
- func (db *Database) Find(search *Search, params ...Parameter) (*Find, error)
- func (db *Database) HasDocument(id string, params ...Parameter) (bool, error)
- func (db *Database) Manager() *Manager
- func (db *Database) Name() string
- func (db *Database) ReadDocument(id string, params ...Parameter) *ResultSet
- func (db *Database) Request() *Request
- func (db *Database) StartSession(name, password string) (*Session, error)
- func (db *Database) UpdateDocument(doc interface{}, params ...Parameter) *ResultSet
- func (db *Database) View(designID, viewID string, params ...Parameter) (*View, error)
- type DatabaseVersion
- type Design
- func (d *Design) Delete(params ...Parameter) *ResultSet
- func (d *Design) ID() string
- func (d *Design) Language() string
- func (d *Design) SetLanguage(language string)
- func (d *Design) SetShow(id, showf string)
- func (d *Design) SetView(id, mapf, reducef string)
- func (d *Design) Show(id string) (string, bool)
- func (d *Design) View(id string) (string, string, bool)
- func (d *Design) Write(params ...Parameter) *ResultSet
- type Designs
- type Find
- type FindProcessor
- type Index
- type KeyValue
- type Manager
- func (m *Manager) AllDatabaseIDs() ([]string, error)
- func (m *Manager) CreateDatabase(params ...Parameter) *ResultSet
- func (m *Manager) CreateIndex(index *Index, params ...Parameter) *ResultSet
- func (m *Manager) CreateUser(user *User, params ...Parameter) error
- func (m *Manager) DatabaseVersion() (version.Version, error)
- func (m *Manager) DeleteAdministrator(nodename, name string, params ...Parameter) error
- func (m *Manager) DeleteDatabase(params ...Parameter) *ResultSet
- func (m *Manager) DeleteNamedDatabase(name string, params ...Parameter) *ResultSet
- func (m *Manager) DeleteUser(name string, params ...Parameter) error
- func (m *Manager) HasAdministrator(nodename, name string, params ...Parameter) (bool, error)
- func (m *Manager) HasDatabase() (bool, error)
- func (m *Manager) Init(steps ...Step) error
- func (m *Manager) ReadSecurity(params ...Parameter) (*Security, error)
- func (m *Manager) ReadUser(name string, params ...Parameter) (*User, error)
- func (m *Manager) UpdateUser(user *User, params ...Parameter) error
- func (m *Manager) Version() (version.Version, error)
- func (m *Manager) WriteAdministrator(nodename, name, password string, params ...Parameter) error
- func (m *Manager) WriteSecurity(security Security, params ...Parameter) error
- type NamesRoles
- type Option
- type Options
- type Parameter
- func BasicAuthentication(name, password string) Parameter
- func Descending() Parameter
- func EndKey(end interface{}) Parameter
- func FilterDocumentIDs(documentIDs ...string) Parameter
- func FilterSelector(selector json.RawMessage) Parameter
- func FilterView(view string) Parameter
- func Group(level int) Parameter
- func Header(kvs ...KeyValue) Parameter
- func IncludeDocuments() Parameter
- func Keys(keys ...interface{}) Parameter
- func Limit(limit int) Parameter
- func NoReduce() Parameter
- func OneKey(key interface{}) Parameter
- func Query(kvs ...KeyValue) Parameter
- func Revision(revision string) Parameter
- func Since(sequence string) Parameter
- func Skip(skip int) Parameter
- func SkipLimit(skip, limit int) Parameter
- func StartEndKey(start, end interface{}) Parameter
- func StartKey(start interface{}) Parameter
- func StringKeys(keys ...string) Parameter
- func Style(style string) Parameter
- type Request
- func (req *Request) AddQuery(key, value string)
- func (req *Request) ApplyParameters(params ...Parameter) *Request
- func (req *Request) Delete() *ResultSet
- func (req *Request) Get() *ResultSet
- func (req *Request) GetOrPost() *ResultSet
- func (req *Request) Head() *ResultSet
- func (req *Request) Post() *ResultSet
- func (req *Request) Put() *ResultSet
- func (req *Request) SetDocument(doc interface{}) *Request
- func (req *Request) SetHeader(key, value string)
- func (req *Request) SetPath(parts ...string) *Request
- func (req *Request) SetQuery(key, value string)
- func (req *Request) UpdateDocument(update func(interface{}) interface{})
- type ResultSet
- func (rs *ResultSet) Document(value interface{}) error
- func (rs *ResultSet) Error() error
- func (rs *ResultSet) Header(key string) string
- func (rs *ResultSet) ID() string
- func (rs *ResultSet) IsDeleted() bool
- func (rs *ResultSet) IsOK() bool
- func (rs *ResultSet) Raw() ([]byte, error)
- func (rs *ResultSet) Revision() string
- func (rs *ResultSet) StatusCode() int
- type Search
- func (s *Search) Bookmark(bookmark string) *Search
- func (s *Search) Fields(fields ...string) *Search
- func (s *Search) Limit(limit int) *Search
- func (s *Search) MarshalJSON() ([]byte, error)
- func (s *Search) ReadQuorum(quorum int) *Search
- func (s *Search) Skip(skip int) *Search
- func (s *Search) Sort(fieldsOrDirs ...string) *Search
- func (s *Search) Stable(stable bool) *Search
- func (s *Search) Update(update bool) *Search
- func (s *Search) UseIndex(designDocument, name string) *Search
- type Security
- type Session
- type Status
- type Statuses
- type Step
- type StepAction
- type Steps
- type Unmarshable
- type User
- type View
- type ViewProcessor
Constants ¶
const ( ErrStartupActionFailed = "ESTARTUP" ErrInvalidVersion = "EINVVSN" ErrInvalidDocument = "EINVDOC" ErrNoIdentifier = "ENOID" ErrNotFound = "ENOTFOUND" ErrEncoding = "EENCODING" ErrDecoding = "EDECODING" ErrPreparingRequest = "EPREPARE" ErrPerformingRequest = "EPERFORM" ErrClientRequest = "EREQ" ErrReadingResponseBody = "ERESP" ErrUserNotFound = "ENOUSR" ErrUserExists = "EUSREXIST" )
Error codes.
const ( SinceNow = "now" StyleMainOnly = "main_only" StyleAllDocs = "all_docs" )
Fixed values for some of the view parameters.
const ( StatusOK = http.StatusOK StatusCreated = http.StatusCreated StatusAccepted = http.StatusAccepted StatusFound = http.StatusFound StatusBadRequest = http.StatusBadRequest StatusForbidden = http.StatusForbidden StatusNotFound = http.StatusNotFound StatusMethodNotAllowed = http.StatusMethodNotAllowed StatusNotAcceptable = http.StatusNotAcceptable StatusConflict = http.StatusConflict StatusPreconditionFailed = http.StatusPreconditionFailed StatusTooManyRequests = http.StatusTooManyRequests StatusInternalServerError = http.StatusInternalServerError )
Status codes after database requests.
const DatabaseVersionID = "database-version"
DatabaseVersionID is used for the database version document.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangeProcessor ¶
type ChangeProcessor func(id, sequence string, deleted bool, revisions []string, document *Unmarshable) error
ChangeProcessor is a function processing the content of a changed document.
type Changes ¶
type Changes struct {
// contains filtered or unexported fields
}
Changes provides access to the responded changes.
func (*Changes) LastSequence ¶
LastSequence returns the sequence ID of the last change.
func (*Changes) Pending ¶
Pending returns the number of pending changes if the query has been limited.
func (*Changes) Process ¶
func (c *Changes) Process(process ChangeProcessor) error
Process iterates over the found changes and processes them.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database provides the access to a database.
func Open ¶
Open returns a configured connection to a CouchDB server. Permanent parameters, e.g. for authentication, are possible.
func (*Database) AllDocumentIDs ¶
AllDocumentIDs returns a list of all document IDs of the configured database.
func (*Database) BulkWriteDocuments ¶
BulkWriteDocuments allows to create or update many documents en bloc.
func (*Database) CreateDocument ¶
CreateDocument creates a new document.
func (*Database) DeleteDocument ¶
DeleteDocument deletes a existing document.
func (*Database) DeleteDocumentByID ¶
DeleteDocumentByID deletes an existing document simply by its identifier and revision.
func (*Database) HasDocument ¶
HasDocument checks if the document with the ID exists.
func (*Database) ReadDocument ¶
ReadDocument reads the a document by ID.
func (*Database) Request ¶
Request returns a raw database request for this database. Can be used for not covered low-level commands.
func (*Database) StartSession ¶
StartSession starts a cookie based session for the given user.
func (*Database) UpdateDocument ¶
UpdateDocument update a document if exists.
type DatabaseVersion ¶
type DatabaseVersion struct { ID string `json:"_id"` Revision string `json:"_rev,omitempty"` Version string `json:"version"` }
DatabaseVersion stores the current database version with the document ID "database-version".
type Design ¶
type Design struct {
// contains filtered or unexported fields
}
Design provides convenient access to one design document.
func (*Design) SetLanguage ¶
SetLanguage sets the language for views and shows.
type Designs ¶
type Designs struct {
// contains filtered or unexported fields
}
Designs manages access to all design documents.
type Find ¶
type Find struct {
// contains filtered or unexported fields
}
Find allows to find and process documents by a given selector.
func (*Find) Process ¶
func (f *Find) Process(process FindProcessor) error
Process iterates over the found documents and processes them.
type FindProcessor ¶
type FindProcessor func(document *Unmarshable) error
FindProcessor is a function processing the content of a found document.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index allows to generate an index for faster find operations.
func (*Index) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager bundles the methods to manage the database system opposite to handle documents.
func (*Manager) AllDatabaseIDs ¶
AllDatabaseIDs returns a list of all database IDs of the connected server.
func (*Manager) CreateDatabase ¶
CreateDatabase creates the configured database.
func (*Manager) CreateIndex ¶
CreateIndex creates a new index for finds.
func (*Manager) CreateUser ¶
CreateUser adds a new user to the system.
func (*Manager) DatabaseVersion ¶
DatabaseVersion returns the version number of the database.
func (*Manager) DeleteAdministrator ¶
DeleteAdministrator deletes an administrator from the given database.
func (*Manager) DeleteDatabase ¶
DeleteDatabase removes the configured database.
func (*Manager) DeleteNamedDatabase ¶
DeleteNamedDatabase removes the given database.
func (*Manager) DeleteUser ¶
DeleteUser deletes a user from the system.
func (*Manager) HasAdministrator ¶
HasAdministrator checks if a given administrator account exists.
func (*Manager) HasDatabase ¶
HasDatabase checks if the configured database exists.
func (*Manager) Init ¶
Init checks and creates the database if needed and performs the individual steps.
func (*Manager) ReadSecurity ¶
ReadSecurity returns the security for the given database.
func (*Manager) UpdateUser ¶
UpdateUser updates a user in the system.
func (*Manager) WriteAdministrator ¶
WriteAdministrator adds or updates an administrator to the given database.
type NamesRoles ¶
type NamesRoles struct { Names []string `json:"names,omitempty"` Roles []string `json:"roles,omitempty"` }
NamesRoles contains names and roles for administrators and users.
type Option ¶
Option defines a function setting an option.
type Options ¶
Options is returned when calling Options() on Database to provide information about the database configuration.
type Parameter ¶
type Parameter func(req *Request)
Parameter is a function changing one (or if needed multile) parameter.
func BasicAuthentication ¶
BasicAuthentication is intended for basic authentication against the database.
func Descending ¶
func Descending() Parameter
Descending sets the flag for a descending order of changes gathering.
func FilterDocumentIDs ¶
FilterDocumentIDs sets a filtering of the changes to the given document identifiers.
func FilterSelector ¶
func FilterSelector(selector json.RawMessage) Parameter
FilterSelector sets the filter to the passed selector expression.
func FilterView ¶
FilterView sets the name of a view which map function acts as filter in case it emits at least one record.
func IncludeDocuments ¶
func IncludeDocuments() Parameter
IncludeDocuments sets the flag for the including of found view documents.
func Keys ¶
func Keys(keys ...interface{}) Parameter
Keys sets a number of keys wanted for a view request.
func NoReduce ¶
func NoReduce() Parameter
NoReduce sets the flag for usage of a reduce function to false.
func OneKey ¶
func OneKey(key interface{}) Parameter
OneKey reduces a view result to only one emitted key.
func StartEndKey ¶
func StartEndKey(start, end interface{}) Parameter
StartEndKey sets the startkey and endkey for a view request.
func StartKey ¶
func StartKey(start interface{}) Parameter
StartKey sets the startkey for a view request.
func StringKeys ¶
StringKeys sets a number of keys of type string wanted for a view request.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is responsible for an individual request to a CouchDB. It has to be created by the dabase and can be used queued.
cdb.Request().SetPath(...).SetDocument(...).Put()
func (*Request) ApplyParameters ¶
ApplyParameters applies a list of parameters to the request.
func (*Request) GetOrPost ¶
GetOrPost decides based on the document if it will perform a GET request or a POST request. The document can be set directly or by one of the parameters. Several of the CouchDB commands work this way.
func (*Request) SetDocument ¶
SetDocument sets the document of the request.
func (*Request) UpdateDocument ¶
func (req *Request) UpdateDocument(update func(interface{}) interface{})
UpdateDocument allows to modify or exchange the request document.
type ResultSet ¶
type ResultSet struct {
// contains filtered or unexported fields
}
ResultSet contains the server result set.
func (*ResultSet) Document ¶
Document returns the received document of a client request and unmorshals it.
func (*ResultSet) StatusCode ¶
StatusCode returns the status code of the request.
type Search ¶
type Search struct {
// contains filtered or unexported fields
}
Search allows to formulate what documents shall be selected and the additional parameters.
func (*Search) Bookmark ¶
Bookmark enables to specify which page of results is required. Every search returns an opaque string under the bookmark key that can be passed this way. Only works for indexes of type "text".
func (*Search) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Search) ReadQuorum ¶
ReadQuorum sets the needed read quorum for the result. The default is 1, so that the document found un the index is returned. Higher quorums forces to read from more replicas. This case needs more time.
func (*Search) Sort ¶
Sort sets the sorting of the result by alternates of field names and directions like "asc" or "desc". For examle ("name", "asc", "age", "desc").
type Security ¶
type Security struct { Admins NamesRoles `json:"admins,omitempty"` Members NamesRoles `json:"members,omitempty"` }
Security contains administrators and members for one database.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session contains the information of a CouchDB session.
type Status ¶
type Status struct { OK bool `json:"ok"` ID string `json:"id"` Revision string `json:"rev"` Error string `json:"error"` Reason string `json:"reason"` }
Status contains internal status information CouchDB returns.
type Statuses ¶
type Statuses []Status
Statuses is the list of status information after a bulk writing.
type Step ¶
type Step func() (version.Version, StepAction)
Step returns the version after a startup step and the action that shall be performed on the database. The returned action will only be performed, if the current if the new version is than the current version.
type StepAction ¶
StepAction is the concrete action of a step.
type Unmarshable ¶
type Unmarshable struct {
// contains filtered or unexported fields
}
Unmarshable describes a not yet unmarshalled value that can be unmarshalled into a given variable. It is used to access key, value, or document of view result rows.
func NewUnmarshableJSON ¶
func NewUnmarshableJSON(msg json.RawMessage) *Unmarshable
NewUnmarshableJSON creates a new Unmarshable out of a json.RawMessage
func NewUnmarshableRaw ¶
func NewUnmarshableRaw(raw []byte) *Unmarshable
NewUnmarshableRaw creates a new Unmarshable out of the raw bytes.
func (*Unmarshable) Raw ¶
func (u *Unmarshable) Raw() []byte
Raw returns the unmarshable as raw byte slice.
func (*Unmarshable) String ¶
func (u *Unmarshable) String() string
String returns the unmarshable as string.
func (*Unmarshable) Unmarshal ¶
func (u *Unmarshable) Unmarshal(doc interface{}) error
Unmarshal unmarshals the interface into the passed variable.
type User ¶
type User struct { DocumentID string `json:"_id,omitempty"` DocumentRevision string `json:"_rev,omitempty"` Name string `json:"name"` Password string `json:"password"` Type string `json:"type,omitempty"` Roles []string `json:"roles,omitempty"` }
User contains name and password for user management and authentication.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View provides access to the responded views.
func (*View) Process ¶
func (v *View) Process(process ViewProcessor) error
Process iterates over the found view documents and processes them.
func (*View) ReturnedRows ¶
ReturnedRows returns the nnumber of returned view rows.
type ViewProcessor ¶
type ViewProcessor func(id string, key, value, document *Unmarshable) error
ViewProcessor is a function processing the content of a view row.