github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/bulk.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build js 14 15 package pouchdb 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/gopherjs/gopherjs/js" 22 23 "github.com/go-kivik/kivik/v4/driver" 24 internal "github.com/go-kivik/kivik/v4/int/errors" 25 ) 26 27 type bulkResult struct { 28 *js.Object 29 OK bool `js:"ok"` 30 ID string `js:"id"` 31 Rev string `js:"rev"` 32 Error string `js:"name"` 33 StatusCode int `js:"status"` 34 Reason string `js:"message"` 35 IsError bool `js:"error"` 36 } 37 38 func (d *db) BulkDocs(ctx context.Context, docs []interface{}, options driver.Options) (results []driver.BulkResult, err error) { 39 defer func() { 40 if r := recover(); r != nil { 41 if e, ok := r.(error); ok { 42 err = e 43 } else { 44 err = fmt.Errorf("%v", r) 45 } 46 } 47 }() 48 opts := map[string]interface{}{} 49 options.Apply(opts) 50 result, err := d.db.BulkDocs(ctx, docs, opts) 51 if err != nil { 52 return nil, err 53 } 54 for result != js.Undefined && result.Length() > 0 { 55 r := &bulkResult{} 56 r.Object = result.Call("shift") 57 var err error 58 if r.IsError { 59 err = &internal.Error{Status: r.StatusCode, Message: r.Reason} 60 } 61 results = append(results, driver.BulkResult{ 62 ID: r.ID, 63 Rev: r.Rev, 64 Error: err, 65 }) 66 } 67 68 return results, nil 69 }