-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
169 lines (134 loc) · 3.45 KB
/
wrapper.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package model
import (
"context"
"github.com/flimzy/kivik"
)
type replicator interface {
Replicate(ctx context.Context, targetDSN, sourceDSN string, options ...kivik.Options) (*kivik.Replication, error)
}
type dsner interface {
DSN() string
}
type kivikClient interface {
CreateDB(ctx context.Context, dbName string, options ...kivik.Options) error
DB(ctx context.Context, dbName string, options ...kivik.Options) (kivikDB, error)
DestroyDB(ctx context.Context, dbName string, options ...kivik.Options) error
dsner
replicator
}
type clientWrapper struct {
*kivik.Client
}
var _ kivikClient = &clientWrapper{}
func (c *clientWrapper) DB(ctx context.Context, dbName string, options ...kivik.Options) (kivikDB, error) {
db, err := c.Client.DB(ctx, dbName, options...)
if err != nil {
return nil, err
}
return wrapDB(db), nil
}
func wrapClient(c *kivik.Client) kivikClient {
return &clientWrapper{Client: c}
}
type queryGetter interface {
querier
getter
}
type querier interface {
Query(ctx context.Context, ddoc, view string, options ...kivik.Options) (kivikRows, error)
}
type putter interface {
Put(context.Context, string, interface{}) (string, error)
}
type getter interface {
Get(ctx context.Context, docID string, options ...kivik.Options) (kivikRow, error)
}
type allDocer interface {
AllDocs(ctx context.Context, options ...kivik.Options) (kivikRows, error)
}
type bulkDocer interface {
BulkDocs(context.Context, interface{}) (kivikBulkResults, error)
}
type kivikBulkResults interface {
Close() error
Next() bool
Err() error
ID() string
UpdateErr() error
}
type getPutter interface {
putter
getter
}
type getPutBulkDocer interface {
getter
putter
bulkDocer
}
type attachmentGetter interface {
GetAttachment(ctx context.Context, docID, rev, filename string) (*kivik.Attachment, error)
}
type finder interface {
Find(ctx context.Context, query interface{}) (kivikRows, error)
}
type deleter interface {
Delete(ctx context.Context, docID, rev string) (newRev string, err error)
}
type statser interface {
Stats(ctx context.Context) (*kivik.DBStats, error)
}
type clientNamer interface {
Client() kivikClient
Name() string
}
type kivikDB interface {
getter
putter
querier
bulkDocer
finder
deleter
statser
clientNamer
attachmentGetter
allDocer
}
type dbWrapper struct {
*kivik.DB
}
var _ kivikDB = &dbWrapper{}
func (db *dbWrapper) Get(ctx context.Context, docID string, options ...kivik.Options) (kivikRow, error) {
return db.DB.Get(ctx, docID, options...)
}
func (db *dbWrapper) Query(ctx context.Context, ddoc, view string, options ...kivik.Options) (kivikRows, error) {
return db.DB.Query(ctx, ddoc, view, options...)
}
func (db *dbWrapper) BulkDocs(ctx context.Context, docs interface{}) (kivikBulkResults, error) {
return db.DB.BulkDocs(ctx, docs)
}
func (db *dbWrapper) Find(ctx context.Context, query interface{}) (kivikRows, error) {
return db.DB.Find(ctx, query)
}
func (db *dbWrapper) AllDocs(ctx context.Context, options ...kivik.Options) (kivikRows, error) {
return db.DB.AllDocs(ctx, options...)
}
func (db *dbWrapper) Client() kivikClient {
return wrapClient(db.DB.Client())
}
func wrapDB(db *kivik.DB) kivikDB {
return &dbWrapper{DB: db}
}
type kivikRow interface {
ScanDoc(dest interface{}) error
}
type kivikRows interface {
Close() error
Next() bool
ScanDoc(dest interface{}) error
ScanValue(dest interface{}) error
ScanKey(dest interface{}) error
TotalRows() int64
ID() string
Err() error
Key() string
}