This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pouchdb.go
385 lines (345 loc) · 12.2 KB
/
pouchdb.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package pouchdb
import (
"bytes"
"encoding/json"
"errors"
"io"
"reflect"
"strings"
"github.com/flimzy/jsblob"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jsbuiltin"
)
type PouchDB struct {
o *js.Object
}
type Result map[string]interface{}
type DBInfo struct {
DBName string `json:"db_name"`
DocCount uint `json:"doc_count"`
UpdateSeq uint64 `json:"update_seq"`
}
// GlobalPouch is the global pouchdb object. The package will look for it in
// the global object (js.Global), or try to require it if it is not found. If
// this does not work for you, you ought to set it explicitly yourself:
//
// pouchdb.GlobalPouch = js.Global.Call("require", "/path/to/your/copy/of/pouchdb")
var GlobalPouch *js.Object
func globalPouch() *js.Object {
if GlobalPouch != nil && GlobalPouch != js.Undefined {
return GlobalPouch
}
GlobalPouch = js.Global.Get("PouchDB")
if GlobalPouch == js.Undefined {
panic("go-pouchdb: Cannot find global PouchDB object. Did you load the PouchDB library?")
}
return GlobalPouch
}
// Plugin registers a loaded plugin with the global PouchDB object
func Plugin(plugin *js.Object) {
globalPouch().Call("plugin", plugin)
}
// Debug enables debugging for the specified module. Note this only affects
// connections made after this is run.
// See: http://pouchdb.com/api.html#debug_mode
func Debug(module string) {
globalPouch().Get("debug").Call("enable", module)
}
// DebugDisable disables debugging.
func DebugDisable() {
globalPouch().Get("debug").Call("disable")
}
// New creates a database or opens an existing one.
// See: http://pouchdb.com/api.html#create_database
func New(db_name string) *PouchDB {
return &PouchDB{globalPouch().New(db_name)}
}
// NewWithOpts creates a database or opens an existing one.
// See: http://pouchdb.com/api.html#create_database
func NewWithOpts(db_name string, opts Options) *PouchDB {
return &PouchDB{globalPouch().New(db_name, opts.compile())}
}
// Info fetches information about a database.
//
// See: http://pouchdb.com/api.html#database_information
func (db *PouchDB) Info() (DBInfo, error) {
rw := NewResultWaiter()
db.Call("info", rw.Done)
result, err := rw.ReadResult()
if err != nil {
return DBInfo{}, err
}
var dbinfo DBInfo
err = ConvertJSONObject(result, &dbinfo)
return dbinfo, err
}
// Deestroy will delete the database.
// See: http://pouchdb.com/api.html#delete_database
func (db *PouchDB) Destroy(opts Options) error {
rw := NewResultWaiter()
db.Call("destroy", opts.compile(), rw.Done)
return rw.Error()
}
// ConvertJSONObject takes an intterface{} and runs it through json.Marshal()
// and json.Unmarshal() so that any struct tags will be applied.
func ConvertJSONObject(input, output interface{}) error {
encoded, err := json.Marshal(input)
if err != nil {
return err
}
return json.Unmarshal(encoded, output)
}
// convertJSObject converts the provided *js.Object to an interface{} then
// calls convertJSONObject. This is necessary for objects, because json.Marshal
// ignores any unexported fields in objects, and this includes practically
// everything inside a js.Object.
func ConvertJSObject(jsObj *js.Object, output interface{}) error {
return ConvertJSONObject(jsObj.Interface(), output)
}
// Put will create a new document or update an existing document.
// See: http://pouchdb.com/api.html#create_document
func (db *PouchDB) Put(doc interface{}) (newrev string, err error) {
var convertedDoc interface{}
ConvertJSONObject(doc, &convertedDoc)
rw := NewResultWaiter()
db.Call("put", convertedDoc, rw.Done)
return rw.ReadRev()
}
// Get retrieves a document, specified by docId.
// The document is unmarshalled into the given object.
// Some fields (like _conflicts) will only be returned if the
// options require it. Please refer to the CouchDB HTTP API documentation
// for more information.
//
// See http://pouchdb.com/api.html#fetch_document
// and http://docs.couchdb.org/en/latest/api/document/common.html?highlight=doc#get--db-docid
func (db *PouchDB) Get(docId string, doc interface{}, opts Options) error {
rw := NewResultWaiter()
db.Call("get", docId, opts.compile(), rw.Done)
obj, err := rw.ReadResult()
if err != nil {
return err
}
return ConvertJSONObject(obj, doc)
}
// Attachment represents document attachments.
// This structure is borrowed from http://godoc.org/github.com/fjl/go-couchdb#Attachment
type Attachment struct {
Name string // File name
Type string // MIME type of the Body
MD5 []byte // MD5 checksum of the Body
Body io.Reader // The body itself
}
// PutAttachment creates or updates an attachment. To create an attachment
// on a non-existing document, pass an empty rev.
//
// See http://pouchdb.com/api.html#save_attachment and
// http://godoc.org/github.com/fjl/go-couchdb#DB.PutAttachment
func (db *PouchDB) PutAttachment(docid string, att *Attachment, rev string) (newrev string, err error) {
rw := NewResultWaiter()
db.Call("putAttachment", docid, att.Name, rev, attachmentObject(att), att.Type, rw.Done)
return rw.ReadRev()
}
// attachmentObject converts an io.Reader to a JavaScript Buffer in node, or
// a Blob in the browser
func attachmentObject(att *Attachment) *js.Object {
buf := new(bytes.Buffer)
buf.ReadFrom(att.Body)
if buffer := js.Global.Get("Buffer"); jsbuiltin.TypeOf(buffer) == "function" {
// The Buffer type is supported, so we'll use that
return buffer.New(buf.String())
}
// We must be in the browser, so return a Blob instead
return js.Global.Get("Blob").New([]interface{}{buf.Bytes()}, map[string]string{"type": att.Type})
}
func attachmentFromPouch(name string, obj *js.Object) *Attachment {
att := &Attachment{
Name: name,
}
var body string
if jsbuiltin.TypeOf(obj.Get("write")) == "function" {
// This looks like a Buffer object; we're in node
body = obj.Call("toString", "utf-8").String()
att.Body = strings.NewReader(body) // FIXME: bytes, not string
} else {
// We're in the browser
att.Type = obj.Get("type").String()
blob := jsblob.Blob{*obj}
att.Body = bytes.NewReader(blob.Bytes())
}
return att
}
// Attachment retrieves an attachment. The rev argument can be left empty to
// retrieve the latest revision. The caller is responsible for closing the
// attachment's Body if the returned error is nil.
//
// Note that PouchDB's getDocument() does not fetch meta data (except for the
// MIME type in the browser only), so the MD5 sum and (in node) the content
// type fields will be empty.
//
// See http://pouchdb.com/api.html#get_attachment and
// http://godoc.org/github.com/fjl/go-couchdb#Attachment
func (db *PouchDB) Attachment(docid, name, rev string) (*Attachment, error) {
opts := Options{
Rev: rev,
}
rw := NewResultWaiter()
db.Call("getAttachment", docid, name, opts.compile(), rw.Done)
obj, err := rw.Read()
if err != nil {
return nil, err
}
x := attachmentFromPouch(name, obj)
return x, nil
}
func (db *PouchDB) DeleteAttachment(docid, name, rev string) (newrev string, err error) {
rw := NewResultWaiter()
db.Call("removeAttachment", docid, name, rev, rw.Done)
return rw.ReadRev()
}
// Remove will delete the document. The document must specify both _id and
// _rev. On success, it returns the _rev of the new document with _delete set
// to true.
//
// See: http://pouchdb.com/api.html#delete_document
func (db *PouchDB) Remove(doc interface{}, opts Options) (newrev string, err error) {
var convertedDoc interface{}
ConvertJSONObject(doc, &convertedDoc)
rw := NewResultWaiter()
db.Call("remove", convertedDoc, opts.compile(), rw.Done)
return rw.ReadRev()
}
// BulkDocs will create, update or delete multiple documents.
//
// See: http://pouchdb.com/api.html#batch_create
func (db *PouchDB) BulkDocs(docs interface{}, opts Options) ([]Result, error) {
s := reflect.ValueOf(docs)
if s.Kind() != reflect.Slice {
return nil, errors.New("docs must be a slice")
}
convertedDocs := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i {
ConvertJSONObject(s.Index(i).Interface(), &(convertedDocs[i]))
}
rw := NewResultWaiter()
db.Call("bulkDocs", convertedDocs, opts.compile(), rw.Done)
return rw.ReadBulkResults()
}
// AllDocs will fetch multiple documents.
// The output of the query is unmarshalled into the given result. The format
// of the result depends on the options. Please refer to the CouchDB HTTP API
// documentation for all the possible options that can be set.
//
// See http://pouchdb.com/api.html#batch_fetch and
// http://docs.couchdb.org/en/latest/api/database/bulk-api.html#db-all-docs
func (db *PouchDB) AllDocs(result interface{}, opts Options) error {
rw := NewResultWaiter()
db.Call("allDocs", opts.compile(), rw.Done)
obj, err := rw.Read()
if err != nil {
return err
}
return ConvertJSObject(obj, &result)
}
// Invoke a map/reduce function, which allows you to perform more complex
// queries on PouchDB than what you get with allDocs().
//
// See http://pouchdb.com/api.html#query_database
func (db *PouchDB) Query(view string, result interface{}, opts Options) error {
rw := NewResultWaiter()
db.Call("query", view, opts.compile(), rw.Done)
obj, err := rw.Read()
if err != nil {
return err
}
return ConvertJSObject(obj, &result)
}
type MapFunc func(string)
func (db *PouchDB) QueryFunc(fn MapFunc, result interface{}, opts Options) error {
rw := NewResultWaiter()
db.Call("query", fn, opts.compile(), rw.Done)
obj, err := rw.Read()
if err != nil {
return err
}
return ConvertJSObject(obj, &result)
}
// Replicate will replicate data from source to target in the foreground.
// For "live" replication use ReplicateLive()
// See: http://pouchdb.com/api.html#replication
func Replicate(source, target *PouchDB, opts Options) (Result, error) {
rw := NewResultWaiter()
repl := globalPouch().Call("replicate", source, target, opts.compile())
repl.Call("then", func(r *js.Object) {
rw.Done(nil, r)
})
repl.Call("catch", func(e *js.Object) {
rw.Done(e, nil)
})
return rw.ReadResult()
}
// Sync data from src to target and target to src. This is a convenience method for bidirectional data replication.
//
// See http://pouchdb.com/api.html#sync
func Sync(source, target *PouchDB, opts Options) ([]Result, error) {
results := make([]Result, 2)
result, err := Replicate(source, target, opts)
results[0] = result
if err != nil {
return results, err
}
result, err = Replicate(target, source, opts)
results[1] = result
return results, err
}
// Replicate will replicate data from source to target in the background.
// This method returns a *ChangeFeed which can be used to monitor progress
// in a Go routine. For foreground sync, use Replicate().
// See: http://pouchdb.com/api.html#replication
// ViewCleanup cleans up any stale map/reduce indexes.
//
// See: http://pouchdb.com/api.html#view_cleanup
func (db *PouchDB) ViewCleanup() error {
rw := NewResultWaiter()
db.Call("viewCleanup", rw.Done)
return rw.Error()
}
// Compact triggers a compaction operation in the local or remote database.
//
// See: http://pouchdb.com/api.html#compaction
func (db *PouchDB) Compact(opts Options) error {
rw := NewResultWaiter()
db.Call("compact", opts, rw.Done)
return rw.Error()
}
// RevsDiff will, given a set of document/revision IDs return the subset of
// those that do not correspond to revisions stored in the database.
// See: http://pouchdb.com/api.html#revisions_diff
// func (db *PouchDB) RevsDiff(diff *js.Object, fn interface{}) {
// db.Call("revsDiff", diff, fn)
// }
// Call calls the underlying PouchDB object's method with the given name and
// arguments. This method is used internally, and may also facilitate the use
// of plugins which may add methods to PouchDB which are not implemented in
// the GopherJS bindings.
func (db *PouchDB) Call(name string, args ...interface{}) *js.Object {
return db.o.Call(name, args...)
}
// GetJS gets the requested key from the underlying PouchDB object
func (db *PouchDB) GetJS(name string) *js.Object {
return db.o.Get(name)
}
// OnCreate registers the function as an event listener for the 'created' event.
// See https://pouchdb.com/api.html#events
func OnCreate(fn func(dbName string)) {
globalPouch().Call("on", "created", func(dbname string) {
go fn(dbname)
})
}
// OnDestroy registers the function as an event listener for the 'destroyed'
// event. See https://pouchdb.com/api.html#events
func OnDestroy(fn func(dbName string)) {
globalPouch().Call("on", "destroyed", func(dbname string) {
go fn(dbname)
})
}