...
1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "context"
17 "encoding/json"
18 "fmt"
19 "net/http"
20
21 "github.com/go-kivik/kivik/v4/couchdb/chttp"
22 "github.com/go-kivik/kivik/v4/driver"
23 )
24
25 func (d *db) BulkGet(ctx context.Context, docs []driver.BulkGetReference, options driver.Options) (driver.Rows, error) {
26 opts := map[string]interface{}{}
27 options.Apply(opts)
28 query, err := optionsToParams(opts)
29 if err != nil {
30 return nil, err
31 }
32 body := map[string]interface{}{
33 "docs": docs,
34 }
35 chttpOpts := &chttp.Options{
36 Query: query,
37 GetBody: chttp.BodyEncoder(body),
38 Header: http.Header{
39 chttp.HeaderIdempotencyKey: []string{},
40 },
41 }
42 resp, err := d.Client.DoReq(ctx, http.MethodPost, d.path("_bulk_get"), chttpOpts)
43 if err != nil {
44 return nil, err
45 }
46 if err = chttp.ResponseError(resp); err != nil {
47 return nil, err
48 }
49 return newBulkGetRows(ctx, resp.Body), nil
50 }
51
52
53
54 type bulkGetError struct {
55 ID string `json:"id"`
56 Rev string `json:"rev"`
57 Err string `json:"error"`
58 Reason string `json:"reason"`
59 }
60
61 var _ error = &bulkGetError{}
62
63 func (e *bulkGetError) Error() string {
64 return fmt.Sprintf("%s: %s", e.Err, e.Reason)
65 }
66
67 type bulkResultDoc struct {
68 Doc json.RawMessage `json:"ok,omitempty"`
69 Error *bulkGetError `json:"error,omitempty"`
70 }
71
72 type bulkResult struct {
73 ID string `json:"id"`
74 Docs []bulkResultDoc `json:"docs"`
75 }
76
View as plain text