couchdb

package module
v0.0.0-...-310a5a9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2018 License: MIT Imports: 14 Imported by: 13

README

couchdb-go

Build Status

NOTE: Use the v1.0 Tag for CouchDB 1.0. The current master is being used for CouchDB 2.x work (still a work in progress).

Description

This is my golang CouchDB driver. There are many like it, but this one is mine.

Installation

go get github.com/rhinoman/couchdb-go

Documentation

See the Godoc: http://godoc.org/github.com/rhinoman/couchdb-go

Example Usage

Connect to a server and create a new document:


type TestDocument struct {
	Title string
	Note string
}

...

var timeout = time.Duration(500 * time.Millisecond)
conn, err := couchdb.NewConnection("127.0.0.1",5984,timeout)
auth := couchdb.BasicAuth{Username: "user", Password: "password" }
db := conn.SelectDB("myDatabase", &auth)

theDoc := TestDocument{
	Title: "My Document",
	Note: "This is a note",
}

theId := genUuid() //use whatever method you like to generate a uuid
//The third argument here would be a revision, if you were updating an existing document
rev, err := db.Save(theDoc, theId, "")  
//If all is well, rev should contain the revision of the newly created
//or updated Document

Documentation

Overview

Package couchdb provides a simple REST client for CouchDB

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth interface {
	//Adds authentication headers to a request
	AddAuthHeaders(*http.Request)
	//Extracts Updated auth info from Couch Response
	UpdateAuth(*http.Response)
	//Sets updated auth (headers, cookies, etc.) in an http response
	//For the update function, the map keys are cookie and/or header names
	GetUpdatedAuth() map[string]string
	//Purely for debug purposes.  Do not call, ever.
	DebugString() string
}

Basic interface for Auth

type AuthInfo

type AuthInfo struct {
	Authenticated          string   `json:"authenticated"`
	AuthenticationDb       string   `json:"authentication_db"`
	AuthenticationHandlers []string `json:"authentication_handlers"`
}

type AuthInfoResponse

type AuthInfoResponse struct {
	Info    AuthInfo    `json:"info"`
	Ok      bool        `json:"ok"`
	UserCtx UserContext `json:"userCtx"`
}

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

HTTP Basic Authentication support

func (*BasicAuth) AddAuthHeaders

func (ba *BasicAuth) AddAuthHeaders(req *http.Request)

Adds Basic Authentication headers to an http request

func (*BasicAuth) DebugString

func (ba *BasicAuth) DebugString() string

func (*BasicAuth) GetUpdatedAuth

func (ba *BasicAuth) GetUpdatedAuth() map[string]string

Get Updated Auth Does nothing for BasicAuth

func (*BasicAuth) UpdateAuth

func (ba *BasicAuth) UpdateAuth(resp *http.Response)

do nothing for basic auth

type BulkDocument

type BulkDocument struct {
	// contains filtered or unexported fields
}

BulkDocument Bulk Document API http://docs.couchdb.org/en/1.6.1/api/database/bulk-api.html#db-bulk-docs

func (*BulkDocument) Commit

func (b *BulkDocument) Commit() ([]BulkDocumentResult, error)

Commit POST /{db}/_bulk_docs

func (*BulkDocument) Delete

func (b *BulkDocument) Delete(id, rev string) error

Delete Delete document

func (*BulkDocument) Save

func (b *BulkDocument) Save(doc interface{}, id, rev string) error

Save Save document

type BulkDocumentResult

type BulkDocumentResult struct {
	Ok       bool    `json:"ok"`
	ID       string  `json:"id"`
	Revision string  `json:"rev"`
	Error    *string `json:"error"`
	Reason   *string `json:"reason"`
}

BulkDocumentResult Bulk Document Response

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(address string, port int,
	timeout time.Duration) (*Connection, error)

Creates a regular http connection. Timeout sets the timeout for the http Client

func NewSSLConnection

func NewSSLConnection(address string, port int,
	timeout time.Duration) (*Connection, error)

Creates an https connection. Timeout sets the timeout for the http Client

func (*Connection) AddUser

func (conn *Connection) AddUser(username string, password string,
	roles []string, auth Auth) (string, error)

Add a User. This is a convenience method for adding a simple user to CouchDB. If you need a User with custom fields, etc., you'll just have to use the ordinary document methods on the "_users" database.

func (*Connection) CreateDB

func (conn *Connection) CreateDB(name string, auth Auth) error

Create a new Database.

