-
Notifications
You must be signed in to change notification settings - Fork 15
/
couchdb.go
47 lines (36 loc) · 1.1 KB
/
couchdb.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
package main
import (
"fmt"
"context"
"encoding/json"
kivik "github.com/go-kivik/kivik/v4"
_ "github.com/go-kivik/couchdb/v4"
"github.com/pkg/errors"
)
func SaveToCouchDB(kvWriteSet []byte) error{
dbName := "offchaindb"
couchdbUrl := "http://localhost:5990/"
fmt.Println("\n Saving the Block details to CouchDB")
fmt.Println(" CouchDB URL = ", couchdbUrl)
fmt.Println(" DB Name = ", dbName)
client, err := kivik.New("couch", couchdbUrl)
if err != nil {
fmt.Println("failed to set couchdb - "+err.Error())
return errors.WithMessage(err,"failed to set couchdb: ")
}
db := client.DB(dbName)
User := &SampleUser{}
err = json.Unmarshal(kvWriteSet, User)
if err != nil{
return errors.WithMessage(err,"unmarshaling write set error: ")
}
fmt.Println("\n Collection Key ", User.Email)
rev, err := db.Put(context.TODO(), User.Email, User)
if err != nil {
fmt.Println(" Error during insertion - "+err.Error())
} else {
fmt.Println(" User Inserted into CouchDB - Revision = "+rev+" \n")
}
fmt.Println(" ######################################################################## \n")
return nil
}