...

Source file src/github.com/go-kivik/kivik/v4/bulk.go

Documentation: github.com/go-kivik/kivik/v4

     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  package kivik
    14  
    15  import (
    16  	"context"
    17  	"errors"
    18  	"net/http"
    19  
    20  	"github.com/go-kivik/kivik/v4/driver"
    21  	internal "github.com/go-kivik/kivik/v4/int/errors"
    22  )
    23  
    24  // BulkResult is the result of a single BulkDoc update.
    25  type BulkResult struct {
    26  	ID    string `json:"id"`
    27  	Rev   string `json:"rev"`
    28  	Error error
    29  }
    30  
    31  // BulkDocs allows you to create and update multiple documents at the same time
    32  // within a single request. This function returns an iterator over the results
    33  // of the bulk operation.
    34  //
    35  // See the [CouchDB documentation].
    36  //
    37  // As with [DB.Put], each individual document may be a JSON-marshable object, or
    38  // a raw JSON string in a [encoding/json.RawMessage], or [io.Reader].
    39  //
    40  // [CouchDB documentation]: https://docs.couchdb.org/en/stable/api/database/bulk-api.html#db-bulk-docs
    41  func (db *DB) BulkDocs(ctx context.Context, docs []interface{}, options ...Option) ([]BulkResult, error) {
    42  	if db.err != nil {
    43  		return nil, db.err
    44  	}
    45  	docsi, err := docsInterfaceSlice(docs)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	if len(docsi) == 0 {
    50  		return nil, &internal.Error{Status: http.StatusBadRequest, Err: errors.New("kivik: no documents provided")}
    51  	}
    52  	endQuery, err := db.startQuery()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	defer endQuery()
    57  	opts := multiOptions(options)
    58  	if bulkDocer, ok := db.driverDB.(driver.BulkDocer); ok {
    59  		bulki, err := bulkDocer.BulkDocs(ctx, docsi, opts)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		results := make([]BulkResult, len(bulki))
    64  		for i, result := range bulki {
    65  			results[i] = BulkResult(result)
    66  		}
    67  		return results, nil
    68  	}
    69  	results := make([]BulkResult, 0, len(docsi))
    70  	for _, doc := range docsi {
    71  		var err error
    72  		var id, rev string
    73  		if docID, ok := extractDocID(doc); ok {
    74  			id = docID
    75  			rev, err = db.Put(ctx, id, doc, opts)
    76  		} else {
    77  			id, rev, err = db.CreateDoc(ctx, doc, opts)
    78  		}
    79  		results = append(results, BulkResult{
    80  			ID:    id,
    81  			Rev:   rev,
    82  			Error: err,
    83  		})
    84  	}
    85  	return results, nil
    86  }
    87  
    88  func docsInterfaceSlice(docsi []interface{}) ([]interface{}, error) {
    89  	for i, doc := range docsi {
    90  		x, err := normalizeFromJSON(doc)
    91  		if err != nil {
    92  			return nil, &internal.Error{Status: http.StatusBadRequest, Err: err}
    93  		}
    94  		docsi[i] = x
    95  	}
    96  	return docsi, nil
    97  }
    98  

View as plain text