func (*Connection) CreateSession

func (conn *Connection) CreateSession(username string,
	password string) (*CookieAuth, error)

Creates a session using the Couchdb session api. Returns auth token on success

func (*Connection) DeleteDB

func (conn *Connection) DeleteDB(name string, auth Auth) error

Delete a Database.

func (*Connection) DeleteUser

func (conn *Connection) DeleteUser(username string, rev string, auth Auth) (string, error)

Delete a user.

func (*Connection) DestroySession

func (conn *Connection) DestroySession(auth *CookieAuth) error

Destroys a session (user log out, etc.)

func (*Connection) GetAuthInfo

func (conn *Connection) GetAuthInfo(auth Auth) (*AuthInfoResponse, error)

Returns auth information for a user

func (*Connection) GetConfigOption

func (conn *Connection) GetConfigOption(section string,
	option string, auth Auth) (string, error)

Gets a CouchDB configuration option

func (*Connection) GetDBList

func (conn *Connection) GetDBList() (dbList []string, err error)

DATABASES. Return a list of all databases on the server

func (*Connection) GetUser

func (conn *Connection) GetUser(username string, userData interface{},
	auth Auth) (string, error)

Fetch a user record

func (*Connection) GrantRole

func (conn *Connection) GrantRole(username string, role string,
	auth Auth) (string, error)

Grants a role to a user

func (*Connection) Ping

func (conn *Connection) Ping() error

Use to check if database server is alive.

func (*Connection) RevokeRole

func (conn *Connection) RevokeRole(username string, role string,
	auth Auth) (string, error)

Revoke a user role

func (*Connection) SelectDB

func (conn *Connection) SelectDB(dbName string, auth Auth) *Database

Select a Database.

func (*Connection) SetConfig

func (conn *Connection) SetConfig(section string,
	option string, value string, auth Auth) error

Set a CouchDB configuration option

type CookieAuth

type CookieAuth struct {
	AuthToken        string
	UpdatedAuthToken string
}

Cookie-based auth (for sessions)

func (*CookieAuth) AddAuthHeaders

func (ca *CookieAuth) AddAuthHeaders(req *http.Request)

Adds session token to request

func (*CookieAuth) DebugString

func (ca *CookieAuth) DebugString() string

func (*CookieAuth) GetUpdatedAuth

func (ca *CookieAuth) GetUpdatedAuth() map[string]string

Set AuthSession Cookie

func (*CookieAuth) UpdateAuth

func (ca *CookieAuth) UpdateAuth(resp *http.Response)

Couchdb returns updated AuthSession tokens

type Database

type Database struct {
	// contains filtered or unexported fields
}

func (*Database) AddRole

func (db *Database) AddRole(role string, isAdmin bool) error

Security helper function. Adds a role to a database security doc.

func (*Database) Compact

func (db *Database) Compact() (resp string, e error)

Compact the current database.

func (*Database) Copy

func (db *Database) Copy(fromId string, fromRev string, toId string) (string, error)

Copies a document into a new... document. Returns the revision of the newly created document

func (*Database) DbExists

func (db *Database) DbExists() error

DbExists checks if the database exists

func (*Database) Delete

func (db *Database) Delete(id string, rev string) (string, error)

Deletes a document. Or rather, tells CouchDB to mark the document as deleted. Yes, CouchDB will return a new revision, so this function returns it.

func (*Database) DeleteAttachment

func (db *Database) DeleteAttachment(docId string, docRev string,
	attName string) (string, error)

Deletes an attachment

func (*Database) Find

func (db *Database) Find(results interface{}, params *FindQueryParams) error

func (*Database) GetAttachment

func (db *Database) GetAttachment(docId string, docRev string,
	attType string, attName string) (io.ReadCloser, error)

Gets an attachment. Returns an io.Reader -- the onus is on the caller to close it. Please close it.

func (*Database) GetAttachmentByProxy

func (db *Database) GetAttachmentByProxy(docId string, docRev string,
	attType string, attName string, r *http.Request, w http.ResponseWriter) error

Fetches an attachment and proxies the result

func (*Database) GetList

func (db *Database) GetList(designDoc string, list string,
	view string, results interface{}, params *url.Values) error

Get the result of a list operation This assumes your list function in couchdb returns JSON

func (*Database) GetMultipleFromView

func (db *Database) GetMultipleFromView(designDoc string, view string,
	results interface{}, keys []string) error

Get multiple results of a view.

