###Connecting to server:
defaultTimeout := time.Second * 10
conn, err := gocouch.Connect("127.0.0.1", 5984, nil, defaultTimeout)
auth := gocouch.BasicAuth{"admin", "pass"}
conn, err := gocouch.Connect("127.0.0.1", 5984, auth, defaultTimeout)
###Database operations:
Get existing database or create new one:
db, err := conn.MustGetDatabase("some_db", nil)
Non-nil Auth
passed to this method has higher priority, so it will owerride previously used on Connect
. By default this method use Auth
object provided when connection was created.
Get all existing databases:
list, err := conn.GetAllDBs()
Fetch new database related action on CouchDB instance:
var events map[string]interface{}
err := conn.GetDBEvent(&events, nil)
This request will block workflow until any event happens or default timeout (60 sec) will be exceeded. If you try to specify custom operation timeout note that it accepts milliseconds. Also it's safe to use in goroutine.
If you want to fetch many events, you can use GetDBEventChan
:
eventChan, err := conn.GetDBEventChan()
defer close(eventChan)
###Basic CRUD actions with a single document:
package main
import (
"github.com/pupizoid/gocouch"
"time"
)
func main() {
server, _ := gocouch.Connect("127.0.0.1", 5984, nil, time.Second * 10)
type Doc struct {
Field string `json:"field"`
Rev string `json:"_rev,omitempty"`
ID string `json:"_id,omitempty"`
}
var document1, document2 Doc
db, _ := server.MustGetDatabase("test_db", nil)
document1.Field = "value"
db.Put("test_id", &document1)
db.Get("test_id", &document2, nil)
db.Del(document2.ID, document2.Rev)
}
###Bulk operations:
##TODO: