-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouchdb.go
More file actions
131 lines (104 loc) · 3.11 KB
/
couchdb.go
File metadata and controls
131 lines (104 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package couchdb provides a Go client library for Apache CouchDB using Resty
package couchdb
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
// NewClient creates a new CouchDB client
func NewClient(baseURL string, opts *ClientOptions) *Client {
if opts == nil {
opts = &ClientOptions{}
}
if opts.Timeout == 0 {
opts.Timeout = 30 * time.Second
}
client := resty.New()
client.SetBaseURL(strings.TrimSuffix(baseURL, "/"))
client.SetTimeout(opts.Timeout)
client.SetHeader("Content-Type", "application/json")
client.SetDebug(opts.Debug)
if opts.Username != "" && opts.Password != "" {
client.SetBasicAuth(opts.Username, opts.Password)
}
return &Client{
resty: client,
baseURL: strings.TrimSuffix(baseURL, "/"),
}
}
type ServerInfo struct {
CouchDB string `json:"couchdb"`
Version string `json:"version"`
UUID string `json:"uuid"`
}
func (e *Error) Error() string {
return fmt.Sprintf("CouchDB error %d: %s - %s", e.StatusCode, e.Type, e.Reason)
}
// CompactDesignDoc compacts a specific design document's view indexes
func (db *Database) CompactDesignDoc(ctx context.Context, designDoc string) error {
resp, err := db.client.resty.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
Post("/" + db.name + "/_compact/" + designDoc)
if err != nil {
return err
}
if resp.IsError() {
return db.client.parseError(resp)
}
return nil
}
// Helper methods for common view patterns
// ViewByKey is a convenience method to query a view by a single key
func (db *Database) ViewByKey(ctx context.Context, designDoc, viewName string, key interface{}) (*ViewResult, error) {
return db.View(ctx, designDoc, viewName, &ViewOptions{
Key: key,
})
}
// ViewByKeyRange is a convenience method to query a view by key range
func (db *Database) ViewByKeyRange(ctx context.Context, designDoc, viewName string, startKey, endKey interface{}) (*ViewResult, error) {
return db.View(ctx, designDoc, viewName, &ViewOptions{
StartKey: startKey,
EndKey: endKey,
})
}
// ViewAll is a convenience method to get all results from a view
func (db *Database) ViewAll(ctx context.Context, designDoc, viewName string, includeDocs bool) (*ViewResult, error) {
return db.View(ctx, designDoc, viewName, &ViewOptions{
IncludeDocs: includeDocs,
})
}
// Changes returns database changes
func (db *Database) Changes(ctx context.Context, opts map[string]interface{}) (map[string]interface{}, error) {
req := db.client.resty.R().SetContext(ctx)
for k, v := range opts {
req.SetQueryParam(k, fmt.Sprintf("%v", v))
}
var result map[string]interface{}
resp, err := req.
SetResult(&result).
Get("/" + db.name + "/_changes")
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, db.client.parseError(resp)
}
return result, nil
}
// Compact triggers database compaction
func (db *Database) Compact(ctx context.Context) error {
resp, err := db.client.resty.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
Post("/" + db.name + "/_compact")
if err != nil {
return err
}
if resp.IsError() {
return db.client.parseError(resp)
}
return nil
}