func (*Database) GetSecurity

func (db *Database) GetSecurity() (*Security, error)

Returns the Security document from the database.

func (*Database) GetView

func (db *Database) GetView(designDoc string, view string,
	results interface{}, params *url.Values) error

Get the results of a view.

func (*Database) NewBulkDocument

func (db *Database) NewBulkDocument() *BulkDocument

NewBulkDocument New BulkDocument instance

func (*Database) Read

func (db *Database) Read(id string, doc interface{}, params *url.Values) (string, error)

Fetches a document from the database. Pass it a &struct to hold the contents of the fetched document (doc). Returns the current revision and/or error

func (*Database) ReadMultiple

func (db *Database) ReadMultiple(ids []string, results interface{}) error

Fetches multiple documents in a single request given a set of arbitrary _ids

func (*Database) RemoveRole

func (db *Database) RemoveRole(role string) error

Security helper function. Removes a role from a database security doc.

func (*Database) Save

func (db *Database) Save(doc interface{}, id string, rev string) (string, error)

Save a document to the database. If you're creating a new document, pass an empty string for rev. If updating, you must specify the current rev. Returns the revision number assigned to the doc by CouchDB.

func (*Database) SaveAttachment

func (db *Database) SaveAttachment(docId string,
	docRev string, attName string,
	attType string, attContent io.Reader) (string, error)

Saves an attachment. docId and docRev refer to the parent document. attType is the MIME type of the attachment (ex: image/jpeg) or some such. attContent is a byte array containing the actual content.

func (*Database) SaveDesignDoc

func (db *Database) SaveDesignDoc(name string,
	designDoc interface{}, rev string) (string, error)

Save a design document. If creating a new design doc, set rev to "".

func (*Database) SaveSecurity

func (db *Database) SaveSecurity(sec Security) error

Save a security document to the database.

type Error

type Error struct {
	StatusCode int
	URL        string
	Method     string
	ErrorCode  string //empty for HEAD requests
	Reason     string //empty for HEAD requests
}

func (*Error) Error

func (err *Error) Error() string

stringify the error

type FindQueryParams

type FindQueryParams struct {
	Selector interface{} `json:"selector"`
	Limit    int         `json:"limit,omitempty"`
	Skip     int         `json:"skip,omitempty"`
	Sort     interface{} `json:"sort,omitempty"`
	Fields   []string    `json:"fields,omitempty"`
	UseIndex interface{} `json:"user_index,omitempty"`
}

type Members

type Members struct {
	Users []string `json:"names,omitempty"`
	Roles []string `json:"roles,omitempty"`
}

type PassThroughAuth

type PassThroughAuth struct {
	AuthHeader string
}

Pass-through Auth header

func (*PassThroughAuth) AddAuthHeaders

func (pta *PassThroughAuth) AddAuthHeaders(req *http.Request)

Use if you already have an Authentication header you want to pass through to couchdb

func (*PassThroughAuth) DebugString

func (pta *PassThroughAuth) DebugString() string

func (*PassThroughAuth) GetUpdatedAuth

func (pta *PassThroughAuth) GetUpdatedAuth() map[string]string

Does nothing for PassThroughAuth

func (*PassThroughAuth) UpdateAuth

func (pta *PassThroughAuth) UpdateAuth(resp *http.Response)

do nothing for pass through

type ProxyAuth

type ProxyAuth struct {
	Username  string
	Roles     []string
	AuthToken string
}

Proxy authentication

func (*ProxyAuth) AddAuthHeaders

func (pa *ProxyAuth) AddAuthHeaders(req *http.Request)

func (*ProxyAuth) DebugString

func (pa *ProxyAuth) DebugString() string

func (*ProxyAuth) GetUpdatedAuth

func (pa *ProxyAuth) GetUpdatedAuth() map[string]string

do nothing for Proxy Auth

func (*ProxyAuth) UpdateAuth

func (pa *ProxyAuth) UpdateAuth(resp *http.Response)

do nothing for proxy auth

type Security

type Security struct {
	Members Members `json:"members"`
	Admins  Members `json:"admins"`
}

type UserContext

type UserContext struct {
	Name  string   `json:"name"`
	Roles []string `json:"roles"`
}

type UserRecord

type UserRecord struct {
	Name     string   `json:"name"`
	Password string   `json:"password,omitempty"`
	Roles    []string `json:"roles"`
	TheType  string   `json:"type"` //apparently type is a keyword in Go :)

}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL